GrammarBuilder Class

Definition

Provides a mechanism for programmatically building the constraints for a speech recognition grammar.

public ref class GrammarBuilder
public class GrammarBuilder
type GrammarBuilder = class
Public Class GrammarBuilder
Inheritance
GrammarBuilder

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.

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;
}

Remarks

Speech recognition grammars are commonly authored in the XML format defined by the Speech Recognition Grammar Specification (SRGS) Version 1.0. If you are familiar with SRGS but want to generate the grammars programmatically, you can use the System.Speech.Recognition.SrgsGrammar namespace, whose members correspond closely to the elements and attributes defined by SRGS. If you are unfamiliar with SRGS, or you want a lightweight, programmatic approach to authoring grammars with which you can efficiently accomplish many common scenarios; you can use the GrammarBuilder and Choices classes.

Use GrammarBuilder objects to build a hierarchical tree composed of Choices objects that contain alternate phrases, interspersed with preamble and post-amble phrases at each node, and seeded with semantic values that convey meaning back to the application.

To use a GrammarBuilder to create a Grammar object, use the following steps.

  1. Create a GrammarBuilder object.

  2. Append constraints to the GrammarBuilder, such as String objects, Choices, SemanticResultKey, SemanticResultValue, DictationGrammar, and other GrammarBuilder objects that define the constraints for the grammar.

  3. Use one of the Grammar constructors to create a Grammar object from the completed GrammarBuilder grammar.

Authoring with GrammarBuilder is best suited to grammars that have a single rule containing lists, or perhaps lists of lists. To programmatically build grammars that have multiple rules, or that need to make internal rule references, use the classes of the System.Speech.Recognition.SrgsGrammar namespace.

Instances of GrammarBuilder can also be obtained by implicit conversions from certain other classes or by combining a GrammarBuilder with a second object that contains constraints for a grammar.. For more information, see the Implicit and Addition operators and the Add methods.

To add rules to an existing GrammarBuilder, use the Add, Append, AppendDictation, AppendRuleReference, and AppendWildcard methods.

Important

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.

To help with debugging, the DebugShowPhrases property returns the current status of the GrammarBuilder as a string.

Constructors

GrammarBuilder()

Initializes a new, empty instance of the GrammarBuilder class.

GrammarBuilder(Choices)

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

GrammarBuilder(GrammarBuilder, Int32, Int32)

Initializes a new instance of the GrammarBuilder class from a repeated element.

GrammarBuilder(SemanticResultKey)

Initializes a new instance of the GrammarBuilder class from a semantic key.

GrammarBuilder(SemanticResultValue)

Initializes a new instance of the GrammarBuilder class from a semantic value.

GrammarBuilder(String)

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

GrammarBuilder(String, Int32, Int32)

Initializes a new instance of the GrammarBuilder class from the sequence of words in a String and specifies how many times the String can be repeated.

GrammarBuilder(String, SubsetMatchingMode)

Initializes a new instance of the GrammarBuilder class for a subset of a sequence of words.

Properties

Culture

Gets or sets the culture of the speech recognition grammar.

DebugShowPhrases

Gets a string that shows the contents and structure of the grammar contained by the GrammarBuilder.

Methods

Add(Choices, GrammarBuilder)

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

Add(GrammarBuilder, Choices)

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

Add(GrammarBuilder, GrammarBuilder)

Creates a new GrammarBuilder that contains a sequence of two GrammarBuilder objects.

Add(GrammarBuilder, String)

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

Add(String, GrammarBuilder)

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

Append(Choices)

Appends a set of alternatives to the current sequence of grammar elements.

Append(GrammarBuilder)

Appends a grammar element to the current sequence of grammar elements.

Append(GrammarBuilder, Int32, Int32)

Appends a repeated grammar element to the current sequence of grammar elements.

Append(SemanticResultKey)

Appends a semantic key to the current sequence of grammar elements.

Append(SemanticResultValue)

Appends a semantic value to the current sequence of grammar elements.

Append(String)

Appends a phrase to the current sequence of grammar elements.

Append(String, Int32, Int32)

Appends a repeated phrase to the current sequence of grammar elements.

Append(String, SubsetMatchingMode)

Appends an element for a subset of a phrase to the current sequence of grammar elements.

AppendDictation()

Appends the default dictation grammar to the current sequence of grammar elements.

AppendDictation(String)

Appends the specified dictation grammar to the current sequence of grammar elements.

AppendRuleReference(String)

Appends a grammar definition file to the current sequence of grammar elements.

AppendRuleReference(String, String)

Appends the specified rule of a grammar definition file to the current sequence of grammar elements.

AppendWildcard()

Appends a recognition grammar element that matches any input to the current sequence of grammar elements.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Operators

Addition(Choices, GrammarBuilder)

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

Addition(GrammarBuilder, Choices)

Creates a new GrammarBuilder that contains a GrammarBuilder followed by a Choices.

Addition(GrammarBuilder, GrammarBuilder)

Creates a new GrammarBuilder that contains a sequence of two GrammarBuilder objects.

Addition(GrammarBuilder, String)

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

Addition(String, GrammarBuilder)

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

Implicit(Choices to GrammarBuilder)

Converts a Choices object to a GrammarBuilder object.

Implicit(SemanticResultKey to GrammarBuilder)

Converts a SemanticResultKey object to a GrammarBuilder object.

Implicit(SemanticResultValue to GrammarBuilder)

Converts a SemanticResultValue object to a GrammarBuilder object.

Implicit(String to GrammarBuilder)

Converts a string to a GrammarBuilder object.

Applies to

See also