SpeechRecognitionEngine.SpeechRecognitionRejected 事件


SpeechRecognitionEngine 采用不与其加载启用的 Grammar 对象匹配的输入的时候引发。Raised when the SpeechRecognitionEngine receives input that does not match any of its loaded and enabled Grammar objects.

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


下面的示例将识别一些短语,如 "在爵士乐类别中显示音乐家列表" 或 "显示唱片集 gospel"。The following example recognizes phrases such as "Display the list of artists in the jazz category" or "Display albums gospel". 该示例使用SpeechRecognitionRejected事件的处理程序,以便在语音输入无法与语法的内容匹配且足以Confidence产生成功的识别时在控制台中显示通知。The example uses a handler for the SpeechRecognitionRejected event to display a notification in the console when the speech input cannot be matched to the contents of the grammar with sufficient Confidence to produce a successful recognition. 处理程序还显示由于置信Alternates度较低而被拒绝的识别结果。The handler also displays recognition result Alternates that were rejected because of low confidence scores.

using System;  
using System.Speech.Recognition;  
namespace SampleRecognition  
  class Program  
    static void Main(string[] args)  
    // Initialize an in-process speech recognition engine.  
      using (SpeechRecognitionEngine recognizer =  
         new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))  
        // 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");  
        mediaMenu.Append("the list of", 0, 1);  
        mediaMenu.Append("in the", 0, 1);  
        mediaMenu.Append("category", 0, 1);  
        //  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.SpeechRecognitionRejected +=  
          new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);  
        // Load the grammar object to the recognizer.  
        // Set the input to the recognizer.  
        // Start recognition.  
        // Keep the console window open.  
    // Handle the SpeechRecognitionRejected event.  
    static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)  
      Console.WriteLine("Speech input was rejected.");  
      foreach (RecognizedPhrase phrase in e.Result.Alternates)  
      Console.WriteLine("  Rejected phrase: " + phrase.Text);  
      Console.WriteLine("  Confidence score: " + phrase.Confidence);  
    // 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);  
      Console.WriteLine("  Confidence score: " + e.Result.Confidence);  


识别器会引发此事件,前提是它确定输入与已加载和启用Grammar的任何对象的完全置信度不符。The recognizer raises this event if it determines that input does not match with sufficient confidence any of its loaded and enabled Grammar objects. RecognitionResultResult 属性包含被SpeechRecognitionRejectedEventArgs拒绝的对象。The Result property of the SpeechRecognitionRejectedEventArgs contains the rejected RecognitionResult object. 您可以使用SpeechRecognitionRejected事件的处理程序来检索已拒绝Alternates Confidence的识别及其分数。You can use the handler for the SpeechRecognitionRejected event to retrieve recognition Alternates that were rejected and their Confidence scores.

如果你的应用程序使用SpeechRecognitionEngine的是实例,则可以修改使用其中一种UpdateRecognizerSetting方法接受或拒绝语音输入的置信度级别。If your application is using a SpeechRecognitionEngine instance, you can modify the confidence level at which speech input is accepted or rejected with one of the UpdateRecognizerSetting methods. 您可以使用BabbleTimeoutInitialSilenceTimeoutEndSilenceTimeoutEndSilenceTimeoutAmbiguous属性修改语音识别对非语音输入的响应方式。You can modify how the speech recognition responds to non-speech input using the BabbleTimeout, InitialSilenceTimeout, EndSilenceTimeout, and EndSilenceTimeoutAmbiguous properties.

创建 SpeechRecognitionRejected 委托时,需要标识将处理该事件的方法。When you create a SpeechRecognitionRejected delegate, 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.