Share via


SemanticResultKey Constructors

Definition

Constructs an instance of SemanticResultKey and associates the key with grammar components.

Overloads

SemanticResultKey(String, GrammarBuilder[])

Assigns a semantic key to one or more GrammarBuilder objects used to create a speech recognition grammar.

SemanticResultKey(String, String[])

Assigns a semantic key to one or more String instances used to create a speech recognition grammar.

Remarks

The constructors for SemanticResultKey specify a text tag (the semantic key) and a set of grammar components to add to a speech recognition grammar.

The grammar components can be specified either as an array of GrammarBuilder objects, or as an array of String instances.

If the grammar components are used in recognition, you can access the returned SemanticValue using the text tag provided to the constructor of SemanticResultKey as a semantic key. The Value property of the SemanticValue instance will be determined by the grammar components used in the definition of SemanticResultKey.

SemanticResultKey(String, GrammarBuilder[])

Source:
SemanticResultKey.cs
Source:
SemanticResultKey.cs

Assigns a semantic key to one or more GrammarBuilder objects used to create a speech recognition grammar.

public:
 SemanticResultKey(System::String ^ semanticResultKey, ... cli::array <System::Speech::Recognition::GrammarBuilder ^> ^ builders);
public SemanticResultKey (string semanticResultKey, params System.Speech.Recognition.GrammarBuilder[] builders);
new System.Speech.Recognition.SemanticResultKey : string * System.Speech.Recognition.GrammarBuilder[] -> System.Speech.Recognition.SemanticResultKey
Public Sub New (semanticResultKey As String, ParamArray builders As GrammarBuilder())

Parameters

semanticResultKey
String

The tag to be used as a semantic key to access the SemanticValue instance associated with the GrammarBuilder objects specified by the builders argument.

builders
GrammarBuilder[]

An array of grammar components that will be associated with a SemanticValue object accessible with the tag defined in semanticResultKey.

Examples

The following example creates a Grammar to recognize password input of the form "My password is …", where the actual input is matched with a wildcard.

The wildcard is tagged by a SpeechRecognizer whose key value is "Password". The SpeechRecognized handler checks for the presence of this tag, obtains the audio input of the password, and verifies the password.

private void pwdGrammar()
{
  GrammarBuilder pwdBuilder = new GrammarBuilder("My Password is");
  GrammarBuilder wildcardBuilder = new GrammarBuilder();
  wildcardBuilder.AppendWildcard();
  SemanticResultKey wildcardKey= new SemanticResultKey("Password", wildcardBuilder);
  pwdBuilder+=wildcardKey;
  Grammar grammar = new Grammar(pwdBuilder);
  grammar.Name = "Password input";

  grammar.SpeechRecognized +=
    delegate(object sender, SpeechRecognizedEventArgs eventArgs)
    {
      SemanticValue semantics = eventArgs.Result.Semantics;
      RecognitionResult result=eventArgs.Result;

      if (!semantics.ContainsKey("Password"))
      {
        SpeechUI.SendTextFeedback(eventArgs.Result, "No Password Provided", false);
      }
      else
      {
        RecognizedAudio pwdAudio = result.GetAudioForWordRange(
                  result.Words[3],
                  result.Words[result.Words.Count - 1]);
                  MemoryStream pwdMemoryStream = new MemoryStream();
                  pwdAudio.WriteToAudioStream(pwdMemoryStream);
        if (!IsValidPwd(pwdMemoryStream))
        {
          string badPwd = System.IO.Path.GetTempPath() + "BadPwd" + (new Random()).Next().ToString() + ".wav";
          FileStream waveStream = new FileStream(badPwd, FileMode.Create);
          pwdAudio.WriteToWaveStream(waveStream);
          waveStream.Flush();
          waveStream.Close();
          SpeechUI.SendTextFeedback(eventArgs.Result, "Invalid Password", false);
        }
      }
    };

  grammar.Enabled = true;
  _recognizer.LoadGrammar(grammar);
  UpdateGrammarTree(_grammarTreeView, _recognizer);

}

Remarks

Because of implicit conversions, the builders argument supports SemanticResultValue, SemanticResultKey, Choices, and String objects as well. For more information on implicit conversions, see Implicit.

