SemanticResultValue Constructor (GrammarBuilder, Object)


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

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

public SemanticResultValue(
	GrammarBuilder builder,
	object value


Type: System.Speech.Recognition.GrammarBuilder

A grammar component to be used in recognition.

Type: System.Object

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

If the grammar element specified by GrammarBuilder is used in the recognition logic, value will be set in the semantics of the recognized output.

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 is added to the recognized semantics.

myGb.Append(new SemanticResultValue(myChoice, true);

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

The following example returns a Grammar that recognizes the command "Set/Change/Alter Foreground/Background … [color list]". SemanticResultValue and SemanticResultKey instances (in conjunction 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.

private Grammar FgBgColorGrammar() 
  Grammar grammar = null;

  // Allow the 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);
                  new SemanticResultValue(introGB,
                  String.Format("Command: {0}", introString)));
  GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);

  // Define the arguments for the command to select foreground or background 
  // and to change their color as semantic values.
  Choices fgOrbgChoice = new Choices();
  GrammarBuilder backgroundGB=new GrammarBuilder("background");
  backgroundGB.Append(new SemanticResultValue(true));
  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))) 

    // Use implicit conversion of SemanticResultValue to GrammarBuilder.    
              (GrammarBuilder) (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));

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

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

.NET Framework
Available since 3.0
Return to top