SemanticResultKey Constructor (String, GrammarBuilder[])


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

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


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.

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();
  SemanticResultKey wildcardKey= new SemanticResultKey("Password", wildcardBuilder);
  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);
        RecognizedAudio pwdAudio = result.GetAudioForWordRange(
                  result.Words[result.Words.Count - 1]);
                  MemoryStream pwdMemoryStream = new MemoryStream();
        if (!IsValidPwd(pwdMemoryStream)) 
          string badPwd = System.IO.Path.GetTempPath() + "BadPwd" + (new Random()).Next().ToString() + ".wav";
          FileStream waveStream = new FileStream(badPwd, FileMode.Create);  
          SpeechUI.SendTextFeedback(eventArgs.Result, "Invalid Password", false);    

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


.NET Framework
Available since 3.0
Return to top