File tree Expand file tree Collapse file tree 2 files changed +2906
-0
lines changed Expand file tree Collapse file tree 2 files changed +2906
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments