Delen via


SpeechRecognitionEngine.Recognize Method

Definition

Starts a synchronous speech recognition operation.

Overloads

Recognize()

Performs a synchronous speech recognition operation.

Recognize(TimeSpan)

Performs a synchronous speech recognition operation with a specified initial silence timeout period.

Remarks

These methods perform a single, synchronous recognition operation. The recognizer performs this operation against its loaded and enabled speech recognition grammars.

During a call to this method, the recognizer can raise the following events:

The recognizer does not raise the RecognizeCompleted event when using one of the Recognize methods.

The Recognize methods return a RecognitionResult object, or null if the operation is not successful or the recognizer is not enabled.

A synchronous recognition operation can fail for the following reasons:

  • Speech is not detected before the timeout intervals expire for the BabbleTimeout or InitialSilenceTimeout properties, or for the initialSilenceTimeout parameter of the Recognize method.

  • The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

To modify how the recognizer handles the timing of speech or silence with respect to recognition, use the BabbleTimeout, InitialSilenceTimeout, EndSilenceTimeout, and EndSilenceTimeoutAmbiguous properties.

The SpeechRecognitionEngine must have at least one Grammar object loaded before performing recognition. To load a speech recognition grammar, use the LoadGrammar or LoadGrammarAsync method.

To perform asynchronous recognition, use one of the RecognizeAsync methods.

Recognize()

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

Performs a synchronous speech recognition operation.

public:
 System::Speech::Recognition::RecognitionResult ^ Recognize();
public System.Speech.Recognition.RecognitionResult Recognize ();
member this.Recognize : unit -> System.Speech.Recognition.RecognitionResult
Public Function Recognize () As RecognitionResult

Returns

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

Examples

The following example shows part of a console application that demonstrates basic speech recognition. The example creates a DictationGrammar, loads it into an in-process speech recognizer, and performs one recognition operation.

using System;  
using System.Speech.Recognition;  

namespace SynchronousRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  
      // Create an in-process speech recognizer for the en-US locale.  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(  
          new System.Globalization.CultureInfo("en-US")))  
      {  

        // Create and load a dictation grammar.  
        recognizer.LoadGrammar(new DictationGrammar());  

        // Configure input to the speech recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  

        // Modify the initial silence time-out value.  
        recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(5);  

        // Start synchronous speech recognition.  
        RecognitionResult result = recognizer.Recognize();  

        if (result != null)  
        {  
          Console.WriteLine("Recognized text = {0}", result.Text);  
        }  
        else  
        {  
          Console.WriteLine("No recognition result available.");  
        }  
      }  

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

Remarks

This method performs a single recognition operation. The recognizer performs this operation against its loaded and enabled speech recognition grammars.

During a call to this method, the recognizer can raise the following events:

The recognizer does not raise the RecognizeCompleted event when using this method.

The Recognize() method returns a RecognitionResult object, or null if the operation is not successful.

A synchronous recognition operation can fail for the following reasons:

  • Speech is not detected before the timeout intervals expire for the BabbleTimeout or InitialSilenceTimeout properties.

  • The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

To perform asynchronous recognition, use one of the RecognizeAsync methods.

See also

Applies to

Recognize(TimeSpan)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

Performs a synchronous speech recognition operation with a specified initial silence timeout period.

public:
 System::Speech::Recognition::RecognitionResult ^ Recognize(TimeSpan initialSilenceTimeout);
public System.Speech.Recognition.RecognitionResult Recognize (TimeSpan initialSilenceTimeout);
member this.Recognize : TimeSpan -> System.Speech.Recognition.RecognitionResult
Public Function Recognize (initialSilenceTimeout As TimeSpan) As RecognitionResult

Parameters

initialSilenceTimeout
TimeSpan

The interval of time a speech recognizer accepts input containing only silence before finalizing recognition.

Returns

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

Examples

The following example shows part of a console application that demonstrates basic speech recognition. The example creates a DictationGrammar, loads it into an in-process speech recognizer, and performs one recognition operation.

using System;  
using System.Speech.Recognition;  

namespace SynchronousRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  
      // Create an in-process speech recognizer for the en-US locale.  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(  
          new System.Globalization.CultureInfo("en-US")))  
      {  
        // Create and load a dictation grammar.  
        recognizer.LoadGrammar(new DictationGrammar());  

        // Configure input to the speech recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  

        // Start synchronous speech recognition.  
        RecognitionResult result = recognizer.Recognize(TimeSpan.FromSeconds(5));  

        if (result != null)  
        {  
          Console.WriteLine("Recognized text = {0}", result.Text);  
        }  
        else  
        {  
          Console.WriteLine("No recognition result available.");  
        }  
      }  

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

Remarks

If the speech recognition engine detects speech within the time interval specified by initialSilenceTimeout argument, Recognize(TimeSpan) performs a single recognition operation and then terminates. The initialSilenceTimeout parameter supersedes the recognizer's InitialSilenceTimeout property.

During a call to this method, the recognizer can raise the following events:

The recognizer does not raise the RecognizeCompleted event when using this method.

The Recognize() method returns a RecognitionResult object, or null if the operation is not successful.

A synchronous recognition operation can fail for the following reasons:

  • Speech is not detected before the timeout intervals expire for the BabbleTimeout or for the initialSilenceTimeout parameter.

  • The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

To perform asynchronous recognition, use one of the RecognizeAsync methods.

See also

Applies to