-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1848 lines (1610 loc) · 73.4 KB
/
index.html
File metadata and controls
1848 lines (1610 loc) · 73.4 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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WordFlow - Daily Vocabulary</title>
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
window.transformersPipeline = pipeline;
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
max-width: 500px;
width: 100%;
background: white;
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
overflow: hidden;
}
.header {
background: #4A90E2;
color: white;
padding: 20px;
text-align: center;
}
.header h1 {
font-size: 24px;
margin-bottom: 5px;
}
.date {
font-size: 14px;
opacity: 0.9;
}
.word-card {
padding: 25px;
text-align: center;
}
.word {
font-size: 42px;
font-weight: 700;
color: #333;
margin-bottom: 10px;
}
.pronunciation {
color: #666;
font-size: 18px;
margin-bottom: 20px;
}
.audio-btn {
background: #50E3C2;
border: none;
border-radius: 50%;
width: 50px;
height: 50px;
margin: 0 auto 20px;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 4px 10px rgba(80, 227, 194, 0.4);
transition: transform 0.2s;
}
.audio-btn:hover:not(:disabled) {
transform: scale(1.05);
}
.audio-btn:disabled {
background: #cccccc;
cursor: not-allowed;
opacity: 0.6;
box-shadow: none;
}
.audio-btn svg {
width: 24px;
height: 24px;
fill: white;
}
.definition {
background: #f8f9fa;
border-radius: 12px;
padding: 20px;
margin-bottom: 20px;
text-align: left;
}
.definition h3 {
color: #4A90E2;
margin-bottom: 10px;
font-size: 18px;
}
.definition p {
line-height: 1.6;
color: #444;
}
.example {
background: #f0f7ff;
border-left: 4px solid #4A90E2;
padding: 15px;
margin: 15px 0;
border-radius: 0 8px 8px 0;
position: relative;
}
.example-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
}
.regenerate-btn {
background: #4A90E2;
border: none;
border-radius: 50%;
width: 32px;
height: 32px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s;
padding: 0;
flex-shrink: 0;
}
.regenerate-btn:hover {
background: #3a7bc8;
transform: rotate(90deg);
}
.regenerate-btn:active {
transform: rotate(180deg) scale(0.95);
}
.regenerate-btn.loading {
animation: spin 1s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.regenerate-btn svg {
width: 18px;
height: 18px;
fill: white;
}
.example p {
margin: 0;
position: relative;
}
.example-text-container {
position: relative;
display: inline-block;
width: 100%;
}
.skeleton-loader {
display: none !important;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 10;
background: #f0f7ff;
opacity: 0;
transition: opacity 0.1s ease;
}
.skeleton-loader.active {
display: block !important;
opacity: 1 !important;
}
.skeleton-line {
height: 1.2em;
background: linear-gradient(90deg,
#e0e0e0 25%,
#f0f0f0 50%,
#e0e0e0 75%);
background-size: 200% 100%;
border-radius: 4px;
margin-bottom: 0.5em;
animation: skeleton-shimmer 1.5s ease-in-out infinite;
}
.skeleton-line:last-child {
margin-bottom: 0;
}
.skeleton-line.short {
width: 60%;
}
.skeleton-line.medium {
width: 80%;
}
.skeleton-line.long {
width: 100%;
}
@keyframes skeleton-shimmer {
0% {
background-position: -200% 0;
}
100% {
background-position: 200% 0;
}
}
.char-morph {
display: inline-block;
transition: opacity 0.3s ease, transform 0.3s ease;
}
.char-morph.morphing {
opacity: 0.3;
transform: scale(0.8);
}
.char-morph.morphing-in {
animation: charMorphIn 0.4s ease forwards;
}
@keyframes charMorphIn {
0% {
opacity: 0;
transform: scale(0.5) translateY(-5px);
}
50% {
opacity: 0.7;
transform: scale(1.1) translateY(0);
}
100% {
opacity: 1;
transform: scale(1) translateY(0);
}
}
.practice-section {
padding: 0 25px 25px;
}
.practice-section h3 {
color: #333;
margin-bottom: 15px;
text-align: center;
}
.practice-btns {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.practice-btn {
flex: 1;
padding: 12px;
border: none;
border-radius: 10px;
background: #f0f7ff;
color: #4A90E2;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.practice-btn:hover {
background: #4A90E2;
color: white;
}
.practice-btn.active {
background: #4A90E2;
color: white;
}
.practice-area {
background: #f8f9fa;
border-radius: 12px;
padding: 20px;
margin-bottom: 20px;
min-height: 120px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.practice-text {
font-size: 18px;
text-align: center;
margin-bottom: 15px;
color: #333;
}
.practice-instruction {
font-size: 14px;
color: #666;
text-align: center;
margin-bottom: 15px;
}
.record-btn {
background: #4A90E2;
color: white;
border: none;
padding: 10px 20px;
border-radius: 20px;
font-weight: 600;
cursor: pointer;
display: flex;
align-items: center;
gap: 8px;
transition: background 0.2s;
}
.record-btn:hover {
background: #3a7bc8;
}
.record-btn.recording {
background: #e74c3c;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.05);
}
100% {
transform: scale(1);
}
}
.transcript {
margin-top: 15px;
padding: 10px;
background: white;
border-radius: 8px;
width: 100%;
min-height: 60px;
border: 1px solid #e0e0e0;
}
.transcript-text {
font-style: italic;
color: #555;
}
.input-field {
width: 100%;
padding: 12px;
border: 2px solid #4A90E2;
border-radius: 8px;
font-size: 16px;
margin-bottom: 15px;
font-family: inherit;
}
.input-field:focus {
outline: none;
border-color: #3a7bc8;
}
.textarea-field {
width: 100%;
padding: 12px;
border: 2px solid #4A90E2;
border-radius: 8px;
font-size: 16px;
margin-bottom: 15px;
font-family: inherit;
resize: vertical;
min-height: 80px;
}
.textarea-field:focus {
outline: none;
border-color: #3a7bc8;
}
.submit-btn {
background: #4A90E2;
color: white;
border: none;
padding: 10px 20px;
border-radius: 20px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
.submit-btn:hover {
background: #3a7bc8;
}
.feedback {
margin-top: 15px;
padding: 12px;
border-radius: 8px;
font-size: 14px;
}
.feedback.correct {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.feedback.incorrect {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.feedback.info {
background: #d1ecf1;
color: #0c5460;
border: 1px solid #bee5eb;
}
.progress {
display: flex;
justify-content: flex-end;
align-items: center;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #eee;
}
.next-btn {
background: #4A90E2;
color: white;
border: none;
padding: 10px 20px;
border-radius: 20px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
.next-btn:hover:not(:disabled) {
background: #3a7bc8;
}
.next-btn:disabled {
background: #cccccc;
cursor: not-allowed;
opacity: 0.6;
}
.loading {
text-align: center;
padding: 40px;
color: #666;
opacity: 0;
transition: opacity 0.4s ease-in-out;
}
.loading.show {
opacity: 1;
}
.loading img {
max-width: 200px;
margin-bottom: 20px;
}
.error {
text-align: center;
padding: 40px;
color: #e74c3c;
}
.browser-warning {
background: #fff3cd;
color: #856404;
padding: 10px;
border-radius: 8px;
margin: 10px 0;
font-size: 14px;
text-align: center;
border: 1px solid #ffeaa7;
}
.hidden {
display: none;
}
#word-container {
opacity: 0;
transition: opacity 0.5s ease-in-out;
}
#word-container.show {
opacity: 1;
}
@media (max-width: 500px) {
.word {
font-size: 36px;
}
.practice-btns {
flex-direction: column;
}
.loading img {
max-width: 64px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>WordDice</h1>
<div class="date" id="current-date">Loading...</div>
</div>
<div id="loading" class="loading show">
<img src="https://media.tenor.com/sUiwSBs8S6QAAAAj/dice-game.gif" alt="Loading animation">
<p>Loading today's word...</p>
</div>
<div id="word-container" class="hidden">
<div class="word-card">
<div class="word" id="word">Serendipity</div>
<div class="pronunciation" id="pronunciation">/ˌser.ənˈdɪp.ə.ti/</div>
<button class="audio-btn" id="audio-btn">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z">
</path>
</svg>
</button>
<div class="definition">
<h3>Definition</h3>
<p id="definition">The occurrence and development of events by chance in a happy or beneficial way.
</p>
<div class="example">
<div class="example-header">
<p id="example" style="flex: 1; margin: 0;">"A fortunate stroke of serendipity led them to discover the beautiful hidden
beach."</p>
<button class="regenerate-btn" id="regenerate-btn" title="Generate new example sentence">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>
</svg>
</button>
</div>
</div>
</div>
</div>
<div class="practice-section">
<h3>Practice Using This Word</h3>
<div id="browser-warning" class="browser-warning hidden">
Speech recognition works best in Chrome and Edge. Make sure you're in a quiet environment and allow
microphone access when prompted.
</div>
<div class="practice-btns">
<button class="practice-btn active" id="repeat-btn">Repeat Sentence</button>
<button class="practice-btn" id="fill-btn">Fill in Blank</button>
<button class="practice-btn" id="create-btn">Create Sentence</button>
</div>
<div class="practice-area">
<div class="practice-text" id="practice-text">
"A fortunate stroke of serendipity led them to discover the beautiful hidden beach."
</div>
<div class="practice-instruction" id="practice-instruction">
Click the record button and repeat the sentence above
</div>
<div id="recording-section">
<button class="record-btn" id="record-btn">
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path
d="M8 12c2.21 0 4-1.79 4-4V4c0-2.21-1.79-4-4-4S4 1.79 4 4v4c0 2.21 1.79 4 4 4zm0 1c-2.76 0-5-2.24-5-5V4c0-2.76 2.24-5 5-5s5 2.24 5 5v4c0 2.76-2.24 5-5 5z" />
<path
d="M0 7.5v1c0 .28.22.5.5.5H2v-2H.5c-.28 0-.5.22-.5.5zM16 7.5c0-.28-.22-.5-.5-.5H14v2h1.5c.28 0 .5-.22.5-.5v-1zM4.5 13.5c-.28 0-.5.22-.5.5v1c0 .28.22.5.5.5h1c.28 0 .5-.22.5-.5V14c0-.28-.22-.5-.5-.5h-1zM11.5 13.5c-.28 0-.5.22-.5.5v1c0 .28.22.5.5.5h1c.28 0 .5-.22.5-.5V14c0-.28-.22-.5-.5-.5h-1zM8 15c-.28 0-.5.22-.5.5v1c0 .28.22.5.5.5s.5-.22.5-.5v-1c0-.28-.22-.5-.5-.5z" />
</svg>
Start Recording
</button>
<div class="transcript">
<div class="transcript-text" id="transcript-text">Your speech will appear here...</div>
</div>
</div>
<div id="input-section" class="hidden">
<input type="text" class="input-field" id="practice-input"
placeholder="Type your answer here...">
<textarea class="textarea-field hidden" id="practice-textarea"
placeholder="Type your sentence here..."></textarea>
<button class="submit-btn" id="submit-btn">Submit</button>
<div id="feedback" class="feedback hidden"></div>
</div>
</div>
<div class="progress">
<button class="next-btn" id="next-btn">Next Word</button>
</div>
</div>
</div>
<div id="error" class="error hidden">
<p>Sorry, we couldn't load today's word. Please try again later.</p>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Elements
const loadingEl = document.getElementById('loading');
const wordContainerEl = document.getElementById('word-container');
const errorEl = document.getElementById('error');
const browserWarningEl = document.getElementById('browser-warning');
const currentDateEl = document.getElementById('current-date');
const wordEl = document.getElementById('word');
const pronunciationEl = document.getElementById('pronunciation');
const definitionEl = document.getElementById('definition');
const exampleEl = document.getElementById('example');
const regenerateBtn = document.getElementById('regenerate-btn');
const audioBtn = document.getElementById('audio-btn');
const nextBtn = document.getElementById('next-btn');
const repeatBtn = document.getElementById('repeat-btn');
const fillBtn = document.getElementById('fill-btn');
const createBtn = document.getElementById('create-btn');
const practiceTextEl = document.getElementById('practice-text');
const practiceInstructionEl = document.getElementById('practice-instruction');
const recordBtn = document.getElementById('record-btn');
const transcriptTextEl = document.getElementById('transcript-text');
const recordingSectionEl = document.getElementById('recording-section');
const inputSectionEl = document.getElementById('input-section');
const practiceInputEl = document.getElementById('practice-input');
const practiceTextareaEl = document.getElementById('practice-textarea');
const submitBtn = document.getElementById('submit-btn');
const feedbackEl = document.getElementById('feedback');
// Speech synthesis and recognition
const synth = window.speechSynthesis;
let recognition = null;
let isRecording = false;
let currentWord = null;
let silenceTimeout = null;
let lastSpeechTime = null;
const SILENCE_TIMEOUT_MS = 3000; // 3 seconds of silence before auto-stop
let regenerateCancelled = false; // Flag to cancel regeneration
// Word list for fallback
let wordList = [
"serendipity", "eloquent", "resilient", "ephemeral", "lucid",
"magnanimous", "ubiquitous", "voracious", "zenith", "nostalgia",
"quintessential", "ambiguous", "diligent", "empathy", "frugal"
];
// CSV word list management
// Configure your CSV file URL here
const CSV_URL = 'https://raw.githubusercontent.com/codefrydev/Data/refs/heads/main/Dictionary/CommonWords'; // Set your CSV file URL here, e.g., 'https://example.com/words.csv'
let csvWords = [];
let isCsvLoaded = false;
// Local LLM for generating example sentences
let llmPipeline = null;
let llmLoading = false;
let llmReady = false;
// Initialize local LLM (non-blocking, runs in background)
async function initializeLLM() {
if (llmLoading || llmReady) return;
try {
llmLoading = true;
console.log('Loading local LLM for sentence generation (non-blocking)...');
// Wait for transformers pipeline to be available (with timeout)
if (typeof window.transformersPipeline === 'undefined') {
await new Promise(resolve => {
const checkTransformers = setInterval(() => {
if (typeof window.transformersPipeline !== 'undefined') {
clearInterval(checkTransformers);
resolve();
}
}, 100);
// Timeout after 5 seconds - don't wait too long
setTimeout(() => {
clearInterval(checkTransformers);
resolve();
}, 5000);
});
}
if (typeof window.transformersPipeline === 'undefined') {
throw new Error('Transformers.js library not loaded');
}
llmPipeline = await window.transformersPipeline(
'text-generation',
'Xenova/gpt2',
{
dtype: 'q8',
}
);
llmReady = true;
llmLoading = false;
console.log('Local LLM loaded successfully');
} catch (error) {
console.error('Error loading LLM:', error);
llmLoading = false;
llmReady = false; // Explicitly mark as not ready
// Continue without LLM, will use API examples
}
}
// Generate example sentence using local LLM
async function generateExampleSentence(word, definition) {
if (!llmReady || !llmPipeline) {
return null;
}
try {
// Create a conversational prompt that guides the LLM to generate natural sentences
const prompt = `Write a natural, conversational sentence using the word "${word}" which means "${definition}". Make it sound like everyday speech:`;
const output = await llmPipeline(prompt, {
max_new_tokens: 50,
temperature: 0.7,
do_sample: true,
top_k: 40,
top_p: 0.85,
repetition_penalty: 1.4,
pad_token_id: 50256,
});
let generatedText = output[0].generated_text;
// Extract the sentence after the prompt
const promptIndex = generatedText.indexOf(prompt);
if (promptIndex !== -1) {
let sentence = generatedText.substring(promptIndex + prompt.length).trim();
// Clean up the sentence
sentence = sentence.replace(/^["']|["']$/g, ''); // Remove quotes
// Remove any trailing incomplete words or fragments
sentence = sentence.split(/[.!?]/)[0]; // Take first complete sentence
sentence = sentence.trim();
// Filter out weird or nonsensical sentences
// Check for minimum length and ensure it's a real sentence
if (sentence.length < 10 || sentence.length > 200) {
return null; // Too short or too long, skip
}
// Check for repetitive patterns (e.g., "word word word")
const words = sentence.split(/\s+/);
if (words.length < 3) {
return null; // Too few words, not a proper sentence
}
// Check for too many repeated words (indicates weird generation)
const wordCounts = {};
for (let w of words) {
const lower = w.toLowerCase();
wordCounts[lower] = (wordCounts[lower] || 0) + 1;
if (wordCounts[lower] > 3) {
return null; // Too repetitive, skip
}
}
// Ensure the word is in the sentence
if (!sentence.toLowerCase().includes(word.toLowerCase())) {
return null; // Word not found, skip this generation
}
// Capitalize first letter
if (sentence.length > 0) {
sentence = sentence.charAt(0).toUpperCase() + sentence.slice(1);
// Add period if missing
if (!sentence.match(/[.!?]$/)) {
sentence += '.';
}
return sentence;
}
}
// Fallback: try to extract any sentence containing the word
const sentences = generatedText.split(/[.!?]/);
for (let sent of sentences) {
sent = sent.trim();
// Better validation: check for reasonable length and word count
if (sent.length >= 15 && sent.length <= 200 &&
sent.toLowerCase().includes(word.toLowerCase())) {
const words = sent.split(/\s+/);
if (words.length >= 3) {
sent = sent.replace(/^["']|["']$/g, '').trim();
if (sent.length > 0) {
sent = sent.charAt(0).toUpperCase() + sent.slice(1);
if (!sent.match(/[.!?]$/)) {
sent += '.';
}
return sent;
}
}
}
}
return null;
} catch (error) {
console.error('Error generating sentence:', error);
return null;
}
}
// Automatically load CSV on page load
async function loadCsvOnInit() {
if (!CSV_URL || !CSV_URL.trim()) {
console.log('No CSV URL configured, using default word list');
loadNewWord();
return;
}
try {
const response = await fetch(CSV_URL);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const csvText = await response.text();
csvWords = parseCsv(csvText);
if (csvWords.length === 0) {
throw new Error('No words found in CSV file');
}
// Use CSV words instead of default wordList
wordList = csvWords;
isCsvLoaded = true;
console.log(`Loaded ${csvWords.length} words from CSV`);
// Load word after CSV is loaded
loadNewWord();
} catch (error) {
console.error('Error loading CSV:', error);
// Fall back to default word list if CSV fails
console.log('Falling back to default word list');
loadNewWord();
}
}
// Parse CSV content and extract words
function parseCsv(csvText) {
const words = [];
const lines = csvText.split('\n');
for (let line of lines) {
line = line.trim();
if (!line) continue;
// Handle CSV format - split by comma, but handle quoted values
const columns = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
inQuotes = !inQuotes;
} else if (char === ',' && !inQuotes) {
columns.push(current.trim());
current = '';
} else {
current += char;
}
}
// Add the last column
columns.push(current.trim());
// Extract all words from all columns (for single-line comma-separated word lists)
for (let col of columns) {
// Remove quotes if present
let word = col.replace(/^"|"$/g, '').trim();
if (word) {
words.push(word);
}
}
}
return words;
}
// Set current date
const now = new Date();
currentDateEl.textContent = now.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
// Initialize speech recognition
function initializeSpeechRecognition() {
// Check for browser support
if (!('webkitSpeechRecognition' in window) && !('SpeechRecognition' in window)) {
browserWarningEl.textContent = 'Speech recognition is not supported in this browser. Please try Chrome or Edge.';
browserWarningEl.classList.remove('hidden');
recordBtn.disabled = true;
return false;
}
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRecognition();
// Configure recognition
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = 'en-US';
recognition.maxAlternatives = 1;
// Set up event handlers
recognition.onstart = function () {
console.log('Speech recognition started');
transcriptTextEl.textContent = "Listening... Speak now!";
lastSpeechTime = null; // Reset - no speech detected yet
startSilenceTimer();
};
recognition.onresult = function (event) {
let interimTranscript = '';
let finalTranscript = '';
// Process all results
for (let i = event.resultIndex; i < event.results.length; i++) {
const transcript = event.results[i][0].transcript;
if (event.results[i].isFinal) {
finalTranscript += transcript + ' ';
} else {
interimTranscript += transcript;
}
}
// Update last speech time if we have any transcript
if (interimTranscript || finalTranscript) {
lastSpeechTime = Date.now();
resetSilenceTimer();
}