Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
GrammarBuilder Class

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

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)
Visual Basic (Declaration)
Public Class GrammarBuilder
Visual Basic (Usage)
Dim instance As GrammarBuilder
C#
public class GrammarBuilder
Visual C++
public ref class GrammarBuilder
JScript
public class GrammarBuilder

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 System.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 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 Grammar..::.Grammar(GrammarBuilder).

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

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(array<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 t 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,System.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;
}
System..::.Object
  System.Speech.Recognition..::.GrammarBuilder
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker