Skip to content

Commit 6ce27d9

Browse files
committed
Add speech recognition context to the Web Speech API
Explainer for speech recognition context is added in #140
1 parent 6356249 commit 6ce27d9

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
index.html
2+
.DS_Store
3+
.idea/

index.bs

+37-2
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ interface SpeechRecognition : EventTarget {
161161
attribute boolean interimResults;
162162
attribute unsigned long maxAlternatives;
163163
attribute SpeechRecognitionMode mode;
164+
attribute SpeechRecognitionContext context;
164165

165166
// methods to drive the speech interaction
166167
undefined start();
167168
undefined start(MediaStreamTrack audioTrack);
168169
undefined stop();
169170
undefined abort();
171+
undefined updateContext(SpeechRecognitionContext context);
170172
static Promise<boolean> availableOnDevice(DOMString lang);
171173
static Promise<boolean> installOnDevice(DOMString lang);
172174

@@ -191,7 +193,8 @@ enum SpeechRecognitionErrorCode {
191193
"network",
192194
"not-allowed",
193195
"service-not-allowed",
194-
"language-not-supported"
196+
"language-not-supported",
197+
"recognition-context-not-supported"
195198
};
196199

197200
enum SpeechRecognitionMode {
@@ -246,6 +249,28 @@ dictionary SpeechRecognitionEventInit : EventInit {
246249
unsigned long resultIndex = 0;
247250
required SpeechRecognitionResultList results;
248251
};
252+
253+
// The object representing a biasing phrase.
254+
[Exposed=Window]
255+
interface SpeechRecognitionPhrase {
256+
// If the phrase is empty or the boost is outside the range [0, 10], throw a "SyntaxError" DOMException.
257+
constructor(DOMString phrase, optional float boost = 1.0);
258+
attribute DOMString phrase;
259+
attribute float boost;
260+
};
261+
262+
// The object representing a list of biasing phrases.
263+
[Exposed=Window]
264+
interface SpeechRecognitionPhraseList {
265+
readonly attribute unsigned long length;
266+
getter SpeechRecognitionPhrase item(unsigned long index);
267+
};
268+
269+
// The object representing a recognition context collection.
270+
[Exposed=Window]
271+
interface SpeechRecognitionContext {
272+
attribute SpeechRecognitionPhraseList phrases;
273+
};
249274
</xmp>
250275

251276
<h4 id="speechreco-attributes">SpeechRecognition Attributes</h4>
@@ -276,6 +301,10 @@ dictionary SpeechRecognitionEventInit : EventInit {
276301

277302
<dt><dfn attribute for=SpeechRecognition>mode</dfn> attribute</dt>
278303
<dd>An enum to determine where speech recognition takes place. The default value is "ondevice-preferred".</dd>
304+
305+
<dt><dfn attribute for=SpeechRecognition>context</dfn> attribute</dt>
306+
<dd>This attribute will set the speech recognition context for the recognition session to start with.
307+
If the speech recognition model does not support recognition context, a {{SpeechRecognitionErrorEvent}} with the {{recognition-context-not-supported}} error code will be thrown.</dd>
279308
</dl>
280309

281310
<p class=issue>The group has discussed whether WebRTC might be used to specify selection of audio sources and remote recognizers.
@@ -313,12 +342,15 @@ See <a href="https://lists.w3.org/Archives/Public/public-speech-api/2012Sep/0072
313342
The user agent must raise an <a event for=SpeechRecognition>end</a> event once the speech service is no longer connected.
314343
If the abort method is called on an object which is already stopped or aborting (that is, start was never called on it, the <a event for=SpeechRecognition>end</a> or <a event for=SpeechRecognition>error</a> event has fired on it, or abort was previously called on it), the user agent must ignore the call.</dd>
315344

345+
<dt><dfn method for=SpeechRecognition>updateContext({{SpeechRecognitionContext}} context)</dfn> method</dt>
346+
<dd>The updateContext method updates the speech recognition context after the speech recognition session has started. If the recognition session is not active when this method is called, throw an {{InvalidStateError}} and abort.
347+
If the speech recognition model does not support recognition context, throw a {{SpeechRecognitionErrorEvent}} with the {{recognition-context-not-supported}} error code and abort.</dd>
348+
316349
<dt><dfn method for=SpeechRecognition>availableOnDevice({{DOMString}} lang)</dfn> method</dt>
317350
<dd>The availableOnDevice method returns a Promise that resolves to a boolean indicating whether on-device speech recognition is available for a given BCP 47 language tag. [[!BCP47]]</dd>
318351

319352
<dt><dfn method for=SpeechRecognition>installOnDevice({{DOMString}} lang)</dfn> method</dt>
320353
<dd>The installOnDevice method returns a Promise that resolves to a boolean indicating whether the installation of on-device speech recognition for a given BCP 47 language tag initiated successfully. [[!BCP47]]</dd>
321-
322354
</dl>
323355

324356
<p>When the <dfn>start session algorithm</dfn> with <var>requestMicrophonePermission</var> is invoked, the user agent MUST run the following steps:
@@ -421,6 +453,9 @@ For example, some implementations may fire <a event for=SpeechRecognition>audioe
421453

422454
<dt><dfn enum-value for=SpeechRecognitionErrorCode>"language-not-supported"</dfn></dt>
423455
<dd>The language was not supported.</dd>
456+
457+
<dt><dfn enum-value for=SpeechRecognitionErrorCode>"recognition-context-not-supported"</dfn></dt>
458+
<dd>The speech recognition model does not support recognition context.</dd>
424459
</dl>
425460
</dd>
426461

0 commit comments

Comments
 (0)