Skip to content

Commit d00802a

Browse files
committed
fixing word confidence example
1 parent bb96403 commit d00802a

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

examples/static/microphone-word-confidence.html

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,33 @@ <h2>Code for this demo:</h2>
5858
var $curSentence = $('<span class="interim">&nbsp;</span>').appendTo($output);
5959

6060
// 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+
});
8386
}
87+
8488
});
8589

8690
document.querySelector('#stop').onclick = stream.stop.bind(stream);

0 commit comments

Comments
 (0)