Share via


GrammarBuilder Class

Provides an easy-to-use mechanism for constructing complex Grammar objects from simple inputs.

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

Syntax

'Declaration
<DebuggerDisplayAttribute("{DebugSummary}")> _
Public Class GrammarBuilder
[DebuggerDisplayAttribute("{DebugSummary}")] 
public class GrammarBuilder
[DebuggerDisplayAttribute(L"{DebugSummary}")] 
public ref class GrammarBuilder
/** @attribute DebuggerDisplayAttribute("{DebugSummary}") */ 
public class GrammarBuilder
DebuggerDisplayAttribute("{DebugSummary}") 
public class GrammarBuilder

Remarks

The Speech platform offers the GrammarBuilder class as a simple mechanism for constructing Grammar objects programmatically. Unlike the construction of Grammar instances from objects created using the Microsoft.Speech.Recognition.SrgsGrammar namespace, creating Grammar objects through a GrammarBuilder does not require a detailed understanding of the Speech Recognition Grammar Specification (SRGS).

To construct a basic Grammar object from a GrammarBuilder the following steps are required:

1. Create an appropriate GrammarBuilder instance. For more information on creating Grammar objects see GrammarBuilder, Add and op_Addition.

2. Append to the GrammarBuilder objects which support grammar logic such as external reference to external rules, the Speech platform provided dictation grammars, System.String, Choices, SemanticResultKey, SemanticResultValue, and other GrammarBuilder instances. For more information, see Append.

3. Construct a Grammar object from the GrammarBuilder using a Microsoft.Speech.Recognition.Grammar(GrammarBuilder).

To support debugging, the current status of a GrammarBuilder can be written to a string using DebugShowPhrases.

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Recognition.GrammarBuilder

Example

The code sample below constructs a Grammar which recognizes either of two phrases: "Make background [color]" or "Configure background as [color]" using an explicit creation of and implicit conversion to GrammarBuilder instances.

Steps to make example Grammar recognizing “Make background [color]" or "Configure background as [color]":

1. Create a Choices (colorChoice) object containing a set of color names to recognize from; each associated with a particular RGB value.

2. Each name/value pair is encapsulated in an explicitly constructed new instance of SemanticResultValue (choiceResultValue).

3. A new GrammarBuilder object (resultValueBuilder) is constructed from choiceResultValue and then is added to the colorChoice instance by implicitly converting it to a GrammarBuilder instance, using Add(GrammarBuilder[]).

4. Implicitly create one GrammarBuilder object (makeBackgroundBuilder) with the string “Make background”.

5. Implicitly create a GrammarBuilder (choiceBuilder)from a SemanticResultValue instance containing a look up table created from the colorChoice object, and which can be can be referenced from other lookup tables with the string "RGB"

6. Append choiceBuilder to makeBackgroundBuilder using Append(GrammarBuilder).

7. Explicitly construct a second GrammarBuilder object, configureBackgroundBuilder, with the introductory phrases ("Make background" or "Configure background as").

8. Using Append(GrammarBuilder), append to a new instance of SemanticResultKey created from the colorChoiceChoices object to configureBackgroundBuilder to add the color name/value pair lookup table that can be can be referenced with the string "RGB".

9. The constructor used to create the SemanticResultKey object (#ctor(System.String,Microsoft.Speech.Recognition.GrammarBuilder[])) require as its second argument a GrammarBuilder instance. In this case, the instance of GrammarBuilder is obtained through implicit conversion of the Choices instance, colorChoice.

10. A Choices object (bothChoices) is constructed from the two GrammarBuilder instances (configureBackgroundBuilder and makeBackgroundBuilder).

11. The bothChoices instance of Choices is then used to explicitly construct a new instance of a GrammarBuilder (bothBuilder), which is then passed to Grammar(GrammarBuilder) to create an instance of Grammar which will accept either command.

private Grammar CreateGrammarBuilderRGBSemantics2(params int[] info) {
    //Create a set of choices, each a lookup from a color name to RGB
    //Choices constructors do not take SematicResultValue, so cast SematicResultValue to GramarBuilder
    Choices colorChoice = new Choices();
    foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) {
        SemanticResultValue choiceResultValue =
            new SemanticResultValue(colorName, Color.FromName(colorName).ToARGB());
        GrammarBuilder resultValueBuilder = new GrammarBuilder(choiceResultValue);
        colorChoice.Add(resultValueBuilder);
    }
    SemanticResultKey choiceResultKey = new SemanticResultKey("RGB", colorChoice);
    GrammarBuilder choiceBuilder = new GrammarBuilder(choiceResultKey);
    //Create two intermediate grammars with introductory phrase and the color choice
    GrammarBuilder makeBackgroundBuilder = "Make background";
    makeBackgroundBuilder.Append(choiceBuilder);
    GrammarBuilder configureBackgroundBuilder = new GrammarBuilder("Configure background as");
    configureBackgroundBuilder.Append(new SemanticResultKey("RGB", colorChoice));

    //Create the final grammar recognizing either intermediate grammar 
    //By creating a choice from both GrammarBuilders above
    //Then constructing a GrammarBuilder from that choice
    //Then constructing the grammar.
    Choices bothChoices = new Choices(makeBackgroundBuilder, configureBackgroundBuilder);
    GrammarBuilder bothBuilder = new GrammarBuilder(bothChoices);
    Grammar grammar = new Grammar(bothBuilder);
    grammar.Name = "Make Background /Configure background as";
    return grammar;
}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

GrammarBuilder Members
Microsoft.Speech.Recognition Namespace