blob: 3a56e9edad917eec3d9bad11f1b51120b041f453 (
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
---
title: SpeechGrammar.weight
slug: Web/API/SpeechGrammar/weight
tags:
- API
- SpeechGrammar
- Web Speech API
- 实验性
- 属性
- 引用
- 权重
- 识别
- 语音识别
translation_of: Web/API/SpeechGrammar/weight
---
<p>{{APIRef("Web Speech API")}}{{SeeCompatTable}}</p>
<p>{{domxref("SpeechGrammar")}} 接口的可选属性 <strong><code>weight</code></strong> 设置并返回了一个 <code>SpeechGrammar</code> 对象的权重。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">var myGrammarWeight = speechGrammarInstance.weight;</pre>
<h3 id="值">值</h3>
<p>浮点数表示了当前文法的权重,范围在 0.0-1.0 之间。</p>
<h2 id="样例">样例</h2>
<pre class="brush: 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 - 与上面第四行所设置的权重一致
</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', '#dfn-grammarWeight', 'weight')}}</td>
<td>{{Spec2('Web Speech API')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome(33)}} <sup>[1]</sup></td>
<td>{{CompatGeckoDesktop(44)}} <sup>[2]</sup></td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome</th>
<th>Firefox Mobile (Gecko)</th>
<th>Firefox OS</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
<td>{{CompatUnknown}}</td>
<td>2.5</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<ul>
<li>[1] 语音识别接口目前在 Chrome 中是带前缀的,因此你需要使用带有前缀的名称,例如 <code>webkitSpeechRecognition</code>;此外你还需要将你的代码运行在一个 Web 服务器上来保证其正常工作。</li>
<li>[2] 虽然当前的语音识别在桌面版的 Firefox 中不会正常工作,但还是可以在 <a>about:config</a> 中,可以通过 <code>media.webspeech.recognition.enable</code> 来启用 —— 一旦所需的内部权限解决后,它很快就会启用。</li>
</ul>
<h3 id="Firefox_OS_权限">Firefox OS 权限</h3>
<p>如需在应用中使用语音识别,你必须在你的 <a href="/en-US/docs/Web/Apps/Build/Manifest">manifest</a> 中指明下面的权限:</p>
<pre class="brush: json">"permissions": {
"audio-capture" : {
"description" : "Audio capture"
},
"speech-recognition" : {
"description" : "Speech recognition"
}
}</pre>
<p>若还需要设置应用的特权类型,因此你还需要包含以下这项:</p>
<pre class="brush: json"> "type": "privileged"</pre>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Web_Speech_API">Web Speech API</a></li>
</ul>
|