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

View in English Always switch to English

SpeechGrammar

Deprecated

Avoid using this feature in new projects. Advancements in speech recognition mean that speech grammars are not needed to achieve low error rates. This feature may be a candidate for removal from web standards or browsers.

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

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

规范

规范
Web Speech API
# speechreco-speechgrammar

浏览器兼容性

参见