Partilhar via


Note

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

SpeechRecognitionRejectedEventArgs Class

Provides information for the SpeechRecognitionRejected event.

Inheritance Hierarchy

System.Object
  System.EventArgs
    Microsoft.Speech.Recognition.RecognitionEventArgs
      Microsoft.Speech.Recognition.SpeechRecognitionRejectedEventArgs

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

Syntax

'Declaration
<SerializableAttribute> _
Public Class SpeechRecognitionRejectedEventArgs _
    Inherits RecognitionEventArgs
'Usage
Dim instance As SpeechRecognitionRejectedEventArgs
[SerializableAttribute]
public class SpeechRecognitionRejectedEventArgs : RecognitionEventArgs

Remarks

An instance of SpeechRecognitionRejectedEventArgs is created when the SpeechRecognitionEngine object raises the SpeechRecognitionRejected event at the completion of a RecognizeAsync() operation.

The SpeechRecognitionEngine raises the SpeechRecognitionRejected when none of the Alternates generated by a recognition operation have a high enough Confidence score to be accepted. To obtain detailed information about rejected phrases, access the Result property in the handler for the event.

SpeechRecognitionRejectedEventArgs derives from RecognitionEventArgs.

Examples

The following example recognizes phrases such as "Display the list of artists in the jazz category" or "Display albums gospel". 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. The handler also displays recognition result Alternates that were rejected because of low confidence scores.

using System;
using Microsoft.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    static void Main(string[] args)

    // Initialize a SpeechRecognitionEngine object.
    {
      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(listTypes);
        mediaMenu.Append("in the", 0, 1);
        mediaMenu.Append(genres);
        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.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
        recognizer.SpeechRecognitionRejected +=
          new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);

        // Load the grammar object to the recognizer.
        recognizer.LoadGrammarAsync(mediaMenuGrammar);

        // Set the input to the recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Start asynchronous, continuous recognition.
        recognizer.RecognizeAsync(RecognizeMode.Multiple);
        Console.WriteLine("Starting asynchronous recognition...");

        // Keep the console window open.
        Console.ReadLine();
      }
    }

    // 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);
        Console.WriteLine("  Grammar name:  " + phrase.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);
    }
  }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SpeechRecognitionRejectedEventArgs Members

Microsoft.Speech.Recognition Namespace

RecognitionEventArgs

RecognitionResult

SpeechRecognitionRejected

SpeechRecognized

SpeechRecognizedEventArgs

SpeechHypothesized

SpeechHypothesizedEventArgs