ISpeechRecoResult Alternates Method (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

Interface: ISpeechRecoResult

Alternates Method

The Alternates method returns a list of alternate words.

Many recognitions, successful or not, frequently return several words or phrases that closely match the spoken sequence. The one most nearly matching the sequence with a high confidence level is returned as a successful recognition. The other words and phrases are returned as alternates that are available for examination.

ISpeechRecoResult.Alternates(
     RequestCount As Long,
     [StartElement As Long = 0],
     [Elements As Long = -1]
) As ISpeechPhraseAlternates

Parameters

RequestCount
Specifies the maximum number of alternates to retrieve, which must be greater than zero. Any number of alternates may be chosen and will be returned in descending order of confidence. That is, the first alternate returned has the highest confidence and will most likely be the word or phrase chosen by the successful recognition. The second alternate returned will be the next most likely choice, and the last alternate returned the least likely match.
StartElement
[Optional] Specifies which element to use as a starting point. If omitted, zero is used and indicates the first alternate as the starting point. Because it is zero based, the second element would be one.
Elements
[Optional] Specifies the number of elements to retrieve. Default is -1, which specifies all alternate elements are retrieved.

Return Value

The Alternates method returns an ISpeechPhraseAlternates variable.

Remarks

ISpeechRecoResult.Alternates applies only to dictation grammar. Command and control alternates are handled separately and independently. See ISpeechRecoContext.CmdMaxAlternates for command and control alternates.

Example

The following Visual Basic form code returns the 6 most likely matches for a recognition. The method call uses only one parameter, the number of alternates to return; the other parameters are assigned by default and return all the available words and phrases. Note that the first alternate returned is always the recognized word or phrase.

To run this code, paste it into the Declarations section of a form that contains no controls. In addition to the usual reference to the Microsoft Speech Object Library, this code also needs a reference to the simpleaudio 1.0 Type Library.


Option Explicit

Const AUDIOFORMAT = SAFT8kHz16BitMono

' Text-to-Speech variables:
Dim WithEvents Voice As SpVoice
Dim EndofStream As Boolean
Dim AudioPlugOut As SpAudioPlug

' Speech Recognition variables:
Dim WithEvents RecoContext As SpInProcRecoContext
Dim Grammar As ISpeechRecoGrammar
Dim Recognizer As SpInprocRecognizer
Dim AudioPlugIn As SpAudioPlug

Private Sub Form_Load()

    Const Text = "One of the world's seven wonders"
    Dim Output As Variant

    On Error GoTo EH

    Set Voice = New SpVoice

    ' Set up output audio:
    Set AudioPlugOut = New SpAudioPlug
    AudioPlugOut.Init True, AUDIOFORMAT
    Set Voice.AudioOutputStream = AudioPlugOut

    ' Set up input audio:
    Set AudioPlugIn = New SpAudioPlug
    AudioPlugIn.Init False, AUDIOFORMAT
    Set Recognizer = New SpInprocRecognizer
    Set Recognizer.AudioInputStream = AudioPlugIn

    ' Set up speech recognition:
    Set RecoContext = Recognizer.CreateRecoContext
    Set Grammar = RecoContext.CreateGrammar(1)
    Grammar.DictationLoad
    Grammar.DictationSetState SGDSActive

    ' Speak some text to be recognized.
    Voice.Speak Text, SVSFlagsAsync

    Do While EndofStream = False
        DoEvents

        ' Get audio data from audio object.
        Output = AudioPlugOut.GetData

        ' Output audio data to input audio object--
        If (Len(Output) * 2 <> 0) Then
            AudioPlugIn.SetData (Output)
        End If

    Loop

    Grammar.DictationSetState SGDSInactive

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub RecoContext_Recognition _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)

    Dim phraseAlternate As ISpeechPhraseAlternates
    Dim i As Long
    Dim T As String
    Dim theString As String

    On Error GoTo EH

    Set phraseAlternate = Result.Alternates(6)
    For i = 0 To phraseAlternate.Count - 1
        theString = phraseAlternate.Item(i).PhraseInfo.GetText
        T = T & "Alternates #" + Str(i) + ": " & theString + vbNewLine
    Next i

    MsgBox T, vbInformation
    End

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Voice_EndStream _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant)

    EndofStream = True

End Sub

Private Sub ShowErrMsg()

    ' Declare identifier:
    Dim T As String

    T = "Desc: " & Err.Description & vbNewLine
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub