Share via


GrammarBuilder Constructor (SemanticResultKey)

Constructs a GrammarBuilder object from a phrase expressed as a SemanticResultKey instance.

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

Syntax

'Declaration
Public Sub New ( _
    key As SemanticResultKey _
)
public GrammarBuilder (
    SemanticResultKey key
)
public:
GrammarBuilder (
    SemanticResultKey^ key
)
public GrammarBuilder (
    SemanticResultKey key
)
public function GrammarBuilder (
    key : SemanticResultKey
)

Parameters

  • key
    Valid instance of SemanticResultKey used to construct a new instance of GrammarBuilder object.

Remarks

The semantics defined by the SemanticResultKey object create a GrammarBuilder with results that can be accessed on recognition as a SemanticValue instance obtained using the string key to index the Semantics property on the returned instance of RecognizedPhrase.

Care should be taken in appending to a GrammarBuilder based on a SemanticResultKey instance.

An exception will be generated during the use of a Grammar generated from a GrammarBuilder with an SemanticResultKey appended its grammar logic will result in more than one instance of SemanticResultKey with the same tag value and at the same level in the recognition semantic tree are created.

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.

The explicit construction of a GrammarBuilder object using a Choices instance is highlighted in the example code below.

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);
    }
    //Construct GrammarBuilder to contain the color lookup table.
    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;
}

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 Class
GrammarBuilder Members
Microsoft.Speech.Recognition Namespace