Partilhar via


SpeechRecognitionEngine.EmulateRecognize Method

Definition

Emulates input to the speech recognizer, using text in place of audio for synchronous speech recognition.

Overloads

EmulateRecognize(String)

Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition.

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

Emulates input of specific words to the speech recognizer, using text 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.

EmulateRecognize(String, CompareOptions)

Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

Remarks

These methods bypass the system audio input and provide text to the recognizer as String objects or as an array of RecognizedWordUnit objects. This can be helpful when you are testing or debugging an application or grammar. For example, you can use emulation to determine whether a word is in a grammar and what semantics are returned when the word is recognized. Use the SetInputToNull method to disable audio input to the speech recognition engine during emulation operations.

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.

Note

The RecognitionResult object generated by the speech recognizer in response to emulated input has a value of null for its Audio property.

To emulate asynchronous recognition, use the EmulateRecognizeAsync method.

EmulateRecognize(String)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition.

public:
 System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(System::String ^ inputText);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (string inputText);
member this.EmulateRecognize : string -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (inputText As String) As RecognitionResult

Parameters

inputText
String

The input for the recognition operation.

Returns

The result for the recognition operation, or null if the operation is not successful or the recognizer is not enabled.

Exceptions

The recognizer has no speech recognition grammars loaded.

inputText is null.

inputText is the empty string ("").

Examples

The code example below is part of a console application that demonstrates emulated input, the associated recognition results, and the associated events raised by the speech recognizer. The example generates the following output.

TestRecognize("Smith")...  
 SpeechDetected event raised.  
 SpeechRecognized event raised.  
  Grammar = Smith; Text = Smith  
...Recognition result text = Smith  

TestRecognize("Jones")...  
 SpeechDetected event raised.  
 SpeechRecognized event raised.  
  Grammar = Jones; Text = Jones  
...Recognition result text = Jones  

TestRecognize("Mister")...  
 SpeechDetected event raised.  
 SpeechHypothesized event raised.  
  Grammar = Smith; Text = mister  
 SpeechRecognitionRejected event raised.  
  Grammar = <not available>; Text =  
...No recognition result.  

TestRecognize("Mister Smith")...  
 SpeechDetected event raised.  
 SpeechRecognized event raised.  
  Grammar = Smith; Text = mister Smith  
...Recognition result text = mister Smith  

press any key to exit...  
using System;  
using System.Globalization;  
using System.Speech.Recognition;  

namespace Sre_EmulateRecognize  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  

      // Create an in-process speech recognizer for the en-US locale.  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(new CultureInfo("en-US")))  
      {  

        // Load grammars.  
        recognizer.LoadGrammar(CreateNameGrammar("Smith"));  
        recognizer.LoadGrammar(CreateNameGrammar("Jones"));  

        // Disable audio input to the recognizer.  
        recognizer.SetInputToNull();  

        // Add handlers for events raised by the EmulateRecognize method.  
        recognizer.SpeechDetected +=  
          new EventHandler<SpeechDetectedEventArgs>(  
            SpeechDetectedHandler);  
        recognizer.SpeechHypothesized +=  
          new EventHandler<SpeechHypothesizedEventArgs>(  
            SpeechHypothesizedHandler);  
        recognizer.SpeechRecognitionRejected +=  
          new EventHandler<SpeechRecognitionRejectedEventArgs>(  
            SpeechRecognitionRejectedHandler);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  

        // Start four synchronous emulated recognition operations.  
        TestRecognize(recognizer, "Smith");  
        TestRecognize(recognizer, "Jones");  
        TestRecognize(recognizer, "Mister");  
        TestRecognize(recognizer, "Mister Smith");  
      }  

      Console.WriteLine("press any key to exit...");  
      Console.ReadKey(true);  
    }  

    // Create a simple name grammar.  
    // Set the grammar name to the surname.  
    private static Grammar CreateNameGrammar(string surname)  
    {  
      GrammarBuilder builder = new GrammarBuilder("mister", 0, 1);  
      builder.Append(surname);  

      Grammar nameGrammar = new Grammar(builder);  
      nameGrammar.Name = surname;  

      return nameGrammar;  
    }  

    // Send emulated input to the recognizer for synchronous recognition.  
    private static void TestRecognize(  
      SpeechRecognitionEngine recognizer, string input)  
    {  
      Console.WriteLine("TestRecognize(\"{0}\")...", input);  
      RecognitionResult result =  
        recognizer.EmulateRecognize(input,CompareOptions.IgnoreCase);  
      if (result != null)  
      {  
        Console.WriteLine("...Recognition result text = {0}",  
          result.Text ?? "<null>");  
      }  
      else  
      {  
        Console.WriteLine("...No recognition result.");  
      }  
      Console.WriteLine();  
    }  

    static void SpeechDetectedHandler(  
      object sender, SpeechDetectedEventArgs e)  
    {  
      Console.WriteLine(" SpeechDetected event raised.");  
    }  

    // Handle events.  
    static void SpeechHypothesizedHandler(  
      object sender, SpeechHypothesizedEventArgs e)  
    {  
      Console.WriteLine(" SpeechHypothesized event raised.");  
      if (e.Result != null)  
      {  
        Console.WriteLine("  Grammar = {0}; Text = {1}",  
          e.Result.Grammar.Name ?? "<none>", e.Result.Text);  
      }  
      else  
      {  
        Console.WriteLine("  No recognition result available.");  
      }  
    }  

    static void SpeechRecognitionRejectedHandler(  
      object sender, SpeechRecognitionRejectedEventArgs e)  
    {  
      Console.WriteLine(" SpeechRecognitionRejected event raised.");  
      if (e.Result != null)  
      {  
        string grammarName;  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name ?? "<none>";  
        }  
        else  
        {  
          grammarName = "<not available>";  
        }  
        Console.WriteLine("  Grammar = {0}; Text = {1}",  
          grammarName, e.Result.Text);  
      }  
      else  
      {  
        Console.WriteLine("  No recognition result available.");  
      }  
    }  

    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine(" SpeechRecognized event raised.");  
      if (e.Result != null)  
      {  
        Console.WriteLine("  Grammar = {0}; Text = {1}",  
          e.Result.Grammar.Name ?? "<none>", e.Result.Text);  
      }  
      else  
      {  
        Console.WriteLine("  No recognition result available.");  
      }  
    }  
  }  
}  

Remarks

The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated.

The recognizers that ship with Vista and Windows 7 ignore case and character width when applying grammar rules to the input phrase. For more information about this type of comparison, see the CompareOptions enumeration values OrdinalIgnoreCase and IgnoreWidth. The recognizers also ignore new lines and extra white space and treat punctuation as literal input.

See also

Applies to

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

Emulates input of specific words to the speech recognizer, using text 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.

public:
 System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(cli::array <System::Speech::Recognition::RecognizedWordUnit ^> ^ wordUnits, System::Globalization::CompareOptions compareOptions);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (System.Speech.Recognition.RecognizedWordUnit[] wordUnits, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognize : System.Speech.Recognition.RecognizedWordUnit[] * System.Globalization.CompareOptions -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (wordUnits As RecognizedWordUnit(), compareOptions As CompareOptions) As RecognitionResult

Parameters

wordUnits
RecognizedWordUnit[]

An array of word units that contains the input for the recognition operation.

compareOptions
CompareOptions

A bitwise combination of the enumeration values that describe the type of comparison to use for the emulated recognition operation.

Returns

The result for the recognition operation, or null if the operation is not successful or the recognizer is not enabled.

Exceptions

The recognizer has no speech recognition grammars loaded.

wordUnits is null.

wordUnits contains one or more null elements.

compareOptions contains the IgnoreNonSpace, IgnoreSymbols, or StringSort flag.

Remarks

The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated.

The recognizer uses compareOptions when it applies grammar rules to the input phrase. The recognizers that ship with Vista and Windows 7 ignore case if the OrdinalIgnoreCase or IgnoreCase value is present. The recognizer always ignores the character width and never ignores the Kana type. The recognizer also ignores new lines and extra white space and treats punctuation as literal input. For more information about character width and Kana type, see the CompareOptions enumeration.

See also

Applies to

EmulateRecognize(String, CompareOptions)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

public:
 System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(System::String ^ inputText, System::Globalization::CompareOptions compareOptions);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (string inputText, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognize : string * System.Globalization.CompareOptions -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (inputText As String, compareOptions As CompareOptions) As RecognitionResult

Parameters

inputText
String

The input phrase for the recognition operation.

compareOptions
CompareOptions

A bitwise combination of the enumeration values that describe the type of comparison to use for the emulated recognition operation.

Returns

The result for the recognition operation, or null if the operation is not successful or the recognizer is not enabled.

Exceptions

The recognizer has no speech recognition grammars loaded.

inputText is null.

inputText is the empty string ("").

compareOptions contains the IgnoreNonSpace, IgnoreSymbols, or StringSort flag.

Remarks

The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated.

The recognizer uses compareOptions when it applies grammar rules to the input phrase. The recognizers that ship with Vista and Windows 7 ignore case if the OrdinalIgnoreCase or IgnoreCase value is present. The recognizer always ignores the character width and never ignores the Kana type. The recognizer also ignores new lines and extra white space and treats punctuation as literal input. For more information about character width and Kana type, see the CompareOptions enumeration.

See also

Applies to