Add Method (GrammarBuilder, String)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

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

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

Syntax

Visual Basic (Declaration)
Public Shared Function Add ( _
	builder As GrammarBuilder, _
	phrase As String _
) As GrammarBuilder
Visual Basic (Usage)
Dim builder As GrammarBuilder
Dim phrase As String
Dim returnValue As GrammarBuilder

returnValue = GrammarBuilder.Add(builder, phrase)
C#
public static GrammarBuilder Add(
	GrammarBuilder builder,
	string phrase
)

Parameters

builder
Type: Microsoft.Speech.Recognition..::..GrammarBuilder

The first grammar element.

phrase
Type: System..::..String

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

Return Value

Type: Microsoft.Speech.Recognition..::..GrammarBuilder

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

Remarks

GrammarBuilder supports implicit conversions from the following classes:

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

For more information, see the ImplicitWideningImplicitImplicitImplicit and Addition operators.

Important noteImportant

Caution is recommended when combining Choices or GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances with other grammar elements. The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element. For more information about building a speech recognition grammar that contains semantic information, see Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech).

Examples

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.

C# Copy imageCopy Code
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;
}

See Also