GrammarBuilder Constructor (String)

GrammarBuilder Constructor (String)


Initializes a new instance of the GrammarBuilder class from a sequence of words.

Namespace:   System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public GrammarBuilder(
	string phrase


Type: System.String

The sequence of words.

The phrase represents an exact spoken phrase that the speech recognition grammar can recognize. For more information about building a speech recognition grammar that contains strings, see 0d87b445-a342-4369-9b3f-91d07fd7e114.

The following example uses GrammarBuilder and Choices objects to construct a grammar that can recognize either of the two phrases, "Make background colorChoice" or "Set background to colorChoice".

After creating a list of acceptable values for colorChoice using a Choices object, the example initializes two GrammarBuilder objects, makePhrase and setPhrase, using a string as an argument.

The example finally creates a Grammar object from a Choices object cast to a GrammarBuilder object.

private Grammar CreateColorGrammar()

  // Create a set of color choices.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");

  // Create a Choices for the two alternative phrases, convert the Choices
  // to a GrammarBuilder, and construct the Grammar object from the result.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;

.NET Framework
Available since 3.0
Return to top
© 2016 Microsoft