שתף באמצעות


SpeechRecognizer.EmulateRecognizeAsync Method

Definition

Emulates input to the shared speech recognizer, using text instead of audio for asynchronous speech recognition.

Overloads

EmulateRecognizeAsync(String)

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

EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)

Emulates input of specific words to the shared speech recognizer, using text instead 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 shared speech recognizer, using text instead 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. This can be helpful when you are testing or debugging an application or grammar.

The shared 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

If Windows Speech Recognition is in the Sleeping state, then the shared recognizer does not process input and does not raise the SpeechDetected and related events, but still raises the EmulateRecognizeCompleted event.

Note

The RecognitionResult object generated by the shared 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:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

Emulates input of a phrase to the shared speech recognizer, using text instead 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.

Examples

The following example is part of a console application that loads a speech recognition grammar and demonstrates asynchronous emulated input, the associated recognition results, and the associated events raised by the speech recognizer. If Windows Speech Recognition is not running, then starting this application will also start Windows Speech Recognition. If Windows Speech Recognition is in the Sleeping state, then EmulateRecognizeAsync always returns null.

using System;  
using System.Speech.Recognition;  
using System.Threading;  

namespace SharedRecognizer  
{  
  class Program  
  {  
    static bool completed;  

    static void Main(string[] args)  
    {  
      // Initialize an instance of the shared recognizer.  
      using (SpeechRecognizer recognizer = new SpeechRecognizer())  
      {  
        // Create and load a sample grammar.  
        Grammar testGrammar =  
          new Grammar(new GrammarBuilder("testing testing"));  
        testGrammar.Name = "Test Grammar";  

        recognizer.LoadGrammar(testGrammar);  

        // Attach event handlers for recognition events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  
        recognizer.EmulateRecognizeCompleted +=  
          new EventHandler<EmulateRecognizeCompletedEventArgs>(  
            EmulateRecognizeCompletedHandler);  

        completed = false;  

        // This EmulateRecognizeAsync call generates a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing testing");  

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

        completed = false;  

        // This EmulateRecognizeAsync call does not match the grammar   
        // or generate a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing one two three");  

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

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

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      if (e.Result != null)  
      {  
        Console.WriteLine("Recognition result = {0}",  
          e.Result.Text ?? "<no text>");  
      }  
      else  
      {  
        Console.WriteLine("No recognition result");  
      }  
    }  

    // Handle the EmulateRecognizeCompleted event.   
    static void EmulateRecognizeCompletedHandler(  
      object sender, EmulateRecognizeCompletedEventArgs e)  
    {  
      if (e.Result == null)  
      {  
        Console.WriteLine("No result generated.");  
      }  

      completed = true;  
    }  
  }  
}

Remarks

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:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

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

Remarks

This method creates a RecognitionResult object using the information provided in the wordUnits parameter.

The recognizer uses the 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 treats 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:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs
Source:
SpeechRecognizer.cs

Emulates input of a phrase to the shared speech recognizer, using text instead 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.

Remarks

The recognizer uses the 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 treats 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