Export (0) Print
Expand All

GrammarBuilder Class

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

Namespace: System.Speech.Recognition
Assembly: System.Speech (in system.speech.dll)

public class GrammarBuilder
public class GrammarBuilder
public class GrammarBuilder
Not applicable.

The offers the GrammarBuilder class offers as simple mechanism for constructing Grammar objects programmatically. Unlike the construction of Grammar instances from objects create using System.Speech.Recognition.SrgsGrammar name space , creating Grammar from GrammarBuilder objects does not requires a detailed understanding of the Speech Recognition Grammar Specification (SRGS) and it XML specification.

The basics of constructing a Grammar from a GrammarBuilder requires:

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

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

  3. Constructing a Grammar from the GrammarBuilder use System.Speech.Recognition.Grammar(GrammarBuilder).

  4. 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 a explicit creation of and implicit conversion to GrammarBuilder instances.

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

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

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

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[]).

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

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

Append choiceBuilder to makeBackgroundBuilder using Append(GrammarBuilder).

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

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

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.

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

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

    //Createthefinalgrammarrecognizingeitherintermediategrammar//BycreatingachoicefrombothGrammarBuildersabove//ThenconstructingaGrammarBuilderfromthatchoice//Thenconstructingthegrammar.ChoicesbothChoices=newChoices(makeBackgroundBuilder,configureBackgroundBuilder);GrammarBuilderbothBuilder=newGrammarBuilder(bothChoices);Grammargrammar=newGrammar(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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft