Dijeli putem


SpeechRecognitionEngine.EmulateRecognizeAsync Method

Definition

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

Overloads

EmulateRecognizeAsync(String)

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

EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)

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

EmulateRecognizeAsync(String, CompareOptions)

Emulates input of a phrase to the speech recognizer, using text in place of audio for asynchronous 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. When the recognizer completes the asynchronous recognition operation, it raises the EmulateRecognizeCompleted event. 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 synchronous recognition, use the EmulateRecognize method.

EmulateRecognizeAsync(String)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

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

public:
 void EmulateRecognizeAsync(System::String ^ inputText);
public void EmulateRecognizeAsync (string inputText);
member this.EmulateRecognizeAsync : string -> unit
Public Sub EmulateRecognizeAsync (inputText As String)

Parameters

inputText
String

The input for the recognition operation.

Exceptions

The recognizer has no speech recognition grammars loaded, or the recognizer has an asynchronous recognition operation that is not yet complete.

inputText is null.

inputText is the empty string ("").

Examples

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

TestRecognizeAsync("Smith")...  
 SpeechDetected event raised.  
 SpeechRecognized event raised.  
  Grammar = Smith; Text = Smith  
 EmulateRecognizeCompleted event raised.  
  Grammar = Smith; Text = Smith  
 Done.  

TestRecognizeAsync("Jones")...  
 SpeechDetected event raised.  
 SpeechRecognized event raised.  
  Grammar = Jones; Text = Jones  
 EmulateRecognizeCompleted event raised.  
  Grammar = Jones; Text = Jones  
 Done.  

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

TestRecognizeAsync("Mister Smith")...  
 SpeechDetected event raised.  
 SpeechRecognized event raised.  
  Grammar = Smith; Text = mister Smith  
 EmulateRecognizeCompleted event raised.  
  Grammar = Smith; Text = mister Smith  
 Done.  

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

namespace SreEmulateRecognizeAsync  
{  
  class Program  
  {  
    // Indicate when an asynchronous operation is finished.  
    static bool completed;  

    static void Main(string[] args)  
    {  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(new CultureInfo("en-US")))  
      {  
        // Load grammars.  
        recognizer.LoadGrammar(CreateNameGrammar("Smith"));  
        recognizer.LoadGrammar(CreateNameGrammar("Jones"));  

        // Configure the audio input.  
        recognizer.SetInputToNull();  

        // Add event handlers for the events raised by the  
        // EmulateRecognizeAsync 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);  
        recognizer.EmulateRecognizeCompleted +=  
          new EventHandler<EmulateRecognizeCompletedEventArgs>(  
            EmulateRecognizeCompletedHandler);  

        // Start four asynchronous emulated recognition operations.  
        TestRecognizeAsync(recognizer, "Smith");  
        TestRecognizeAsync(recognizer, "Jones");  
        TestRecognizeAsync(recognizer, "Mister");  
        TestRecognizeAsync(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 asynchronous  
    // recognition.  
    private static void TestRecognizeAsync(  
      SpeechRecognitionEngine recognizer, string input)  
    {  
      completed = false;  

      Console.WriteLine("TestRecognizeAsync(\"{0}\")...", input);  
      recognizer.EmulateRecognizeAsync(input);  

      // Wait for the operation to complete.  
      while (!completed)  
      {  
        Thread.Sleep(333);  
      }  

      Console.WriteLine(" Done.");  
      Console.WriteLine();  
    }  

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

    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.");  
      }  
    }  

    // Handle events.  
    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.");  
      }  
    }  

    static void EmulateRecognizeCompletedHandler(  
      object sender, EmulateRecognizeCompletedEventArgs e)  
    {  
      Console.WriteLine(" EmulateRecognizeCompleted event raised.");  

      if (e.Error != null)  
      {  
        Console.WriteLine("  {0} exception encountered: {1}:",  
          e.Error.GetType().Name, e.Error.Message);  
      }  
      else if (e.Cancelled)  
      {  
        Console.WriteLine("  Operation cancelled.");  
      }  
      else 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.");  
      }  

      completed = true;  
    }  
  }  
}

Remarks

The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated. When the recognizer completes the asynchronous recognition operation, it raises the EmulateRecognizeCompleted event.

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.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by EmulateRecognize(String).

See also

Applies to

EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

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

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

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.

Exceptions

The recognizer has no speech recognition grammars loaded, or the recognizer has an asynchronous recognition operation that is not yet complete.

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. When the recognizer completes the asynchronous recognition operation, it raises the EmulateRecognizeCompleted event.

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 recognizers always ignore the character width and never ignore the Kana type. The recognizers also ignore new lines and extra white space and treat punctuation as literal input. For more information about character width and Kana type, see the CompareOptions enumeration.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by EmulateRecognize(RecognizedWordUnit[], CompareOptions).

See also

Applies to

EmulateRecognizeAsync(String, CompareOptions)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

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

public:
 void EmulateRecognizeAsync(System::String ^ inputText, System::Globalization::CompareOptions compareOptions);
public void EmulateRecognizeAsync (string inputText, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognizeAsync : string * System.Globalization.CompareOptions -> unit
Public Sub EmulateRecognizeAsync (inputText As String, compareOptions As CompareOptions)

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.

Exceptions

The recognizer has no speech recognition grammars loaded, or the recognizer has an asynchronous recognition operation that is not yet complete.

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. When the recognizer completes the asynchronous recognition operation, it raises the EmulateRecognizeCompleted event.

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 recognizers always ignore the character width and never ignore the Kana type. The recognizers also ignore new lines and extra white space and treat punctuation as literal input. For more information about character width and Kana type, see the CompareOptions enumeration.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by EmulateRecognize(String, CompareOptions).

See also

Applies to