Share via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

SemanticResultKey Constructor (String, GrammarBuilder[])

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

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

Syntax

'Declaration
Public Sub New ( _
    semanticResultKey As String, _
    ParamArray builders As GrammarBuilder() _
)
'Usage
Dim semanticResultKey As String
Dim builders As GrammarBuilder()

Dim instance As New SemanticResultKey(semanticResultKey, _
    builders)
public SemanticResultKey(
    string semanticResultKey,
    params GrammarBuilder[] builders
)

Parameters

  • builders
    Type: []
    An array of grammar components that will be associated with a SemanticValue object accessible with the tag defined in semanticResultKey.

Remarks

Because of implicit conversions, the builders argument supports SemanticResultValue, SemanticResultKey, and Choices objects. 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 quick brown fox" 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 a null reference (Nothing in Visual Basic).

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.

Examples

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

}

See Also

Reference

SemanticResultKey Class

SemanticResultKey Members

SemanticResultKey Overload

Microsoft.Speech.Recognition Namespace