SpeechRecognitionEngine.EmulateRecognize Method

Definition

使用文本代替同步语音识别的音频来模拟输入到语音识别器。Emulates input to the speech recognizer, using text in place of audio for synchronous speech recognition.

Overloads

EmulateRecognize(String)

使用文本代替同步语音识别的音频来模拟输入短语到语音识别器。Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition.

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

使用文本代替同步语音识别的音频来模拟特定字输入到语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。Emulates input of specific words to the speech recognizer, using text in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

EmulateRecognize(String, CompareOptions)

使用文本代替异步语音识别的音频来模拟短语输入到语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

Remarks

这些方法绕过系统音频输入,并将文本提供给识别器作为 String 对象或 RecognizedWordUnit 对象的数组。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. 使用 SetInputToNull 方法可在仿真操作期间禁用语音识别引擎的音频输入。Use the SetInputToNull method to disable audio input to the speech recognition engine during emulation operations.

语音识别器引发 SpeechDetectedSpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像未模拟识别操作一样。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.

Note

语音识别器为响应仿真输入而生成的 RecognitionResult 对象的 Audio 属性值为 "null"。The RecognitionResult object generated by the speech recognizer in response to emulated input has a value of null for its Audio property.

若要模拟异步识别,请使用 EmulateRecognizeAsync 方法。To emulate asynchronous recognition, use the EmulateRecognizeAsync method.

EmulateRecognize(String)

使用文本代替同步语音识别的音频来模拟输入短语到语音识别器。Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition.

public:
 System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(System::String ^ inputText);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (string inputText);
member this.EmulateRecognize : string -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (inputText As String) As RecognitionResult

Parameters

inputText
String

标识操作的输入。The input for the recognition operation.

Returns

RecognitionResult

输入的标识结果或 null ,如果操作不成功或识别器未启用。The result for the recognition operation, or null if the operation is not successful or the recognizer is not enabled.

Exceptions

识别器没有加载的语音识别语法。The recognizer has no speech recognition grammars loaded.

inputTextnullinputText is null.

inputText 为空字符串 ("")。inputText is the empty string ("").

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

Remarks

语音识别器引发 SpeechDetectedSpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像未模拟识别操作一样。The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated.

向输入短语应用语法规则时,Vista 和 Windows 7 附带的识别器忽略大小写和字符宽度。The recognizers that ship with Vista and Windows 7 ignore case and character width when applying grammar rules to the input phrase. 有关此类比较的详细信息,请参阅 OrdinalIgnoreCaseIgnoreWidthCompareOptions 枚举值。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.

See also

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

使用文本代替同步语音识别的音频来模拟特定字输入到语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。Emulates input of specific words to the speech recognizer, using text in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

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

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.

Returns

RecognitionResult

输入的标识结果或 null ,如果操作不成功或识别器未启用。The result for the recognition operation, or null if the operation is not successful or the recognizer is not enabled.

Exceptions

识别器没有加载的语音识别语法。The recognizer has no speech recognition grammars loaded.

wordUnitsnullwordUnits is null.

wordUnits 包含一个或多个 null 元素。wordUnits contains one or more null elements.

compareOptions 包含值 IgnoreNonSpaceIgnoreSymbols,或 StringSort 标志。compareOptions contains the IgnoreNonSpace, IgnoreSymbols, or StringSort flag.

Remarks

语音识别器引发 SpeechDetectedSpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像未模拟识别操作一样。The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated.

当识别器将语法规则应用于输入短语时,将使用 compareOptionsThe recognizer uses compareOptions when it applies grammar rules to the input phrase. 如果 OrdinalIgnoreCaseIgnoreCase 值存在,则 Vista 和 Windows 7 附带的识别器会忽略大小写。The recognizers that ship with Vista and Windows 7 ignore case if the OrdinalIgnoreCase or IgnoreCase value is present. 识别器始终忽略字符宽度并从不忽略假名类型。The recognizer always ignores the character width and never ignores the Kana type. 识别器还会忽略新行和额外的空格,并将标点符号视为文本输入。The recognizer also ignores new lines and extra white space and treats punctuation as literal input. 有关字符宽度和假名类型的详细信息,请参阅 CompareOptions 枚举。For more information about character width and Kana type, see the CompareOptions enumeration.

See also

EmulateRecognize(String, CompareOptions)

使用文本代替异步语音识别的音频来模拟短语输入到语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。Emulates input of a phrase to the speech recognizer, using text in place of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

public:
 System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(System::String ^ inputText, System::Globalization::CompareOptions compareOptions);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (string inputText, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognize : string * System.Globalization.CompareOptions -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (inputText As String, compareOptions As CompareOptions) As RecognitionResult

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.

Returns

RecognitionResult

输入的标识结果或 null ,如果操作不成功或识别器未启用。The result for the recognition operation, or null if the operation is not successful or the recognizer is not enabled.

Exceptions

识别器没有加载的语音识别语法。The recognizer has no speech recognition grammars loaded.

inputTextnullinputText is null.

inputText 为空字符串 ("")。inputText is the empty string ("").

compareOptions 包含值 IgnoreNonSpaceIgnoreSymbols,或 StringSort 标志。compareOptions contains the IgnoreNonSpace, IgnoreSymbols, or StringSort flag.

Remarks

语音识别器引发 SpeechDetectedSpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像未模拟识别操作一样。The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated.

当识别器将语法规则应用于输入短语时,将使用 compareOptionsThe recognizer uses compareOptions when it applies grammar rules to the input phrase. 如果 OrdinalIgnoreCaseIgnoreCase 值存在,则 Vista 和 Windows 7 附带的识别器会忽略大小写。The recognizers that ship with Vista and Windows 7 ignore case if the OrdinalIgnoreCase or IgnoreCase value is present. 识别器始终忽略字符宽度并从不忽略假名类型。The recognizer always ignores the character width and never ignores the Kana type. 识别器还会忽略新行和额外的空格,并将标点符号视为文本输入。The recognizer also ignores new lines and extra white space and treats punctuation as literal input. 有关字符宽度和假名类型的详细信息,请参阅 CompareOptions 枚举。For more information about character width and Kana type, see the CompareOptions enumeration.

See also

Applies to