SemanticResultKey Constructor (String, GrammarBuilder[])

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

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

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

Dim instance As New SemanticResultKey(semanticResultKey, _
	builders)
C#
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: array<Microsoft.Speech.Recognition..::..GrammarBuilder>[]()[][]

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 ImplicitWideningImplicitImplicitImplicit.

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.

C# Copy imageCopy Code
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 nullNothingnullptrunita 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.

C# Copy imageCopy Code
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