SpeechRecognizer.SpeechHypothesized 事件

定义

当识别器已识别可以为语法中多个完整的短语的组件的一个字或多个字时发生。Occurs when the recognizer has recognized a word or words that may be a component of multiple complete phrases in a grammar.

public:
 event EventHandler<System::Speech::Recognition::SpeechHypothesizedEventArgs ^> ^ SpeechHypothesized;
public event EventHandler<System.Speech.Recognition.SpeechHypothesizedEventArgs> SpeechHypothesized;
member this.SpeechHypothesized : EventHandler<System.Speech.Recognition.SpeechHypothesizedEventArgs> 
Public Custom Event SpeechHypothesized As EventHandler(Of SpeechHypothesizedEventArgs) 

示例

下面的示例将识别一些短语,如 "在爵士乐类别中显示音乐家列表"。The following example recognizes phrases such as "Display the list of artists in the jazz category". 该示例使用SpeechHypothesized事件在控制台中显示不完整的短语片段,因为它们被识别。The example uses the SpeechHypothesized event to display incomplete phrase fragments in the console as they are recognized.

using System;  
using System.Speech.Recognition;  
  
namespace SampleRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  
  
    // Initialize a shared speech recognition engine.  
    {  
      using (SpeechRecognizer recognizer =  
         new SpeechRecognizer())  
      {  
  
        // Create a grammar.  
        //  Create lists of alternative choices.  
        Choices listTypes = new Choices(new string[] { "albums", "artists" });  
        Choices genres = new Choices(new string[] {   
          "blues", "classical", "gospel", "jazz", "rock" });  
  
        //  Create a GrammarBuilder object and assemble the grammar components.  
        GrammarBuilder mediaMenu = new GrammarBuilder("Display the list of");  
        mediaMenu.Append(listTypes);  
        mediaMenu.Append("in the");  
        mediaMenu.Append(genres);  
        mediaMenu.Append("category.");  
  
        //  Build a Grammar object from the GrammarBuilder.  
        Grammar mediaMenuGrammar = new Grammar(mediaMenu);  
        mediaMenuGrammar.Name = "Media Chooser";  
  
        // Attach event handlers.  
        recognizer.LoadGrammarCompleted +=  
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.SpeechHypothesized +=   
          new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);  
  
        // Load the grammar object to the recognizer.  
        recognizer.LoadGrammarAsync(mediaMenuGrammar);  
  
        // Keep the console window open.  
        Console.ReadLine();  
      }  
    }  
  
    // Handle the SpeechHypothesized event.  
    static void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)  
    {  
      Console.WriteLine("Speech hypothesized: " + e.Result.Text);  
    }  
  
    // Handle the LoadGrammarCompleted event.  
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
    {  
      Console.WriteLine("Grammar loaded: " + e.Grammar.Name);  
    }  
  
    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine("Speech recognized: " + e.Result.Text);  
    }  
  }  
}  
  

注解

当输入不明确时,共享识别器可以引发此事件。The shared recognizer can raise this event when the input is ambiguous. 例如,对于支持识别 "新游戏" 或 "新游戏" 的语音识别语法,"新游戏" 是一种明确的输入,"新游戏" 是一种不明确的输入。For example, for a speech recognition grammar that supports recognition of either "new game please" or "new game", "new game please" is an unambiguous input, and "new game" is an ambiguous input.

SpeechHypothesized事件创建委托时,需要标识将处理该事件的方法。When you create a delegate for a SpeechHypothesized event, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅事件和委托For more information about event-handler delegates, see Events and Delegates.

适用于

另请参阅