-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhearing.html
More file actions
573 lines (502 loc) · 15.3 KB
/
hearing.html
File metadata and controls
573 lines (502 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Practice your pronunciation with interactive flashcards">
<title>5s9 English | Voice Cards</title>
<link rel="preconnect" href="https://cdnjs.cloudflare.com">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastify-js/1.12.0/toastify.min.css">
<style>
:root {
--primary-color: #4CAF50;
--accent-color: #1976D2;
--error-color: #f44336;
--text-primary: #333;
--text-secondary: #666;
--background-gradient: linear-gradient(135deg, navy 0%, #a400ff 100%);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
min-height: 100vh;
margin: 0;
background: var(--background-gradient);
display: grid;
place-items: center;
padding: 1.25rem;
height: 100%;
}
.progress-bar {
width: 90%;
max-width: 350px;
height: 4px;
background: #e0e0e0;
border-radius: 2px;
margin-bottom: 1.25rem;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: var(--primary-color);
transform-origin: left;
transition: transform 0.3s ease;
}
.flashcard-container {
width: 90%;
max-width: 350px;
height: calc(100vh - 8.75rem);
margin-bottom: 5rem;
}
.flashcard {
height: 100%;
background: #fff;
border-radius: 0.75rem;
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
padding: 1.25rem;
display: flex;
flex-direction: column;
gap: 1rem;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem;
background: #f8f9fa;
border-radius: 1.25rem;
}
.icon-button {
background: none;
border: none;
padding: 0.5rem;
border-radius: 50%;
cursor: pointer;
transition: transform 0.2s ease;
color: var(--text-primary);
}
.icon-button:hover {
transform: scale(1.1);
background: rgba(0,0,0,0.05);
}
.icon-button:focus-visible {
outline: 2px solid var(--primary-color);
outline-offset: 2px;
}
.practice-buttons {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0.625rem;
margin: 1rem 0;
}
.practice-btn {
background: var(--primary-color);
color: #fff;
border: none;
padding: 0.5rem 1rem;
border-radius: 0.375rem;
cursor: pointer;
font-size: 0.875rem;
display: flex;
align-items: center;
gap: 0.375rem;
transition: background-color 0.2s ease;
}
.practice-btn:hover:not(:disabled) {
background: #45a049;
}
.practice-btn:disabled {
opacity: 0.7;
cursor: not-allowed;
}
.practice-btn.listening {
background: var(--error-color);
}
.controls {
position: fixed;
bottom: 1.25rem;
left: 0;
right: 0;
display: flex;
justify-content: center;
gap: 0.5rem;
padding: 0.938rem;
}
.controls button {
background: #ffd700;
color: navy;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 0.375rem;
cursor: pointer;
font-size: 1rem;
display: flex;
align-items: center;
gap: 0.5rem;
transition: all 0.2s ease;
}
.controls button:hover:not(:disabled) {
background: var(--accent-color);
color: white;
transform: translateY(-1px);
}
@media (prefers-reduced-motion: reduce) {
* {
transition: none !important;
animation: none !important;
}
}
/* Additional styles for the hearing practice features */
.practice-text {
font-size: 1.25rem;
margin: 1rem 0;
text-align: center;
min-height: 3rem;
}
.answer-input {
width: 100%;
padding: 0.75rem;
border: 2px solid #ddd;
border-radius: 0.375rem;
font-size: 1rem;
margin: 1rem 0;
transition: border-color 0.2s ease;
}
.answer-input:focus {
outline: none;
border-color: var(--primary-color);
}
.result-section {
margin-top: 1rem;
text-align: center;
}
.score-status {
margin-top: 0.5rem;
padding: 0.5rem;
border-radius: 0.375rem;
font-size: 0.875rem;
}
.score-status.pending {
background: #fff3e0;
color: #e65100;
}
.score-status.success {
background: #e8f5e9;
color: #2e7d32;
}
.practice-btn.playing {
background: var(--accent-color);
}
.practice-feedback {
margin-top: 1rem;
font-size: 0.875rem;
color: var(--text-secondary);
}
.correct-answer {
color: var(--primary-color);
font-weight: bold;
margin-top: 0.5rem;
}
</style>
</head>
<body>
<div class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="progress-fill"></div>
</div>
<main class="flashcard-container">
<div class="flashcard">
<div class="card-header">
<span class="card-counter" aria-live="polite">Card 1 of 3</span>
<div class="card-actions">
<button class="icon-button" aria-label="Share flashcard" onclick="sharePage()">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"></path>
<polyline points="16 6 12 2 8 6"></polyline>
<line x1="12" y1="2" x2="12" y2="15"></line>
</svg>
</button>
<button class="icon-button" aria-label="Toggle favorite" onclick="toggleFavorite(this)">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path>
</svg>
</button>
</div>
</div>
<!-- Replace the flashcard-content div with this -->
<div class="flashcard-content">
<p id="practice-text" class="practice-text" aria-live="polite">
[Click Play to hear the sentence]
</p>
<div class="practice-buttons">
<button class="practice-btn" id="playButton" aria-label="Play audio">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polygon points="5 3 19 12 5 21 5 3"></polygon>
</svg>
Play
</button>
<button class="practice-btn" id="checkButton" aria-label="Check answer">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
Check
</button>
</div>
<input type="text"
id="userAnswer"
class="answer-input"
placeholder="Type what you hear..."
aria-label="Your answer"
autocomplete="off">
<div class="result-section" aria-live="polite">
<div id="comparisonResult">[Score will appear here]</div>
<div id="scoreStatus" class="score-status pending">
Try to achieve 95% accuracy to proceed
</div>
<div id="correctAnswer" class="correct-answer"></div>
</div>
</div>
</div>
</main>
<nav class="controls">
<button id="prevBtn" aria-label="Previous card">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M15 18l-6-6 6-6"/>
</svg>
Previous
</button>
<button id="nextBtn" aria-label="Next card">
Next
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M9 18l6-6-6-6"/>
</svg>
</button>
</nav>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastify-js/1.12.0/toastify.js" defer></script>
<script src="floating-back-button.js" defer></script>
<script>
// Modified Constants and State Management
const PASSING_SCORE = 95; // Updated to 95%
let currentCard = 0;
let currentScore = 0;
let isPlaying = false;
const cards = [
{
text: "The quick brown fox jumps over the lazy dog.",
passed: false
},
{
text: "She sells seashells by the seashore.",
passed: false
},
{
text: "How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
passed: false
}
];
// Updated DOM Elements
const elements = {
practiceText: document.getElementById("practice-text"),
playButton: document.getElementById("playButton"),
checkButton: document.getElementById("checkButton"),
userAnswer: document.getElementById("userAnswer"),
comparisonResult: document.getElementById("comparisonResult"),
scoreStatus: document.getElementById("scoreStatus"),
correctAnswer: document.getElementById("correctAnswer"),
prevBtn: document.getElementById("prevBtn"),
nextBtn: document.getElementById("nextBtn"),
counter: document.querySelector(".card-counter"),
progressFill: document.querySelector(".progress-fill")
};
// Card Navigation
function nextCard() {
if (currentCard < cards.length - 1) {
currentCard++;
updateCard();
}
}
function prevCard() {
if (currentCard > 0) {
currentCard--;
updateCard();
}
}
// Card Management
function updateCard() {
elements.practiceText.textContent = "[Click Play to hear the sentence]";
elements.userAnswer.value = "";
elements.comparisonResult.textContent = "[Score will appear here]";
elements.correctAnswer.textContent = "";
elements.scoreStatus.className = "score-status pending";
elements.scoreStatus.textContent = `Try to achieve ${PASSING_SCORE}% accuracy to proceed`;
elements.counter.textContent = `Card ${currentCard + 1} of ${cards.length}`;
elements.progressFill.style.transform = `scaleX(${(currentCard + 1) / cards.length})`;
currentScore = 0;
updateButtons();
}
// Update UI Elements
function updateButtons() {
elements.prevBtn.disabled = currentCard === 0;
elements.nextBtn.disabled = currentCard === cards.length - 1 || currentScore < PASSING_SCORE;
}
function updateScoreStatus() {
if (currentScore >= PASSING_SCORE) {
elements.scoreStatus.className = "score-status success";
elements.scoreStatus.textContent = "Great job! You can proceed to the next card";
elements.correctAnswer.textContent = `Correct! The sentence was: ${cards[currentCard].text}`;
cards[currentCard].passed = true;
} else {
elements.scoreStatus.className = "score-status pending";
// Provide hints based on score ranges
if (currentScore >= 80) {
elements.scoreStatus.textContent = "Very close! Listen carefully and try again";
} else if (currentScore >= 60) {
elements.scoreStatus.textContent = "Getting there! Focus on word order and small details";
} else if (currentScore >= 40) {
elements.scoreStatus.textContent = "Keep trying! Make sure to catch all the words";
} else {
elements.scoreStatus.textContent = "Listen again and type what you hear carefully";
}
}
}
// Text-to-Speech Functions
function playAudio() {
if (!isPlaying) {
isPlaying = true;
elements.playButton.classList.add("playing");
elements.playButton.disabled = true;
const utterance = new SpeechSynthesisUtterance(cards[currentCard].text);
utterance.onend = () => {
isPlaying = false;
elements.playButton.classList.remove("playing");
elements.playButton.disabled = false;
elements.userAnswer.focus();
};
speechSynthesis.speak(utterance);
}
}
// Answer Checking
function checkAnswer() {
const userAnswer = elements.userAnswer.value.trim();
const correctText = cards[currentCard].text;
const similarity = compareStrings(correctText, userAnswer);
currentScore = similarity * 100;
elements.comparisonResult.textContent = `${currentScore.toFixed(2)}% accurate`;
updateScoreStatus();
updateButtons();
}
// Enhanced String Comparison
function compareStrings(str1, str2) {
const words1 = str1.toLowerCase().replace(/[.,!?]/g, '').split(/\s+/);
const words2 = str2.toLowerCase().replace(/[.,!?]/g, '').split(/\s+/);
let matches = 0;
const maxLength = Math.max(words1.length, words2.length);
words1.forEach((word, index) => {
if (words2[index] === word) {
matches++;
}
});
return matches / maxLength;
}
// Favorites Management
function initializeFavorites() {
const urlWithCard = `${window.location.href.split('#')[0]}#card=${currentCard}`;
const favorites = JSON.parse(localStorage.getItem("favorites") || "{}");
if (favorites[urlWithCard]) {
const heartIcon = document.querySelector(".card-header button:last-child svg");
heartIcon.setAttribute("fill", "#2ecc71");
}
}
function toggleFavorite(button) {
const heartIcon = button.querySelector("svg");
const baseUrl = window.location.href.split('#')[0];
const urlWithCard = `${baseUrl}#card=${currentCard}`;
const pageTitle = `${document.title} - Card ${currentCard + 1}`;
let favorites = JSON.parse(localStorage.getItem("favorites") || "{}");
if (favorites[urlWithCard]) {
delete favorites[urlWithCard];
heartIcon.setAttribute("fill", "none");
} else {
favorites[urlWithCard] = {
title: pageTitle,
url: urlWithCard,
cardIndex: currentCard,
timestamp: new Date().toISOString()
};
heartIcon.setAttribute("fill", "#2ecc71");
}
localStorage.setItem("favorites", JSON.stringify(favorites));
}
// Share Functionality
function sharePage() {
const pageTitle = `${document.title} - Card ${currentCard + 1}`;
const baseUrl = window.location.href.split('#')[0];
const urlWithCard = `${baseUrl}#card=${currentCard}`;
if (navigator.share) {
navigator.share({
title: pageTitle,
url: urlWithCard
}).catch(console.error);
} else {
try {
navigator.clipboard.writeText(urlWithCard);
alert("Link copied to clipboard!");
} catch (err) {
alert("Unable to share or copy link");
}
}
}
// Event Listeners
function initializeEventListeners() {
elements.playButton.addEventListener("click", playAudio);
elements.checkButton.addEventListener("click", checkAnswer);
elements.userAnswer.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
checkAnswer();
}
});
elements.prevBtn.addEventListener("click", prevCard);
elements.nextBtn.addEventListener("click", () => {
if (currentScore < PASSING_SCORE) {
Toastify({
text: `Please achieve at least ${PASSING_SCORE}% accuracy to proceed`,
duration: 3000,
gravity: "top",
position: "center",
backgroundColor: "#f44336"
}).showToast();
} else if (currentCard < cards.length - 1) {
nextCard();
Toastify({
text: "Great job! Moving to next card",
duration: 3000,
gravity: "top",
position: "center",
backgroundColor: "#4CAF50"
}).showToast();
}
});
}
// Initialize Application
document.addEventListener("DOMContentLoaded", () => {
const hashParams = new URLSearchParams(window.location.hash.slice(1));
const cardIndex = hashParams.get('card');
if (cardIndex !== null) {
const index = parseInt(cardIndex);
if (index >= 0 && index < cards.length) {
currentCard = index;
}
}
initializeFavorites();
initializeEventListeners();
updateCard();
});
</script>
</body>
</html>