Bagikan melalui


Note

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

SpeechRecognitionEngine.EmulateRecognize Method (RecognizedWordUnit[], CompareOptions)

Emulates input of specific words to the speech recognizer, using an array of RecognizedWordUnit objects in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

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

Syntax

'Declaration
Public Function EmulateRecognize ( _
    wordUnits As RecognizedWordUnit(), _
    compareOptions As CompareOptions _
) As RecognitionResult
'Usage
Dim instance As SpeechRecognitionEngine
Dim wordUnits As RecognizedWordUnit()
Dim compareOptions As CompareOptions
Dim returnValue As RecognitionResult

returnValue = instance.EmulateRecognize(wordUnits, _
    compareOptions)
public RecognitionResult EmulateRecognize(
    RecognizedWordUnit[] wordUnits,
    CompareOptions compareOptions
)

Parameters

  • wordUnits
    Type: []
    An array of word units that contains the input for the recognition operation.
  • compareOptions
    Type: System.Globalization.CompareOptions
    A bitwise combination of the enumeration values that describe the type of comparison to use for the emulated recognition operation.

Return Value

Type: Microsoft.Speech.Recognition.RecognitionResult
The result for the recognition operation, or a null reference (Nothing in Visual Basic) if the operation is not successful.

Exceptions

Exception Condition
InvalidOperationException

The recognizer has no speech recognition grammars loaded.

ArgumentNullException

wordUnits is a null reference (Nothing in Visual Basic).

ArgumentException

wordUnits contains one or more a null reference (Nothing in Visual Basic) elements.

NotSupportedException

compareOptions contains the IgnoreNonSpace, IgnoreSymbols, or StringSort flag. See CompareOptions.

Remarks

The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated. The recognizer ignores new lines and extra white space and treats punctuation as literal input.

The only supported values of the compareOptions argument are the OrdinalIgnoreCase and IgnoreCase members of the CompareOptions enumerated type. All other members will generate a NotSupportedException exception or return a a null reference (Nothing in Visual Basic).

Examples

In the example below, an array of RecognizedWordUnit objects is obtained from a recognized candidate and tested against the other grammars the recognition engine supports.

private void checkOtherGrammars(RecognizedPhrase candidate) 
{
  string msg = "";
  if (candidate != null && candidate.Grammar != null) 
  {
    //Unload successful grammar and try again, see if other grammars 
    Collection<Grammar> unloadList= new Collection<Grammar>(); //keep track of unloaded grammars and reload.
    _recognitionEngine.UnloadGrammar(candidate.Grammar);
    unloadList.Add(candidate.Grammar);
    RecognizedWordUnit[] words = new RecognizedWordUnit[candidate.Words.Count];
    candidate.Words.CopyTo(words, 0);

    //Iterate over remaining grammars
    msg = String.Format("Candidate \"{0}\"\nSelected by Grammar: \"{1}\"\n\n",
        candidate.Text, candidate.Grammar);
    while (_recognitionEngine.Grammars.Count != 0) 
    {
      RecognitionResult emResult = _recognitionEngine.EmulateRecognize(
                words, System.Globalization.CompareOptions.OrdinalIgnoreCase);
      if (emResult == null) 
      {
        break;
      } 
      else 
      {
        msg += String.Format("Emulation returns \"{0}\"\nSelected by Grammar: \"{1}\" \n",
               emResult.Text, emResult.Grammar.Name);
        _recognitionEngine.UnloadGrammar(emResult.Grammar);
        unloadList.Add(emResult.Grammar);
      }
    }

    MessageBox.Show(msg);
    // restore grammars
    for (int i=0;i<unloadList.Count;i++)
    {
      _recognitionEngine.LoadGrammar(unloadList[i]);
    }
  }
}

See Also

Reference

SpeechRecognitionEngine Class

SpeechRecognitionEngine Members

EmulateRecognize Overload

Microsoft.Speech.Recognition Namespace

SpeechRecognized

EmulateRecognize

EmulateRecognizeAsync

RecognizedWordUnit

CompareOptions