Link to home
Start Free TrialLog in
Avatar of codefinger
codefingerFlag for United States of America

asked on

resetting recognition engine to fewer phrases...

The code attached attempts to adjust the speech recognizer so it will only recognize one phrase.
Once that phrase is recognized, that grammer will be unloaded and another grammer, containing many more commands, will be loaded into the recognizer to await further user input.

(The idea is the speech engine only responds to one phrase at first, then once it hears that phrase, it will respond to additional phrases.)

My problem is, despite calling recognizer.UnloadAllGrammers, the recognizer still responds to phrases that were in its previously-loaded grammer.  That is not the behavior I need.

Debugging, I was able to confirm that the new grammer only contains the one phrase to which I want the engine to respond after running the Reset code attached.

Can anyone tell me what it is I may be doing wrong here?



Public Function Reset(ByRef lex As LastException) As Boolean

        Dim retval As Boolean = True

        If Not _recnizer Is Nothing Then
            _recnizer.PauseRecognizerOnRecognition = True
            _recnizer.UnloadAllGrammars()
        End If

        _commands = Nothing


        _commands = New Speech.Recognition.Choices

        _commands.Add(My.Settings.Attention_Phrase)
        
       

        _gbldr = New System.Speech.Recognition.GrammarBuilder(_commands)


        If Not _grm Is Nothing Then
            _grm = Nothing
        End If

        _grm = New System.Speech.Recognition.Grammar(_gbldr)


        If Not _recnizer Is Nothing Then
            _recnizer.LoadGrammar(_grm)
        End If


        LastCommand = ""


        Return retval
    End Function

Open in new window

SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of codefinger

ASKER

Ah, so this time it isn't because of some code I wasn't aware of....like maybe there was a refresh() call on the recognizer or something like that needed after UnloadAllGrammers.

I am not exactly sure how to ask the recognizer engine what phrases it works with...

I have code in the Speech_Recognized event to acknowledge when it hears something it recognizes.
So when my grammer doesn't include a phrase and it reacts anyway, I have a problem. (Debugging confirmed it did not just mishear something else...it actually heard and recognized the earlier phrase that was not supposed to be available.)

I had thought earlier that instead of unloading and loading grammers, I should just load all that I would need and enable and disable them as called for.  The application should react a little faster and MAYBE that approach will resolve the timing issue as well.  I will be working on that this evening.  

Thanks for the input.  I'll let you know how it turns out.
What happens if you start with the 2nd set of phrases, without loading the 1st set of phrases, does it have the same recognition problem?
I think I found part of the solution...there is a property of the recognizer called PauseOnRecognition that can be set to true.   Its too soon to tell for sure, but so far, that seems to have helped.

My new approach is not working out quite like I planned.  I don't think I quite understand how to work with multiple grammars loaded at the same time, but separating the two groups of phrases into two different grammars and unloading and loading them as needed also seems to be helping.

So I am getting there...I won't have a chance to work on this again until Thursday.  Again, thanks for the input.  I will let you know how it goes.
I thought you already had the pause in the code:

        
        If Not _recnizer Is Nothing Then
            _recnizer.PauseRecognizerOnRecognition = True
            _recnizer.UnloadAllGrammars()
        End If

Open in new window


Are you saying that you moved that call to a different point in the process?
DOH!  I thought I just added that pause for the first time last night.   Did not even realize it was already in the orignal code, so I guess that pause is not what is helping after all....sorry, guess I am getting a bit senile....please try to be patient as I dodder along here....
I have never worked with multiple grammars, either, so I am very interested in this process, and the problems that you are experiencing.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would love to hear why you need multiple grammars, and to switch between them.
Imagine that you cannot use your hands.  You cannot push a mute button on your microphone or reach up to take off your headset.   When you are controlling your computer, the microphone must be listening all the time.  But sometimes you need to talk to someone else in the room.  How does the computer "know" when/if you are talking to it and when you are talking to someone else?

My approach is to use two grammars.   The first grammar can only recognize an attention phrase (user defined), like "Computer" or "Attention" and a couple of cancel phrases "Never mind" or "Not you".

When an attention phrase is recognized, the second, more extensive grammar is loaded, and interacts with the user until it is no longer needed.  Then it is unloaded and the first grammar is loaded again to wait quietly until the second grammar is needed again.  The first grammar acts as the gatekeeper.



I can't quite remember, but are you using SpeechRecognitionEngine or SpeechRecognizer?
Public WithEvents _recnizer As New Speech.Recognition.SpeechRecognizer

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That would probably work, but would not the user have to click a mouse?  (No hands, remember?)

Anyway, my approach of loading and unloading the needed grammars seems to work.  I have attached some useful code:  The function attached displays the available phrases from the currently loaded Grammar.   (Its always Grammar(0), since I only use one grammar at a time.)

(FYI, sc is my SpeechClass.)


Public Function DisplayCurrentCommandList(ByRef lex As LastException) As Boolean
        Dim retval As Boolean = True
        Dim strsplit() As String
        Dim cntr As Integer = 0
        Dim str_commands As String = ""
        Dim currentgrammar As System.Speech.Recognition.Grammar



        Try
            currentgrammar = sc._recnizer.Grammars(0)
            Select Case currentgrammar.Name
                Case "Attention Phrases"
                    strsplit = sc.AttentionCommands.ToGrammarBuilder().DebugShowPhrases().Split(",")
                Case "User Phrases"
                    strsplit = sc.UserCommands.ToGrammarBuilder().DebugShowPhrases().Split(",")
               
            End Select

            'eliminate the opening and closing brackets...
            strsplit(0) = Right(strsplit(0), strsplit(0).Length - 1)
            strsplit(strsplit.Length - 1) = Left(strsplit(strsplit.Length - 1), strsplit(strsplit.Length - 1).Length - 1)


            For cntr = 0 To strsplit.Length - 1
                str_commands = str_commands & strsplit(cntr) & vbCrLf
            Next

            frm.TextBoxCommands.Text = str_commands

        Catch ex As Exception

            lex.ErrorMessage = ex.Message
            If Not ex.InnerException Is Nothing Then
                lex.ErrorMessage = lex.ErrorMessage & vbCrLf & ex.InnerException.Message
            End If
            retval = False
        End Try

        Return retval
    End Function

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all the useful input.   It got me past my problem and on to my next challenge.  I will keep this discussion in my history and will probably be referring to it in the future.