Export (0) Print
Expand All

SemanticResultKey Constructor (String, GrammarBuilder[])

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

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

public SemanticResultKey(
	string semanticResultKey,
	params GrammarBuilder[] builders
)

Parameters

semanticResultKey
Type: System.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
Type: System.Speech.Recognition.GrammarBuilder[]

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

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.

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);

}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft