共用方式為


SpeechRecognitionEngine.EmulateRecognize 方法

定義

在同步語音辨識中使用文字代替音訊,模擬對語音辨識器的輸入。

多載

EmulateRecognize(String)

在共用的語音辨識器上模擬片語輸入,針對同步的語音辨識使用文字來代替音訊。

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

在語音辨識器上模擬特定單字輸入,針對同步的語音辨識使用文字取代音訊,並指定辨識器如何處理單字間的 Unicode 比較以及已載入的語音辨識文法。

EmulateRecognize(String, CompareOptions)

在語音辨識器上模擬片語輸入,針對同步的語音辨識使用文字來代替音訊,並指定辨識器如何處理片語間的 Unicode 比較以及已載入的語音辨識文法。

備註

這些方法會略過系統音訊輸入,並提供文字給辨識器做為 String 物件或物件的陣列 RecognizedWordUnit 。 當您測試或偵錯應用程式或文法時,這會很有説明。 例如,您可以使用模擬來判斷文字是否在文法中,以及辨識文字時傳回的語意。 SetInputToNull使用 方法,在模擬作業期間停用語音辨識引擎的音訊輸入。

語音辨識器會 SpeechDetected 引發 、 SpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像辨識作業未模擬一樣。 辨識器會忽略新的行和額外的空白字元,並將標點符號視為常值輸入。

注意

RecognitionResult為了回應模擬輸入而由語音辨識器產生的 物件,其 Audio 屬性值為 null

若要模擬非同步辨識,請使用 EmulateRecognizeAsync 方法。

EmulateRecognize(String)

來源:
SpeechRecognitionEngine.cs
來源:
SpeechRecognitionEngine.cs
來源:
SpeechRecognitionEngine.cs

在共用的語音辨識器上模擬片語輸入,針對同步的語音辨識使用文字來代替音訊。

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

參數

inputText
String

辨識作業的輸出。

傳回

辨識作業的結果,如果作業未成功或辨識器未啟用則為 null

例外狀況

辨識器沒有載入語音辨識文法。

inputTextnull

inputText 為空字串 ("")。

範例

下列程式碼範例是主控台應用程式的一部分,示範模擬的輸入、相關聯的辨識結果,以及語音辨識器所引發的相關聯事件。 此範例會產生下列輸出。

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

備註

語音辨識器會 SpeechDetected 引發 、 SpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像辨識作業未模擬一樣。

當將文法規則套用至輸入片語時,隨附于 Vista 和 Windows 7 的辨識器會忽略大小寫和字元寬度。 如需這種比較類型的詳細資訊,請參閱 CompareOptions 列舉值 OrdinalIgnoreCaseIgnoreWidth 。 辨識器也會忽略新行和額外的空白字元,並將標點符號視為常值輸入。

另請參閱

適用於

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

來源:
SpeechRecognitionEngine.cs
來源:
SpeechRecognitionEngine.cs
來源:
SpeechRecognitionEngine.cs

在語音辨識器上模擬特定單字輸入,針對同步的語音辨識使用文字取代音訊,並指定辨識器如何處理單字間的 Unicode 比較以及已載入的語音辨識文法。

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

參數

wordUnits
RecognizedWordUnit[]

文字單位陣列,包含辨識作業的輸入。

compareOptions
CompareOptions

列舉值的位元組合,描述要用於模擬辨識作業的比較類型。

傳回

辨識作業的結果,如果作業未成功或辨識器未啟用則為 null

例外狀況

辨識器沒有載入語音辨識文法。

wordUnitsnull

wordUnits 包含一個或多個 null 項目。

compareOptions 包含 IgnoreNonSpaceIgnoreSymbolsStringSort 旗標。

備註

語音辨識器會 SpeechDetected 引發 、 SpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像辨識作業未模擬一樣。

辨識器會在將文法規則套用至輸入片語時使用 compareOptions 。 隨附于 Vista 和 Windows 7 的辨識器會忽略 OrdinalIgnoreCaseIgnoreCase 值是否存在的情況。 辨識器一律會忽略字元寬度,且永遠不會忽略假名類型。 辨識器也會忽略新的行和額外的空白字元,並將標點符號視為常值輸入。 如需字元寬度和假名類型的詳細資訊,請參閱 CompareOptions 列舉。

另請參閱

適用於

EmulateRecognize(String, CompareOptions)

來源:
SpeechRecognitionEngine.cs
來源:
SpeechRecognitionEngine.cs
來源:
SpeechRecognitionEngine.cs

在語音辨識器上模擬片語輸入,針對同步的語音辨識使用文字來代替音訊,並指定辨識器如何處理片語間的 Unicode 比較以及已載入的語音辨識文法。

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

參數

inputText
String

辨識作業的輸入片語。

compareOptions
CompareOptions

列舉值的位元組合,描述要用於模擬辨識作業的比較類型。

傳回

辨識作業的結果,如果作業未成功或辨識器未啟用則為 null

例外狀況

辨識器沒有載入語音辨識文法。

inputTextnull

inputText 為空字串 ("")。

compareOptions 包含 IgnoreNonSpaceIgnoreSymbolsStringSort 旗標。

備註

語音辨識器會 SpeechDetected 引發 、 SpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件,就像辨識作業未模擬一樣。

辨識器會在將文法規則套用至輸入片語時使用 compareOptions 。 隨附于 Vista 和 Windows 7 的辨識器會忽略 OrdinalIgnoreCaseIgnoreCase 值是否存在的情況。 辨識器一律會忽略字元寬度,且永遠不會忽略假名類型。 辨識器也會忽略新的行和額外的空白字元,並將標點符號視為常值輸入。 如需字元寬度和假名類型的詳細資訊,請參閱 CompareOptions 列舉。

另請參閱

適用於