GrammarBuilder.Add Method (String, GrammarBuilder)

 

Creates a new GrammarBuilder that contains a phrase followed by a GrammarBuilder object.

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

public static GrammarBuilder Add(
	string phrase,
	GrammarBuilder builder
)

Parameters

phrase
Type: System.String

The first grammar element, which represents a sequence of words.

builder
Type: System.Speech.Recognition.GrammarBuilder

The second grammar element.

Return Value

Type: System.Speech.Recognition.GrammarBuilder

A GrammarBuilder for the sequence of the phrase element followed by the builder element.

GrammarBuilder supports implicit conversions from the following classes:

This method accepts the objects listed above for the builder parameter.

For more information, see the Implicit(Choices to GrammarBuilder) and Addition operators.

The following example creates a speech recognition grammar that can recognize the two phrases, "Make background color" and "Set background to color", where color is selected from a set of colors. Various types are used to build the final grammar, such as String, Choices, and GrammarBuilder objects. The explicit cast operators in the calls to the Add methods are optional.


private Grammar CreateColorGrammar()
{

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

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

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

  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

.NET Framework
Available since 3.0
Return to top
Show: