SpeechRecognitionResult class

 

The SpeechRecognitionResult class contains the results of a speech recognition session.

Syntax

public class SpeechRecognitionResult

The SpeechRecognitionResult class has the following members.

Properties

Name

Description

Text

The recognized text of a speech recognition session.

TextConfidence

The estimated accuracy of the current SpeechRecognitionResult.

Methods

Name

Description

GetAlternates(int)

Returns the list of alternate interpretations of a speech recognition session.

Remarks

The SpeechRecognitionResult class is returned by the SpeechRecognizer.RecognizeSpeechToTextAsync() method. Additional SpeechRecognitionResult instances may be returned by the SpeechRecognitionResult.GetAlternates(int) method. The class is never instantiated directly.

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 the result 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 returned SpeechRecognitionResult object will have a Text value of null and a TextConfidence value of SpeechRecognitionConfidence.Rejected. This SpeechRecognitionResult object becomes the only member of the Alternates list.

Caution

When collecting speech results or intermediate results in a JavaScript application, quiet or unclear speech may cause the recognizeSpeechToTextAsync() method to return an error object in place of result text. To maintain smooth program flow, verify that the result text is a string before attempting to read it. For more information, see How to: Add the Bing Speech Recognition Control to an application with a custom UI.

Example

The following example writes the Text and TextConfidence values of a SpeechRecognitionResult to a TextBlock named ResultsText. It then gets the list of alternates and uses them to populate a ListBox named AlternatesListBox. The SelectionChanged event handler for the listbox then updates ResultsText with the alternate value selected by the user.

private async void SpeakButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        // Get a speech recognition result.
        var result = await SR.RecognizeSpeechToTextAsync();

        // Show the TextConfidence.
        ConfidenceText.Text = getConfidence(result.TextConfidence);

        // Display the text.
        ResultsText.Text = result.Text;

        // Fill a string array with the alternate results.
        var alternates = result.GetAlternates(5);
        if (alternates.Count > 1)
        {
            string[] s = new string[alternates.Count];
            for (int i = 1; i < alternates.Count; i++)
            {
                s[i] = alternates[i].Text;
            }

            // Populate the alternates ListBox with the array.
            AlternatesListBox.ItemsSource = s;
        }
    }
    catch (Exception ex)
    {
        // Do exception handling here.
    }
}

private void AlternatesListBox_SelectionChanged(object sender, 
    SelectionChangedEventArgs e)
{
    // Check in case the ListBox is still empty.
    if (null != AlternatesListBox.SelectedItem)
    {
        // Put the selected text in FinalResult and clear ConfidenceText.
        FinalResult.Text = AlternatesListBox.SelectedItem.ToString();
        ConfidenceText.Text = ""; 
    }
}

private string getConfidence(SpeechRecognitionConfidence confidence)
{
    switch (confidence)
    {
        case SpeechRecognitionConfidence.High:
            return("I am almost sure you said:");
        case SpeechRecognitionConfidence.Medium:
            return("I think you said:");
        case SpeechRecognitionConfidence.Low:
            return("I think you might have said:");
        case SpeechRecognitionConfidence.Rejected:
            return("I'm sorry, I couldn't understand you."
            + " Please click the Cancel button and try again.");
    }
}
function SpeakButton_Click() {
    // Get a speech recognition result.
    SR.recognizeSpeechToTextAsync()
        .then(
            function (result) {
                // Verify that result.text is a string and not an error object.
                if (typeof (result.text) == "string") {
                    // Show the textConfidence.
                    var tc = getConfidence(result.textConfidence);
                    document.getElementById('ConfidenceText').innerHTML = window.toStaticHTML(tc); 

                    // Display the text.
                    document.getElementById('ResultText').innerHTML = result.text;

                    // Write the alternate results to a ListBox.
                    var alternates = result.getAlternates(maxAlt);
                    if (alternates.length > 1) {
                        for (var i = 0; i < alternates.length; i++) {
                            var opt = document.createElement("option");
                            opt.innerHTML = alternates[i].text;
                            document.getElementById('ResultChooser').appendChild(opt);
                        }
                    }
                }
                else {
                    // Handle speech that is quiet or unclear.
                    s = "I'm sorry. I couldn't understand you."
                }
            }, 
            function (error) {
                // Do error handling here.
            }
         )
}

function AlternatesListBox_SelectionChanged( sender, e) {
    var alternatesListBox = document.GetElementById("AlternatesListBox");
    var resultText = document.GetElementById("ResultText ");

    // Check in case the ListBox is still empty.
    if (null != alternatesListBox.childNodes) {
        // Put the selected text in ResultText and clear ConfidenceText.
        var item = alternatesListBox.childNodes[alternatesListBox.selectedIndex];
        resultText.innerText = item.textContent
        document.getElementById('ConfidenceText').innerText = "";
    }
}

function GetConfidence(confidence) {
    switch (confidence) {
        case Bing.Speech.SpeechRecognitionConfidence.high:
            return ("I am almost sure you said:");
        case Bing.Speech.SpeechRecognitionConfidence.medium:
            return ("I think you said:");
        case Bing.Speech.SpeechRecognitionConfidence.low:
            return ("I think you might have said:");
        case Bing.Speech.SpeechRecognitionConfidence.rejected:
            return ("I'm sorry, I couldn't understand you."
            + " Please click the Cancel button and try again.");
    }
}

Requirements

Minimum Supported Client

Windows 8

Required Extensions

Bing.Speech

Namespace

Bing.Speech