【发布时间】:2025-12-22 20:20:07
【问题描述】:
我想使用 Web Speech API 创建一个简单的语音识别。为了更准确,我想使用SpeechGrammarList()constructor。
注意:有很多过时的答案说 chrome 尚不支持语法,但基于此参考,我认为它现在已完全支持!:
我仍然不确定 chrome 现在是否支持它?如果它在 chrome 中工作和支持,如何正确实现语法?
我已经尝试添加语法,但我不知道哪个是更好的实现(即使它不支持但有一天它会,所以我想把它放在我的代码中)
我们可以像这样根据paragraph的单词添加语法列表:
let paragraph = 'i was sent to earth to protect you to do';
let paragraphWords = paragraph.split(' ');
var grammar = '#JSGF V1.0; grammar paragraphWords; public <paragraphWords> = ' + paragraphWords.join(' | ') + ' ;';
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
或者我应该插入整个句子:
let paragraph = 'i was sent to earth to protect you to do';
// let paragraphWords = paragraph.split(' ');
var grammar = '#JSGF V1.0; grammar paragraph ; public <paragraph> = ' + paragraph + ' ;';
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
【问题讨论】: