SemanticResultValue Constructor (Object)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the SemanticResultValue class and specifies a semantic value.

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

public SemanticResultValue(
	object value
)

Parameters

value
Type: System.Object

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

A SemanticResultValue returned by this constructor is not associated with any particular grammar element. The association must be made explicit by using the instance of SemanticResultValue in conjunction with GrammarBuilder.

For instance, in the code fragment below, if a Grammar constructed using this GrammarBuilder instance recognizes the word "background", a value of true is set in the recognized phrase semantics.

GrammarBuilder backgroundGB=new GrammarBuilder("background");
backgroundGB.Append(new SemanticResultValue(true));

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);
    introChoices.Add(
                  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(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))) 
  {

    // Use implicit conversion of SemanticResultValue to GrammarBuilder.    
    colorChoice.Add(
                 (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.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;
}

.NET Framework
Available since 3.0
Return to top
Show: