此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

SpeechGrammar

已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它,但也许已从相关的 web 标准中移除,也许正准备移除或出于兼容性而保留。请尽量不要使用该特性,并更新现有的代码;参见本页面底部的兼容性表格以指导你作出决定。请注意,该特性随时可能无法正常工作。

非标准: 该特性尚未标准化。我们不建议在生产环境中使用非标准特性,因为它们在浏览器中的支持有限,且可能发生变化或被移除。不过,在没有标准选项的特定情况下,它们可以作为合适的替代方案。

Web Speech APISpeechGrammar 接口表示一组希望识别服务识别的单词或单词模式。

语法使用 JSpeech 语法格式JSGF)定义。未来可能还会支持其他格式。

构造函数

SpeechGrammar()

创建一个新的 SpeechGrammar 对象。

实例属性

SpeechGrammar.src

设置并返回一个包含 SpeechGrammar 对象实例中的语法字符串。

SpeechGrammar.weight 可选

设置并返回 SpeechGrammar 对象的权重。

示例

js
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 中设置的权重相同。

规范

Specification
Web Speech API
# speechreco-speechgrammar

浏览器兼容性

参见