SpeechGrammar

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

Web Speech APISpeechGrammar 接口 表示了语音识别对象服务想要识别的一系列词语或模式。

文法通过 JSpeech Grammar Format (JSGF.) 来定义,其他格式的文法会在以后支持。

构造函数

SpeechGrammar.SpeechGrammar()

创建一个新的 SpeechGrammar 对象。

属性

SpeechGrammar.src

设置或返回 SpeechGrammar 对象实例中包含文法的字符串。

SpeechGrammar.weight 可选

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

示例

js
var 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 ;";
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;

console.log(speechRecognitionList[0].src); // 应该返回和上面语法变量一样的内容
console.log(speechRecognitionList[0].weight); // 应该返回 1 - 与上面第四行所设置的权重一致

规范

Specification
Web Speech API
# speechreco-speechgrammar

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
SpeechGrammar
Experimental
SpeechGrammar() constructor
ExperimentalNon-standard
src
Experimental
weight
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
Non-standard. Check cross-browser support before using.
See implementation notes.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.

相关链接