admin管理员组文章数量:1328032
In examples for the Web Speech API, a grammar is always specified. For example, in MDN's colour change example, the grammar is:
#JSGF V1.0;
grammar colors;
public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;
However, in actually using the API (on Chrome 54.0.2840.71), the result function:
- Sometimes returns strings that do not fit the supplied grammar
- Does not provide the parse tree that describes the speech
What then does the grammar actually do? How can I get either of these behaviours (restricting to the grammar and seeing the parse tree)?
In examples for the Web Speech API, a grammar is always specified. For example, in MDN's colour change example, the grammar is:
#JSGF V1.0;
grammar colors;
public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;
However, in actually using the API (on Chrome 54.0.2840.71), the result function:
- Sometimes returns strings that do not fit the supplied grammar
- Does not provide the parse tree that describes the speech
What then does the grammar actually do? How can I get either of these behaviours (restricting to the grammar and seeing the parse tree)?
Share Improve this question edited Dec 29, 2016 at 7:34 Migwell asked Dec 29, 2016 at 5:24 MigwellMigwell 20.2k24 gold badges106 silver badges180 bronze badges 2- Possible duplicate of Is there a way to use a grammar with the HTML 5 speech input API? – Nikolay Shmyrev Commented Dec 29, 2016 at 8:39
- 2 Grammars are not supported in Chrome – Nikolay Shmyrev Commented Dec 29, 2016 at 8:39
2 Answers
Reset to default 3There's currently an open Chromium issue specifically about this:
https://bugs.chromium/p/chromium/issues/detail?id=799849&q=SpeechRecognition%20grammar&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified
I know this is an old question, but I'm going through a few similar ones to this as it's something I've been trying to figure out myself recently, and I have a solution. The grammar doesn't seem to work, at least not reliably or as expected.
As a solution, I've written a function that goes some way toward solving the issue. Supply it with the event.results
from the SpeecRecognition.onresult
callback, and make sure maxAlternatives
is set to something like 10. Also supply a list of phrases. It will return the first transcription it finds containing one of the phrases, otherwise it just returns the transcript with the highest confidence.
function ExtractTranscript(phrases, results) {
// Loop through the alternatives to check if any of our hot phrases are contained in them.
for (let result in results[0]) {
if (new RegExp(phrases.join("|")).test(results[0][result].transcript)) {
return results[0][result].transcript; // Return them if they are
}
}
return results[0][0].transcript; // Otherwise return the highest confidence
}
There are probably was of improving upon this solution for long transcripts etc, but it works for my situation for short mand like phrases. Hopefully it helps someone else out too.
本文标签: javascriptThe effect of the grammar in the Web Speech APIStack Overflow
版权声明:本文标题:javascript - The effect of the grammar in the Web Speech API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235973a2438087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论