Compartilhar via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

SpeechRecognitionEngine.EmulateRecognize Method (String)

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

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

Syntax

'Declaration
Public Function EmulateRecognize ( _
    inputText As String _
) As RecognitionResult
'Usage
Dim instance As SpeechRecognitionEngine
Dim inputText As String
Dim returnValue As RecognitionResult

returnValue = instance.EmulateRecognize(inputText)
public RecognitionResult EmulateRecognize(
    string inputText
)

Parameters

  • inputText
    Type: System.String
    The text to use as the input phrase.

Return Value

Type: Microsoft.Speech.Recognition.RecognitionResult
The result for the recognition operation, or a null reference (Nothing in Visual Basic) if the operation is not successful.

Exceptions

Exception Condition
InvalidOperationException

The recognizer has no speech recognition grammars loaded.

ArgumentNullException

inputText is a null reference (Nothing in Visual Basic).

ArgumentException

inputText is the empty string ("").

Remarks

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.

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

See Also

Reference

SpeechRecognitionEngine Class

SpeechRecognitionEngine Members

EmulateRecognize Overload

Microsoft.Speech.Recognition Namespace

SpeechRecognized

EmulateRecognize

EmulateRecognizeAsync