Export (0) Print
Expand All

SemanticResultKey Constructor (String, GrammarBuilder[])

Assigns a semantic key to one of more GrammarBuilder objects used to construct a Grammar.

Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)

Parameters

semanticResultKey

System.String object containing the tag to be used access the SemanticValue instance associated with the GrammarBuilder objects specified by the builders argument.

builders

One or more GrammarBuilder objects which will be associated with a SemanticValue object accessible with the tag defined in semanticResultKey.

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

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 quick brown fox" to be recognized.

SemanticResultKey stringTest=new SemanticResultKey("stringTest",
                                                    new GrammarBuilder("the"),
                                                    new GrammarBuilder("quick"),
                                                    new GrammarBuilder("brown"),
                                                    new GrammarBuilder("fox"));

While the semanticResultKey argument determines the key 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 will be 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 produced.

There should be one, and only one, untagged SemanticResultValue instance in the GrammarBuilder objects specified by the builders parameter, as multiple instances of untagged SemanticResultValue would be associated with the SemanticResultKey, each attempting 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 an SemanticResultKey instance.

Instance 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 example below 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);

}

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

Show:
© 2015 Microsoft