Working with grammars in NUI Speech

The following sections discuss details of working with grammars in NUI Speech.

Adding, loading and preloading grammars

A speech recognizer needs one or more grammars to perform recognition. To provide the speech recognizer with a grammar, you add a grammar to its grammar set (SpeechGrammarSet). The speech recognition API gives you the following methods to add a grammar to a speech recognizer’s grammar set. Each method creates a grammar object (SpeechGrammar) from a different type of grammar, and adds it to the speech recognizer’s grammar set.

An application adds one or more grammars to a speech recognizer’s grammar set prior to starting recognition. An instance of a speech recognizer has only one grammar set. The speech recognizer loads its grammar set at the beginning of a recognition operation, unless it has been preloaded. Before a speech recognizer can be used, its grammar set must contain one or more list grammars or SRGS grammars.

If the grammar set contains large or many grammars, the time it takes the speech recognizer to load its grammar set may delay the start of recognition. To prevent a delay, you can preload a grammar set using the PreloadGrammarsAsync method before initiating a recognition operation. This loads all the grammars in a grammar set as a separate operation, and not as part of starting recognition.

Note All loaded grammars in a title must collectively contain a total of 300 unique words or fewer. Additionally, the loaded grammars must collectively contain a total of 500 arcs (transitions between words and phrases) or fewer.

Managing grammars to optimize recognition

After a grammar set is loaded for recognition, your app can manage which grammars are active for recognition operations by setting the Enabled property of individual grammars to true or false. By default, this setting is true on a grammar that has been loaded. It is typically more efficient to load grammars once and to activate and deactivate them, rather than to load and unload grammars for each recognition operation. It takes fewer processor resources and time to set the Enabled property than to load and unload a grammar.

You restrict the number of grammars that are active for recognition to limit the amount of data that the speech recognizer needs to search to find a match for speech input. This improves both performance and accuracy of speech recognition. You can make decisions about which grammars to have active based on the phrases that your app expects in the context of the current recognition operation.

For example, if the current app context is to select the type of a new list to create, then you may want to deactivate another grammar that contains commands to edit an existing list. Recognizing commands to edit an existing list may have no meaning to your app when it needs to know what type of new list to create.

When you make decisions about which grammars to have active for a recognition operation, make sure that you prepare the user to know what they can say for the recognition operation. This increases the likelihood that the user will speak a phrase that can be matched to an active grammar. Your app can prompt the user for what can be spoken by giving examples or listing acceptable responses.

SRGS grammar

The following shows the structure of an SRGS grammar for navigating a menu in a game:

<?xml version= "1.0" encoding="utf-8"?>

<!-- 
  Speech recognition commands for SimpleSpeechRecognition sample.

  Advanced Technology Group (ATG)
  Copyright (C) Microsoft Corporation. All rights reserved.
-->

<grammar root="toplevel"
         tag-format="semantics/1.0-literals"
         version="1.0"
         xml:lang="en-US"
         xmlns="http://www.w3.org/2001/06/grammar"
         xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
                       sapi:alphabet="x-microsoft-ups">
   <rule id="toplevel" scope="public">
      <one-of>
        <item>main menu</item>
        <item>start game</item>
        <item>quick load</item>
        <item>quick save</item>
        <item>pause game</item>
        <item>exit game</item>
      </one-of>
   </rule>
</grammar>  

By convention, SRGS grammar files use the .grxml file extension. For details on the XML schema for SRGS files, see the W3C Speech Recognition Grammar Specification.