Designing Your Grammar
Items in your grammar files define what words and phrases are recognized. When the Speech engine matches an item from the grammar file, it returns SML, or Speech Markup Language, which your application uses to extract definitive values from the text that the user spoke. Having too strict a grammar will result in no flexibility from the user's perspective in regards to what they can say; however, too many unnecessary grammar items can lead to lower speech recognition.
Preambles and Postambles
Very often, you will want to allow a generic "preamble," text said before the main item, and "postamble," text said after the main item. For instance, if the main command is "Buy Stock," you would want to allow the user to say "May I Buy Stock please?"
Typically, you can use one grammar (.grxml
) file for your preambles and one for your postambles. Within your other grammar rules, you can then reference the pre- and post-ambles by using RuleRef's.
|Tip: Make the pre- and post-ambles generic and robust enough that you don't limit your users' experience, but keep them reasonable in size so that you don't risk lowering the speech recognition for your main elements.
Use the Grammar Editor tool to graphically set up grammar files (see Figure 7
). The basic task is to set up a text phrase or a list of phrases, and then assign a value that you want your application to use when each phrase is recognized.
|Figure 7: Use the Grammar Editor tool to graphically set up grammar files.|
We found that the following strategies helped us in grammar development:
Typically, if we only need to recognize that a text phrase has been matched, especially in the case of commands, we fill in the Value field with the empty string rather than a value. For example, if you want to capture when the user says "Help," you can simply return the following SML:
<SML text="help" confidence="1.0">
<GoHelp text="help" confidence="1.0"></GoHelp>
|Figure 8: Use rule references within grammar files to avoid duplicating the same rule across different speech controls.|
The control associated with this grammar file recognizes the phrase, and returns the SML element "GoHelp"; the code-behind or client-side script makes a decision based on the SML element being returned, rather than the value.
Use rule references within grammar files to avoid duplicating the same rule across different speech controls. Tip: You must make sure that a rule to be referenced is a public rule, which you can set through the properties pane (see Figure 8
A common grammars file is included with the Speech SDK, both in an XML file version (cmnrules.grxml
) and in a smaller, faster compiled version (cmnrules.cfg
). We copied the compiled version into our project and used it for commonly used grammar elements, such as digits and letters in the alphabet.