Skip to content

Commit 31b9575

Browse files
committed
Finished wesbos#20
1 parent 20efe54 commit 31b9575

File tree

2 files changed

+2906
-0
lines changed

2 files changed

+2906
-0
lines changed

20 - Speech Detection/index-START.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,43 @@
1212
<script>
1313
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1414

15+
const recognition = new SpeechRecognition();
16+
// console.log(recognition);
1517

18+
recognition.interimResults = true;
19+
20+
// create a paragraph
21+
let pg = document.createElement('p');
22+
const words = document.querySelector('.words');
23+
words.appendChild(pg);
24+
25+
recognition.addEventListener('results', e => {
26+
// console.log(e.results);
27+
28+
const transcript = Array.from(e.results)
29+
.map(result => result[0])
30+
.map(result => result.transcript)
31+
.join('');
32+
33+
pg.textContent = transcript;
34+
35+
if (e.results[0].isFinal) {
36+
pg = document.createElement('p');
37+
words.appendChild(pg);
38+
}
39+
if (transcript.includes('cats')) {
40+
console.log('MEOW! 🐅🐆🐈');
41+
}
42+
if (transcript.includes('what\'s the weather')) {
43+
console.log('You asked for the weather! ☁️')
44+
}
45+
46+
console.log(transcript);
47+
});
48+
49+
recognition.addEventListener('end', recognition.start);
50+
51+
recognition.start();
1652
</script>
1753

1854

0 commit comments

Comments
 (0)