SemanticResultValue Constructor (GrammarBuilder, Object)

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

Initializes a new instance of the SemanticResultValue class and associates a semantic value with a GrammarBuilder object.

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

Syntax

Visual Basic (Declaration)
Public Sub New ( _
	builder As GrammarBuilder, _
	value As Object _
)
Visual Basic (Usage)
Dim builder As GrammarBuilder
Dim value As Object

Dim instance As New SemanticResultValue(builder, _
	value)
C#
public SemanticResultValue(
	GrammarBuilder builder,
	Object value
)

Parameters

builder
Type: Microsoft.Speech.Recognition..::..GrammarBuilder

A grammar component to be used in recognition.

value
Type: System..::..Object

The value managed by SemanticResultValue. Must be of type bool, int, float, or string.

Remarks

If the grammar component specified by GrammarBuilder is used in the logic of recognition, the contents of value will be set in the Semantics property of the RecognizedPhrase object.

In the code fragment below, if the recognition logic constructed with the GrammarBuilder instance (myGb) uses the Choices object (myChoice) to identify input, the value true will be added to the recognized semantics.

 Copy imageCopy Code
myGb.Append(new SemanticResultValue(myChoice, true);

As GrammarBuilder supports implicit conversion for Choices, SemanticResultValue, and SemanticResultKey, this constructor may use those objects as well.

Examples

The example below returns a Grammar for recognizing the command "Set/Change/Alter Foreground/Background … [color list]". SemanticResultValue and SemanticResultKey instances (along with Choices and GrammarBuilder objects) are used to define semantics that can be parsed on recognition. The parsed semantics will determine which color was requested and whether the foreground or background is to be modified.

The use of the SemanticResultValue constructor is highlighted.

 Copy imageCopy Code
private Grammar FgBgColorGrammar() {
    Grammar grammar = null;
    //Allow command to begin with set, alter, change.
    Choices introChoices = new Choices();
    foreach (string introString in new string[] { "Change", "Set", "Alter" }) {        GrammarBuilder introGB = new GrammarBuilder(introString);        introChoices.Add(new SemanticResultValue(introGB,                            String.Format("Command: {0}", introString)));    }         
    GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);
    //Now define the arguments to the command for Foreground or background and color as sementic Values
    Choices fgOrbgChoice = new Choices();
    GrammarBuilder backgroundGB=new GrammarBuilder("background");
    backgroundGB.Append(new SemanticResultValue(true));
    fgOrbgChoice.Add(backgroundGB);
    fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground", false));
    SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgBool", fgOrbgChoice);
    Choices colorChoice = new Choices();
    foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) {
        colorChoice.Add((GrammarBuilder)
                                        (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));
        //Uses implicit conversion of SemanticResultValue to GrammarBuilder    
    }

    //Create GrammarBuilder for CmdArgs to be appended to CmdInto using Semantic keys.
    GrammarBuilder cmdArgs = new GrammarBuilder();
    cmdArgs.Append(new SemanticResultKey("BgOrFgBool", fgOrbgChoice));
    cmdArgs.AppendWildcard();
    cmdArgs.Append(new SemanticResultKey("colorStringList", colorChoice));

    GrammarBuilder cmds = GrammarBuilder.Add(cmdIntro,
                                             new GrammarBuilder(new SemanticResultKey("Cmd Args", cmdArgs)));
    grammar = new Grammar(cmds);
    grammar.Name = "Tree [Set,change,alter] [foreground,background] * color";
    return grammar;
}

See Also