SpeechRecognizer.EmulateRecognize Method (String)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

public RecognitionResult EmulateRecognize(
	string inputText
)

Parameters

inputText
Type: System.String

The input for the recognition operation.

Return Value

Type: System.Speech.Recognition.RecognitionResult

The recognition result for the recognition operation, or null, if the operation is not successful or Windows Speech Recognition is in the Sleeping state.

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.

The following example loads a sample grammar to the shared recognizer and emulates input to the 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 EmulateRecognize always returns null.


using System;
using System.Speech.Recognition;

namespace SharedRecognizer
{
  class Program
  {

    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);

        RecognitionResult result;

        // This EmulateRecognize call matches the grammar and returns a
        // recognition result.
        result = recognizer.EmulateRecognize("testing testing");
        OutputResult(result);

        // This EmulateRecognize call does not match the grammar and 
        // returns null.
        result = recognizer.EmulateRecognize("testing one two three");
        OutputResult(result);
      }

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

    // Output information about a recognition result to the console.
    private static void OutputResult(RecognitionResult result)
    {
      if (result != null)
      {
        Console.WriteLine("Recognition result = {0}",
          result.Text ?? "<no text>");
      }
      else
      {
        Console.WriteLine("No recognition result");
      }
    }
  }
}

.NET Framework
Available since 3.0
Return to top
Show: