ISpeechPhraseRule Code Example

Microsoft Speech SDK

Intelligent Interface Technologies Home Page Microsoft Speech SDK

Speech Automation 5.1

ISpeechPhraseRule Code Example

The following Visual Basic form code demonstrates the use of the ISpeechPhraseRule interface. To run this code, create a form with the following controls:

  • A text box called Text1
  • A list box called List1
  • A command button called Command1
  • Paste this code into the Declarations section of the form.

    The Form_Load procedure creates a recognizer, a recognition context and a grammar object. It loads the grammar object with the Solitaire grammar sol.xml and activates it.

    The Command1 procedure speaks the text in the text box into a file for recognition. The recognition context's Recognition event displays selected information from the recognition result. It displays the phrase elements first, and then the properties of the ISpeechPhraseRule object.

    Note that both the Parent and Children properties may have a value of Nothing. Attempting to reference them in this state will cause a run-time error.


    Option Explicit
    
    Const WAVEFILENAME = "C:\ISpeechPhraseElement.wav"
    
    Dim R As SpeechLib.SpInprocRecognizer
    Dim G As SpeechLib.ISpeechRecoGrammar
    Dim F As SpeechLib.SpFileStream
    Dim E As SpeechLib.ISpeechPhraseElement
    Dim V As SpeechLib.SpVoice
    Dim V2 As SpeechLib.SpVoice     'Plays the wave file back
    
    Dim WithEvents C As SpeechLib.SpInProcRecoContext
    
    Private Sub Command1_Click()
        
        List1.Clear
        Call SpeakToFile(Text1.Text, WAVEFILENAME)
        F.Open WAVEFILENAME
        Set R.AudioInputStream = F
    
    End Sub
    
    Private Sub Form_Load()
    
        ' Create Recognizer, RecoContext, Grammar, and Voice
        Set R = New SpInprocRecognizer
        Set C = R.CreateRecoContext
        Set G = C.CreateGrammar(16)
        Set V = New SpVoice
        Set V.Voice = V.GetVoices("gender=male").Item(0)
        Set V2 = New SpVoice
        
        ' Load Grammar with solitaire XML, set active
        G.CmdLoadFromFile "c:\sol.xml", SLODynamic
        G.CmdSetRuleIdState 0, SGDSActive               'Set C & C active
        G.DictationSetState SGDSActive                  'Set Dictation active
    
        Text1.Text = "play the eight of clubs"
    
    End Sub
    
    Private Sub SpeakToFile(ByVal strText As String, ByVal strFName As String)
        Set F = New SpFileStream                    'Create stream
        F.Open strFName, SSFMCreateForWrite, True   'Open as the filename
        Set V.AudioOutputStream = F                 'Set voice output to file
        V.Speak strText, SVSFIsXML                  'Speak synchronously
        F.Close                                     'Close file
    End Sub
    
    Private Sub C_Recognition(ByVal StreamNumber As Long, _
                        ByVal StreamPosition As Variant, _
                        ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
                        ByVal Result As SpeechLib.ISpeechRecoResult)
        Dim X As String
        Dim ii As Integer
        Dim PR As ISpeechPhraseRule
        Dim PRs As ISpeechPhraseRules
    
        ii = 0
        For Each E In Result.PhraseInfo.Elements
            X = "element" & Str(ii) & ": " & E.DisplayText
            List1.AddItem X
            ii = ii + 1
        Next
        
        'This is the rule that recognition was based on
        Set PR = Result.PhraseInfo.Rule
        
        List1.AddItem ""
        List1.AddItem "Id:               " & PR.Id
        List1.AddItem "Rule:             " & PR.Name
        List1.AddItem "NumberOfElements: " & PR.NumberOfElements
        List1.AddItem "FirstElement:     " & PR.FirstElement
        List1.AddItem "EngineConfidence: " & PR.EngineConfidence
        List1.AddItem "Confidence:       " & PR.Confidence
        List1.AddItem ""
        
        If PR.Parent Is Nothing Then
            List1.AddItem "Parent:           none"
        Else
            List1.AddItem "Parent:           " & PR.Parent.Name
        End If
        
        If PR.Children Is Nothing Then
            List1.AddItem "Children:         none"
        Else
            'This routine replaces the original PR object
            Set PRs = PR.Children
            ii = 0
            For Each PR In PRs
                List1.AddItem "Child" & Str(ii) & ":          " & PR.Name
                ii = ii + 1
            Next
        End If
        
    End Sub
    
    Private Sub C_EndStream(ByVal StreamNumber As Long, _
                        ByVal StreamPosition As Variant, _
                        ByVal StreamReleased As Boolean)
        F.Close
        DoEvents
        F.Open WAVEFILENAME
        V2.SpeakStream F
        F.Close
    End Sub