Partager via


SpeechRecognitionResult.GetAlternates(int) method

 

The GetAlternates(int) method returns the list of alternate interpretations of a speech recognition attempt.

Syntax

public IReadOnlyList<SpeechRecognitionResult> GetAlternates(int maxAlternates)

Parameters

maxAlternates

Type: System.Int

The maximum number of alternates to return.

Return Value

Type: System.Collections.Generic.IReadOnlyList<Bing.Speech.SpeechRecognitionResult>

A list of up to maxAlternates alternate speech recognition results.

Remarks

When a SpeechRecognizer finishes analyzing a speech session, it creates an IReadOnlyList of possible interpretations as SpeechRecognitionResult objects and ranks them in order of confidence. The SpeechRecognitionResult object with the highest TextConfidence value is ranked as item [0] in the list and is returned by the SpeechRecognizer.RecognizeSpeechToTextAsync() method. The GetAlternates(int) method gets the list of these alternate results.

In cases where no interpretation meets the minimum confidence threshold, the list of alternates will consist of a single SpeechRecognitionResult object with a Text value of null and a TextConfidence value of SpeechRecognitionConfidence.Rejected.

Invoking the GetAlternates(int) method on a primary SpeechRecognitionResult object or any member of the associated list of alternates will return the same result.

Example

The following example gets the alternates from a SpeechRecognitionResult, and then writes the Text and TextConfidence values to a drop down list named ResultChooser.

// Set maxAlternates.
var maxAlternates = 5; 

// Get a speech recognition result from SpeechRecognizer SR.
var result = await SR.RecognizeSpeechToTextAsync();

// Get the list of alternates.
var alternates = result.GetAlternates(maxAlternates);
if (alternates.Count > 0)
{
    // Write the values to a string[] array.
    string[] s = new string[alternates.Count];
    for (int i = 0; i < alternates.Count; i++)
    {
        s[i] = string.Format("{0} -- {1}", 
            alternates[i].Text, 
            alternates[i].TextConfidence.ToString());
    }

    // Populate the ListBox.
    this.ResultChooser.ItemsSource = s;
}
// Set maxAlternates.
var maxAlternates = 5;

// Get a speech recognition result from SpeechRecognizer SR.
SR.recognizeSpeechToTextAsync()
     .then(
         function (result) {
             if (typeof (result.text) == "string") {
                 // Write the alternate results to <option> elements.
                 var alternates = result.getAlternates(maxAlt);
                 if (alternates.length > 1) {
                     for (var i = 0; i < alternates.length; i++) {
                         var opt = document.createElement("option");
                         var tc = alternates[i].textConfidence.toString();
                         opt.innerHTML = window.toStaticHtml(alternates[i].text + " -- " + tc);

                         // Add the <option> elements to ResultChooser.
                         document.getElementById('ResultChooser').appendChild(opt);
                     }
                 }
             }
         }
      )

Requirements

Minimum Supported Client

Windows 8

Required Extensions

Bing.Speech

Namespace

Bing.Speech