@@ -58,29 +58,33 @@ <h2>Code for this demo:</h2>
58
58
var $curSentence = $ ( '<span class="interim"> </span>' ) . appendTo ( $output ) ;
59
59
60
60
// a result is approximately equivalent to a sentence, and is the granularity that alternatives are selected on
61
- stream . on ( 'data' , function ( result ) {
62
-
63
- // only final results include word confidence
64
- if ( result . final ) {
65
-
66
- var html = result . alternatives [ 0 ] . word_confidence . map ( function ( pair ) {
67
- // the word_confidence array includes a sub-array for wach word like so: ['word', 0.9]
68
- // the score is a range from 1 (100% confident) to 0 (not at all confident)
69
- // RGB color values go on a scale of 0-255 with 0,0,0 being black and 255,255,255 being white.
70
- // In this case, we want confident words to be 0 (black), and the least confident words to be 200 (light grey)
71
- var shade = 200 - Math . round ( pair [ 1 ] * 200 ) ;
72
- return '<span style="color: rgb(' + shade + ',' + shade + ',' + shade + ')">' + pair [ 0 ] + '</span>' ;
73
- } ) . join ( ' ' ) + ' ' ;
74
-
75
- $curSentence . html ( html ) ;
76
-
77
- $curSentence . removeClass ( 'interim' ) . addClass ( 'final' ) ;
78
- // if we have the final text for that sentence, start a new one
79
- $curSentence = $ ( '<span class="interim"/>' ) . appendTo ( $output ) ;
80
- } else {
81
- // for interim results
82
- $curSentence . html ( result . alternatives [ 0 ] . transcript ) ;
61
+ stream . on ( 'data' , function ( msg ) {
62
+
63
+ if ( msg . results ) {
64
+ msg . results . forEach ( function ( result ) {
65
+ // only final results include word confidence
66
+ if ( result . final ) {
67
+ var html = result . alternatives [ 0 ] . word_confidence . map ( function ( pair ) {
68
+ // the word_confidence array includes a sub-array for wach word like so: ['word', 0.9]
69
+ // the score is a range from 1 (100% confident) to 0 (not at all confident)
70
+ // RGB color values go on a scale of 0-255 with 0,0,0 being black and 255,255,255 being white.
71
+ // In this case, we want confident words to be 0 (black), and the least confident words to be 200 (light grey)
72
+ var shade = 200 - Math . round ( pair [ 1 ] * 200 ) ;
73
+ return '<span style="color: rgb(' + shade + ',' + shade + ',' + shade + ')">' + pair [ 0 ] + '</span>' ;
74
+ } ) . join ( ' ' ) + ' ' ;
75
+
76
+ $curSentence . html ( html ) ;
77
+
78
+ $curSentence . removeClass ( 'interim' ) . addClass ( 'final' ) ;
79
+ // if we have the final text for that sentence, start a new one
80
+ $curSentence = $ ( '<span class="interim"/>' ) . appendTo ( $output ) ;
81
+ } else {
82
+ // for interim results
83
+ $curSentence . html ( result . alternatives [ 0 ] . transcript ) ;
84
+ }
85
+ } ) ;
83
86
}
87
+
84
88
} ) ;
85
89
86
90
document . querySelector ( '#stop' ) . onclick = stream . stop . bind ( stream ) ;
0 commit comments