Web Speech API
Web Speech API 的 SpeechGrammar 接口表示一组希望识别服务识别的单词或单词模式。
语法使用 JSpeech 语法格式(JSGF)定义。未来可能还会支持其他格式。
构造函数
- SpeechGrammar.SpeechGrammar()
- : 创建一个新的
SpeechGrammar对象。
- : 创建一个新的
实例属性
- SpeechGrammar.src
- : 设置并返回一个包含
SpeechGrammar对象实例中的语法字符串。
- : 设置并返回一个包含
- SpeechGrammar.weight
- : 设置并返回
SpeechGrammar对象的权重。
- : 设置并返回
示例
const grammar =
"#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 ;";
const recognition = new SpeechRecognition();
const speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
console.log(speechRecognitionList[0].src); // 应返回与 grammar 变量的内容相同的结果。
console.log(speechRecognitionList[0].weight); // 应返回 1 - 与 addFromString 中设置的权重相同。
规范
浏览器兼容性
参见
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