When performing a recognition operation, the GrammarBuilder objects provided in the builders argument are treated as sequential. For example, if the following SemanticResultValue is used to construct a Grammar, input to the recognition engine must contain the words "the quick brown fox" in sequence to be recognized.

SemanticResultKey stringTest=new SemanticResultKey(
    "stringTest", new GrammarBuilder[] {
    new GrammarBuilder("the"),
    new GrammarBuilder("quick"),
    new GrammarBuilder("brown"),
    new GrammarBuilder("fox")});

The semanticResultKey argument contains the tag used to access the SemanticValue which might be returned.

The Value of the SemanticValue is determined by the GrammarBuilder instances provided by the builders parameter.

If the GrammarBuilder objects contain no defining instances of SemanticResultValue, the value of the SemanticValue is null.

If the GrammarBuilder objects provided in the builders parameter provide an untagged (not associated with a SemanticResultKey object) SemanticResultValue instance that is used by the recognition logic, that instance of SemanticResultValue will define the Value property of the SemanticValue that is produced.

There should be one, and only one, untagged SemanticResultValue instance in the GrammarBuilder objects specified by the builders parameter. If multiple instances of untagged SemanticResultValue are associated with the SemanticResultKey, each will attempt to the set the value of the SemanticValue produced in the recognition result. This is not permitted, and the recognizer will generate an exception when it attempts to use a Grammar created using such a SemanticResultKey instance.

Instances of SemanticResultValue contained in the GrammarBuilder objects specified by the builders parameter and already associated with another SemanticResultKey have no effect on the current SemanticResultKey instance.

Applies to

SemanticResultKey(String, String[])

Source:
SemanticResultKey.cs
Source:
SemanticResultKey.cs

Assigns a semantic key to one or more String instances used to create a speech recognition grammar.

public:
 SemanticResultKey(System::String ^ semanticResultKey, ... cli::array <System::String ^> ^ phrases);
public SemanticResultKey (string semanticResultKey, params string[] phrases);
new System.Speech.Recognition.SemanticResultKey : string * string[] -> System.Speech.Recognition.SemanticResultKey
Public Sub New (semanticResultKey As String, ParamArray phrases As String())

Parameters

semanticResultKey
String

The tag to be used access the SemanticValue instance associated with the String objects specified by the phrases argument.

phrases
String[]

One or more String objects, whose concatenated text will be associated with a SemanticValue object accessible with the tag defined in semanticResultKey.

Examples

The following example creates a Grammar from a GrammarBuilder object that uses a SemanticResultKey, which is defined by an array of String objects.

A recognition engine using the Grammar created will recognize the phrase "color red green blue zero". The semantics of the RecognizedPhrase returned by recognition will contain a SemanticValue with a Value of "red green blue". You can access the SemanticValue using the "code" tag.

Because of the SemanticResultValue("zero", 5) appended to the GrammarBuilder, the root SemanticValue object in the RecognizedPhrase will have a value of 5.

private void keyTest()
{
  // Say "color red green blue zero"
  GrammarBuilder gb = new GrammarBuilder("color") +
                        new SemanticResultKey("code",
                          (new string[] {"red", "green", "blue"})) +
                        new SemanticResultValue("zero", 5);
  Grammar g = new Grammar(gb);
  g.Name = "keyTest";
  _recognizer.LoadGrammar(g);
}

Remarks

When performing a recognition operation, the String objects used in the phrases parameter are treated as sequential. For example, if the following SemanticResultValue is used to construct a Grammar, input to the recognition engine must contain the words "the quick brown fox" in sequence to be recognized.

SemanticResultKey stringTest=new SemanticResultKey("stringTest",
                                new string[] {
                                               "the",
                                               "quick",
                                               "brown",
                                               "fox"});

The semanticResultKey argument determines the key used to access the SemanticValue which might be returned.

If you construct a Grammar using a GrammarBuilder object that contains a semantic key with an array of string objects, the Value of the SemanticValue produced by a recognition operation will be the string used in recognition. In the preceding example, this means that Value would be "the quick brown fox".

Applies to