aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/speechgrammar/index.html
blob: 41a4d00162be14e056345d1ea12fbcf275996d1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: SpeechGrammar
slug: Web/API/SpeechGrammar
tags:
  - API
  - SpeechGrammar
  - Web Speech API
  - 实验性
  - 接口
  - 识别
  - 语音
  - 语音识别
translation_of: Web/API/SpeechGrammar
---
<p>{{APIRef("Web Speech API")}}{{SeeCompatTable}}</p>

<p><a href="/en-US/docs/Web/API/Web_Speech_API">Web Speech API</a> 的 <strong><code>SpeechGrammar</code></strong> 接口 表示了语音识别对象服务想要识别的一系列词语或模式。</p>

<p>文法通过 <a class="external external-icon" href="http://www.w3.org/TR/jsgf/">JSpeech Grammar Format</a> (<strong>JSGF</strong>.) 来定义,其他格式的文法会在以后支持。</p>

<h2 id="构造函数">构造函数</h2>

<dl>
 <dt>{{domxref("SpeechGrammar.SpeechGrammar()")}}</dt>
 <dd>创建一个新的 <code>SpeechGrammar</code> 对象。</dd>
</dl>

<h2 id="属性">属性</h2>

<dl>
 <dt>{{domxref("SpeechGrammar.src")}}</dt>
 <dd>设置或返回 <code>SpeechGrammar</code> 对象实例中包含文法的字符串。</dd>
 <dt>{{domxref("SpeechGrammar.weight")}} {{optional_inline}}</dt>
 <dd>设置或返回 <code>SpeechGrammar</code> 对象的权重。</dd>
</dl>

<h2 id="示例">示例</h2>

<pre class="brush: js">var grammar = '#JSGF V1.0; grammar colors; public &lt;color&gt; = 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 - 与上面第四行所设置的权重一致
</pre>

<h2 id="规格">规格</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规格</th>
   <th scope="col">状态</th>
   <th scope="col">描述</th>
  </tr>
  <tr>
   <td>{{SpecName('Web Speech API', '#speechreco-speechgrammar', 'SpeechGrammar')}}</td>
   <td>{{Spec2('Web Speech API')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat("api.SpeechGrammar")}}

<h2 id="相关链接">相关链接</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/Web_Speech_API">Web Speech API</a></li>
</ul>