In order to accept input from the microphone, I must define a set of commands that the speech recognition engine can use when analyzing speech. But before I do that, I need to Load an Agent character. I create an object reference to the character so I only need to type MyAgent rather than Agent1.Characters(“Merlin”). Listing 14.5 shows the routine for initializing the Commands collection.
Listing 14.5: Form_Load Event in Agent Demo
Private Sub Form_Load()
Agent1.Characters.Load “Merlin”, AgentPath & “MerlinSfx.acs”
Set MyAgent = Agent1.Characters(“Merlin”)
MyAgent.Show
MyAgent.Commands.Caption = “MyAgent Commands”
MyAgent.Commands.Visible = True
MyAgent.Commands.Voice = “MyAgent Commands”
MyAgent.Commands.Add “Bye bye”, “Bye bye”, “... bye bye ...”, True, True
MyAgent.Commands.Add “Down”, “Down”, “... down ...”, True, True
MyAgent.Commands.Add “Happy”, “Happy”, “... happy ...”, True, True
MyAgent.Commands.Add “Hello”, “Hello”, “Hello Merlin”, True, True
MyAgent.Commands.Add “Left”, “Left”, “... left ...”, True, True
MyAgent.Commands.Add “Move”, “Move”, “... move ...”, True, True
MyAgent.Commands.Add “Right”, “Right”, “... right ...”, True, True
MyAgent.Commands.Add “Sad”, “Sad”, “... sad ...”, True, True
MyAgent.Commands.Add “Up”, “Up”, “... up ...”, True, True
End Sub
Unlike most other ActiveX controls, the MSAgent control doesn’t allow you to define many properties at design time. You are forced to do this at runtime. So after I’ve created and shown the Agent, I set Commands.Caption and Commands.Voice to “MyAgent Commands”, and then I set the Commands.Visible property to True. This information will be displayed in the Commands window of the Agent server.
Then I initialize each command that the speech recognition engine will support. The parameters, in order, are as follows:
TIP: It’s all in a name: Since you can’t retrieve the Name property from the Command object, I usually assign the same value to the Name and Caption properties. That way, I know what the missing Name property should be.
In the code in Listing 14.5, notice that rather than focusing on complex phrases that the speech recognition engine should try to match, I focus on a keyword that best describes the command. Then I use an ellipsis before and after the word that indicates that zero or more other words (also known as garbage words) may proceed or follow the keyword, as in this line:
MyAgent.Commands.Add “Move”, “Move”, “... move ...”, True, True
I find this often makes the recognition process more reliable. However, you should use this feature carefully, because it may also cause some problems with false matches. In this example, if the user said “move up,” both “move” and “up” would be potential matches.