-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
506 lines (435 loc) · 18.9 KB
/
Copy pathindex.html
File metadata and controls
506 lines (435 loc) · 18.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Fuzzy Word/Phrase Counter with Dynamic RPM</title>
<style>
:root {
--primary-color: #4A90E2;
--secondary-color: #50E3C2;
--background-color: #F8F9FA;
--card-background: #FFFFFF;
--text-color: #333333;
--border-color: #DDDDDD;
--red-color: #FF6B6B;
--dark-blue: #2A60A0; /* Slightly darker primary for hover */
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.container {
width: 100%;
max-width: 600px;
background-color: var(--card-background);
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
padding: 30px;
text-align: center;
border: 1px solid var(--border-color);
}
h1 {
color: var(--primary-color);
font-size: 2.2rem;
margin-bottom: 20px;
}
.controls {
display: flex;
flex-direction: column;
gap: 15px;
margin-bottom: 25px;
}
.input-group {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 8px;
}
label {
font-weight: 600;
font-size: 1rem;
color: #555;
}
#wordToCountInput {
width: 100%;
padding: 12px;
border: 1px solid var(--border-color);
border-radius: 8px;
font-size: 1rem;
box-sizing: border-box;
transition: border-color 0.3s;
}
#wordToCountInput:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
}
.button-group {
display: flex;
gap: 10px;
width: 100%;
}
button {
padding: 12px 25px;
font-size: 1rem;
font-weight: bold;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s, transform 0.1s;
}
#startButton {
background-color: var(--primary-color);
flex-grow: 1;
}
#startButton.active {
background-color: var(--red-color);
}
#clearButton {
background-color: #B0B0B0;
}
button:hover {
transform: translateY(-2px);
}
#startButton:hover:not(.active) {
background-color: var(--dark-blue); /* Darker primary */
}
#startButton.active:hover {
background-color: #D65A5A; /* Darker red */
}
#clearButton:hover {
background-color: #909090;
}
/* Metrics display (timer and RPM) */
.metrics-display {
display: flex;
justify-content: space-between;
margin-bottom: 25px;
gap: 10px;
}
.metric-box {
background-color: var(--secondary-color);
color: #FFFFFF;
padding: 15px 20px;
border-radius: 10px;
flex: 1;
min-width: 0;
}
.metric-box p {
margin: 0 0 5px 0;
font-size: 0.9rem;
font-weight: 500;
}
.metric-value {
font-size: 2.5rem;
font-weight: bold;
line-height: 1;
}
/* Main count display */
.count-display {
background-color: var(--primary-color);
color: #FFFFFF;
padding: 20px;
border-radius: 10px;
margin-bottom: 25px;
}
.count-display p {
margin: 0;
font-size: 1.2rem;
font-weight: 500;
}
#wordCount {
font-size: 4rem;
font-weight: bold;
margin: 10px 0;
line-height: 1;
}
/* Transcript area */
.transcript-container {
text-align: left;
border-top: 1px solid var(--border-color);
padding-top: 20px;
}
.transcript-container h3 {
margin-top: 0;
font-size: 1.2rem;
color: #555;
}
#liveTranscript {
background-color: var(--background-color);
padding: 15px;
border-radius: 8px;
min-height: 50px;
max-height: 150px;
overflow-y: auto;
border: 1px solid var(--border-color);
white-space: pre-wrap;
word-wrap: break-word;
line-height: 1.6;
}
.hidden {
display: none;
}
.message {
margin-top: 20px;
color: var(--red-color);
font-weight: 500;
}
</style>
</head>
<body>
<div class="container">
<h1>Live Fuzzy Word/Phrase Counter</h1>
<p>Counts a specific word (even with small spelling differences) or an exact phrase.</p>
<div class="controls">
<div class="input-group">
<label for="wordToCountInput">Word or Phrase to count:</label>
<input type="text" id="wordToCountInput" placeholder="e.g., 'hello world'" value="okay" autofocus>
</div>
<div class="button-group">
<button id="startButton">Start Listening</button>
<button id="clearButton">Clear</button>
</div>
</div>
<div class="metrics-display">
<div class="metric-box">
<p>Elapsed Time:</p>
<div id="elapsedTimeDisplay" class="metric-value">00:00</div>
</div>
<div class="metric-box">
<p>"<span id="monitoredWordRateDisplay">okay</span>" per Minute:</p>
<div id="wordsPerMinuteDisplay" class="metric-value">0.0</div>
</div>
</div>
<div class="count-display">
<p>Total count for "<span id="displayMonitoredWord">okay</span>":</p>
<div id="totalWordCount">0</div>
</div>
<div class="transcript-container">
<h3>Live Transcript:</h3>
<div id="liveTranscriptOutput"></div>
</div>
<p id="systemMessage" class="hidden"></p>
</div>
<script>
// --- HTML Element References ---
const startStopButton = document.getElementById('startButton');
const resetButton = document.getElementById('clearButton');
const wordInput = document.getElementById('wordToCountInput');
const totalWordCountDisplay = document.getElementById('totalWordCount');
const displayMonitoredWordSpan = document.getElementById('displayMonitoredWord');
const liveTranscriptOutput = document.getElementById('liveTranscriptOutput');
const systemMessageDisplay = document.getElementById('systemMessage');
const elapsedTimeDisplay = document.getElementById('elapsedTimeDisplay');
const wordsPerMinuteDisplay = document.getElementById('wordsPerMinuteDisplay');
const monitoredWordRateDisplay = document.getElementById('monitoredWordRateDisplay');
// --- Application State Variables ---
let speechRecognizer;
let isMicrophoneActive = false;
let currentWordCount = 0;
let wordToMonitor = wordInput.value.toLowerCase().trim();
let sessionStartTime = 0;
let timerIntervalId;
// --- Fuzzy Matching Thresholds ---
// Threshold for individual words (e.g., 'oka' for 'okay')
const FUZZY_MATCH_THRESHOLD_WORD = 2;
// Threshold for individual words WITHIN a phrase (e.g., 'worl' for 'world' in 'hello world')
const FUZZY_MATCH_THRESHOLD_PHRASE_WORD = 3;
// --- Levenshtein Distance Function ---
// Calculates the minimum number of single-character edits (insertions, deletions, substitutions)
// required to change string 'a' into string 'b'.
function calculateLevenshteinDistance(a, b) {
if (a.length === 0) return b.length;
if (b.length === 0) return a.length;
const matrix = [];
// Increment along the first column of each row
for (let i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
// Increment along the first row of each column
for (let j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
const cost = (b.charAt(i - 1) === a.charAt(j - 1)) ? 0 : 1;
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + cost, // substitution
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1 // deletion
);
}
}
return matrix[b.length][a.length];
}
// --- Helper Function: Formats seconds into MM:SS ---
function formatSecondsToMinutesSeconds(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const remainingSeconds = Math.floor(totalSeconds % 60);
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(remainingSeconds).padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
}
// --- Core Function: Updates all real-time metrics (timer, WPM) ---
function updateLiveMetricsDisplay() {
if (!isMicrophoneActive && sessionStartTime === 0) {
elapsedTimeDisplay.textContent = '00:00';
wordsPerMinuteDisplay.textContent = '0.0';
return;
}
const elapsedMilliseconds = Date.now() - sessionStartTime;
const elapsedSeconds = elapsedMilliseconds / 1000;
elapsedTimeDisplay.textContent = formatSecondsToMinutesSeconds(elapsedSeconds);
if (elapsedSeconds > 0) {
const calculatedWPM = (currentWordCount / elapsedSeconds) * 60;
wordsPerMinuteDisplay.textContent = calculatedWPM.toFixed(1);
} else {
wordsPerMinuteDisplay.textContent = '0.0';
}
}
// --- Web Speech API Setup and Event Handlers ---
if (!('webkitSpeechRecognition' in window) && !('SpeechRecognition' in window)) {
systemMessageDisplay.textContent = 'Speech Recognition is not supported in this browser. Please use Chrome or Edge.';
systemMessageDisplay.classList.remove('hidden');
startStopButton.disabled = true;
} else {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
speechRecognizer = new SpeechRecognition();
speechRecognizer.continuous = true;
speechRecognizer.interimResults = false;
speechRecognizer.lang = 'en-US';
// Event handler: When speech is recognized
speechRecognizer.onresult = (event) => {
const lastResult = event.results[event.results.length - 1];
const transcribedTextChunk = lastResult[0].transcript.toLowerCase();
liveTranscriptOutput.textContent += (liveTranscriptOutput.textContent ? ' ' : '') + transcribedTextChunk;
liveTranscriptOutput.scrollTop = liveTranscriptOutput.scrollHeight;
let newOccurrencesFound = 0;
const monitoredTerm = wordToMonitor.toLowerCase();
// --- FUZZY MATCHING LOGIC WITH DISTINCT THRESHOLDS ---
if (monitoredTerm.includes(' ')) {
// PHRASE COUNTING (FUZZY WORD-BY-WORD MATCHING FOR PHRASES)
const targetPhraseWords = monitoredTerm.split(/\s+/).filter(w => w !== '');
const phraseLength = targetPhraseWords.length;
const allTranscriptWords = transcribedTextChunk.split(/\b\W+\b|\s+/).filter(word => word !== '');
// Iterate through the transcript words with a sliding window
for (let i = 0; i <= allTranscriptWords.length - phraseLength; i++) {
let isPhraseFuzzyMatch = true; // Assume true until a word doesn't match
// Compare each word in the current window to the corresponding target phrase word
for (let j = 0; j < phraseLength; j++) {
const currentWordInTranscript = allTranscriptWords[i + j];
const targetWordInPhrase = targetPhraseWords[j];
// Check if word exists AND if its distance is within FUZZY_MATCH_THRESHOLD_PHRASE_WORD
if (!currentWordInTranscript || calculateLevenshteinDistance(currentWordInTranscript, targetWordInPhrase) > FUZZY_MATCH_THRESHOLD_PHRASE_WORD) { // Corrected typo here
isPhraseFuzzyMatch = false;
break;
}
}
if (isPhraseFuzzyMatch) {
newOccurrencesFound++;
}
}
} else {
// SINGLE WORD COUNTING (WITH FUZZY MATCHING)
const wordsInChunk = transcribedTextChunk.split(/\b\W+\b|\s+/).filter(word => word !== '');
for (const word of wordsInChunk) {
const distance = calculateLevenshteinDistance(word, monitoredTerm);
if (distance <= FUZZY_MATCH_THRESHOLD_WORD) {
newOccurrencesFound++;
}
}
}
// --- END FUZZY MATCHING LOGIC ---
if (newOccurrencesFound > 0) {
currentWordCount += newOccurrencesFound;
totalWordCountDisplay.textContent = currentWordCount;
}
updateLiveMetricsDisplay();
};
// Event handler: When speech recognition service stops
speechRecognizer.onend = () => {
if (isMicrophoneActive) {
speechRecognizer.start();
} else {
startStopButton.textContent = 'Start Listening';
startStopButton.classList.remove('active');
updateLiveMetricsDisplay();
}
};
// Event handler: When an error occurs in speech recognition
speechRecognizer.onerror = (event) => {
isMicrophoneActive = false;
startStopButton.textContent = 'Start Listening';
startStopButton.classList.remove('active');
if (event.error === 'not-allowed') {
systemMessageDisplay.textContent = 'Microphone access was denied. Please allow access and try again.';
} else {
systemMessageDisplay.textContent = `An error occurred: ${event.error}`;
}
systemMessageDisplay.classList.remove('hidden');
clearInterval(timerIntervalId);
updateLiveMetricsDisplay();
};
// --- Button Functionality ---
startStopButton.onclick = () => {
if (isMicrophoneActive) {
speechRecognizer.stop();
isMicrophoneActive = false;
clearInterval(timerIntervalId);
updateLiveMetricsDisplay();
} else {
wordToMonitor = wordInput.value.toLowerCase().trim();
if (!wordToMonitor) {
alert("Please enter a word or phrase to count.");
return;
}
displayMonitoredWordSpan.textContent = wordToMonitor;
monitoredWordRateDisplay.textContent = wordToMonitor;
currentWordCount = 0;
totalWordCountDisplay.textContent = currentWordCount;
liveTranscriptOutput.textContent = '';
sessionStartTime = Date.now();
timerIntervalId = setInterval(updateLiveMetricsDisplay, 1000);
speechRecognizer.start();
isMicrophoneActive = true;
startStopButton.textContent = 'Stop Listening';
startStopButton.classList.add('active');
systemMessageDisplay.classList.add('hidden');
}
};
resetButton.onclick = () => {
if (isMicrophoneActive) {
speechRecognizer.stop();
isMicrophoneActive = false;
}
clearInterval(timerIntervalId);
currentWordCount = 0;
sessionStartTime = 0;
totalWordCountDisplay.textContent = currentWordCount;
liveTranscriptOutput.textContent = '';
startStopButton.textContent = 'Start Listening';
startStopButton.classList.remove('active');
systemMessageDisplay.classList.add('hidden');
updateLiveMetricsDisplay();
};
// Update display words when input changes
wordInput.addEventListener('input', () => {
displayMonitoredWordSpan.textContent = wordInput.value || 'word';
monitoredWordRateDisplay.textContent = wordInput.value || 'word';
});
// Initial call to set displays when the page loads
updateLiveMetricsDisplay();
}
</script>
</body>
</html>