Managing loaded grammars to optimize recognition for Windows Phone 8
Collapse the table of content
Expand the table of content

Managing loaded grammars to optimize recognition for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

After a grammar set is loaded for recognition, your app can manage which grammars are active for recognition operations by setting the SpeechGrammarEnabled() property to true or false. The default setting is true. Typically it’s more efficient to load grammars once and then to activate or deactivate them as needed, rather than load and unload grammars for each recognition operation. It takes fewer processor resources and time to set the SpeechGrammarEnabled() 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 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 display a color that the user speaks, then you may not need to have active a grammar that recognizes the names of animals. Recognizing names of animals probably has no meaning to your app when it needs to know which color to display.

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. You can prompt the user for what can be spoken using the SpeechRecognizerUISettingsExampleText() and SpeechRecognizerUISettingsListenText() members of the SpeechRecognizerUISettings enumeration, which you can set using the SpeechRecognizerUISettings() property. See Presenting prompts, confirmations, and disambiguation choices for Windows Phone 8 for more info.

The following example represents an excerpt from an app that asks a user to say the name of a fruit, then a color, then an animal. The example creates and loads grammars that will be needed for this part of the app. Prior to each recognition operation, the example selectively enables and disables grammars such that, for example, the name of an animal won’t be recognized when the app is expecting the name of a fruit. Then the app prompts the user to speak, and lets the user know what it expects to hear before starting recognition.

private async void EnableDisable_Click(object sender, RoutedEventArgs e)
  // Initialize a SpeechRecognizerUI object.
  SpeechRecognizerUI recoWithUI = new SpeechRecognizerUI();

  // Create string arrays to use in grammars.
  string[] fruit = { "apple", "peach", "banana" };
  string[] colors = { "red", "green", "blue" };
  string[] animals = { "dog", "cat", "fish" };

  //Create list grammars from the string arrays and add them to the grammar set.
  //Grammars are enabled by default.
  recoWithUI.Recognizer.Grammars.AddGrammarFromList("fruitList", fruit);
  recoWithUI.Recognizer.Grammars.AddGrammarFromList("colorList", colors);
  recoWithUI.Recognizer.Grammars.AddGrammarFromList("animalList", animals);

  // RECOGNIZE FRUIT: Disable grammars for colors and animals. Only the fruit grammar is enabled.
  recoWithUI.Recognizer.Grammars["colorList"].Enabled = false;
  recoWithUI.Recognizer.Grammars["animalList"].Enabled = false;

  // Prompt the user to say a fruit.
  recoWithUI.Settings.ListenText = "Say a fruit";
  recoWithUI.Settings.ExampleText = " 'apple', 'peach', 'banana' ";

  // Load the grammar set and start recognition for fruits.
  SpeechRecognitionUIResult recoFruit = await recoWithUI.RecognizeWithUIAsync();

  // RECOGNIZE COLORS: Disable the fruit grammar, enable the color grammar. 
  // Only the color grammar is enabled.
  recoWithUI.Recognizer.Grammars["fruitList"].Enabled = false;
  recoWithUI.Recognizer.Grammars["colorList"].Enabled = true;

  // Prompt the user to say a color.
  recoWithUI.Settings.ListenText = "Say a color";
  recoWithUI.Settings.ExampleText = " 'red', 'green', 'blue' ";

  // Start recognition for colors.
  SpeechRecognitionUIResult recoColors = await recoWithUI.RecognizeWithUIAsync();

  // RECOGNIZE ANIMALS: Disable the color grammar, enable the animals grammar. 
  // Only the animals grammar is enabled.
  recoWithUI.Recognizer.Grammars["colorList"].Enabled = false;
  recoWithUI.Recognizer.Grammars["animalList"].Enabled = true;

  // Prompt the user to say an animal.
  recoWithUI.Settings.ListenText = "Say an animal";
  recoWithUI.Settings.ExampleText = " 'cat', 'dog', 'fish' ";

  // Start recognition for animals.
  SpeechRecognitionUIResult recoAnimals = await recoWithUI.RecognizeWithUIAsync();

© 2017 Microsoft