GrammarBuilder Constructor (Choices)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

Initializes a new instance of the GrammarBuilder class from a set of alternatives.

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

Syntax

Visual Basic (Declaration)
Public Sub New ( _
	alternateChoices As Choices _
)
Visual Basic (Usage)
Dim alternateChoices As Choices

Dim instance As New GrammarBuilder(alternateChoices)
C#
public GrammarBuilder(
	Choices alternateChoices
)

Parameters

alternateChoices
Type: Microsoft.Speech.Recognition..::..Choices

The set of alternatives.

Remarks

For more information about building a speech recognition grammar that contains alternatives, see Use a Choices Object to Create a GrammarBuilder (Microsoft.Speech).

Examples

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".

The example uses a Choices object to create a list of acceptable values for colorChoice from an array of String objects. A Choices object is analogous to the one-of element in the SRGS specification, and contains a set of alternate phrases, any of which can be recognized when spoken. The example also uses a Choices object to group an array of two GrammarBuilder objects into a pair of alternative phrases that the resultant grammar can recognize. Alternate words or phrases are a component of most grammars, and the Choices object provides this functionality for grammars constructed with GrammarBuilder.

The example finally creates a Grammar object from a GrammarBuilder constructed from a Choices object.

C# Copy imageCopy Code
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");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // 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});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;
}

See Also