Creating a Grammar Editor Grammar
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
This is the third of several tutorial topics covering development of speech applications using Speech Server. This tutorial creates a smaller version of the sample application Survey (Managed Code Sample).
Use Speech Grammar Editor to create speech recognition grammars that support the World Wide Web Consortium Speech Recognition Grammar Specification (W3C SRGS) Version 1.0. For more information about deciding when to use Speech Grammar Editor and when to use Conversational Grammar Builder, see Use Conversational Grammar Builder to Create Grammars and Use Speech Grammar Editor to Create Grammars.
Creating an SRGS Grammar
Build a rule to recognize answers to the question "On average, how many movies do you see in a theater each year?" This grammar can be easily built using a single rule. Begin by creating the grammar and adding the first few Phrase elements.
To create the first part of the rule
In Solution Explorer, right-click the Grammars node, select Add, and then click New Item.
In the Add New Item dialog box, select Rule-Based Grammar File.
In the Name box, change the file name to MoviesPerYear.grxml, and then click Add.
In Grammar Explorer, right-click Rule1, click Rename, and then rename the rule to MoviesPerYear.
Double-click the MoviesPerYear rule to display Speech Grammar Editor.
In the Toolbox, drag and drop a Phrase element onto the canvas, and then use the scroll bar at the bottom of the designer to drag it to the center of the canvas.
Drag and drop a second Phrase element onto the canvas to the right of the first Phrase element.
Double-click the first Phrase element, and then type I.
Double-click the second Phrase element, and then type see.
Drag and drop a List element onto the canvas to the right of the second Phrase element.
Delete the Phrase element beneath the List element because it is no longer needed.
Drag and drop a third Phrase element onto the canvas to the right of the List element.
Add movies to the third Phrase element.
This is the beginning of a rule that recognizes the phrase "I see 'x' movies???". Obviously, this rule needs some way to recognize spoken numbers; that is, to fill in that missing 'x' value. This value is provided in the next procedure.
To recognize spoken numbers
Drag and drop two RuleRef elements onto the canvas one after the other beneath the List element.
In this case, the RuleRef elements contain references to grammar library rules to recognize spoken numbers. The library grammar is added to new speech projects by default.
Select the upper RuleRef element.
In the Properties pane, click URI, and then click the browse (???) button.
In the Open Rule-Based Grammar File dialog box, browse to the Grammars folder in the current Visual Studio project.
By default, the project is in My Documents\Visual Studio 2005\Projects\SurveyTutorial.
In the project, open the Grammars folder, select Library.grxml, and then click Open.
This sets a reference to the grammar library file.
In the Rule Browser dialog box, select the Cardinal_0 rule, and then click Set Target Rule.
This rule recognizes the number zero.
Repeat steps 2 through 6 for the second RuleRef element, setting a reference to the Cardinal_1_to_999 rule.
This rule recognizes numbers one through 999.
Save the solution.
Now the rule recognizes the phrase "I see three movies???," where the number can be any value from zero to 999. What remains is to provide for recognition of the final words in the utterance and to identify the recognition result the rule returns.
To recognize the final words
Drag and drop a Group element onto the canvas to the right of the final Phrase element.
Drag and drop a List element onto the canvas to the left of the Phrase element and beneath the Group element.
The result should be a List element and a Phrase element below the Group element and a Phrase element below the List element.
Drag and drop a Phrase element onto the canvas under the List element.
There should now be two Phrase elements under the List element.
Type each in one Phrase element and every in the other Phrase element.
In the Phrase element to the right of the List element, type year.
Now the elements under the Group element recognize the two utterances "each year" and "every year." However, because a user might also say "in a year", more Phrase elements are needed.
Drag and drop a Phrase element onto the canvas under the List element, and then drag and drop a second Phrase element to its right, so that the two new Phrase elements are in a pair under the List element.
Add in to the first Phrase element and add a to the second Phrase element.
This grammar rule also needs to make some utterances optional. A user might say "I see five movies in a year," but they might also just say "five."
To make some utterances optional
Right-click the first Phrase element, and then click Make Optional.
The icon added to the upper left corner indicates that zero to one occurrences of that Phrase element are required. In other words, the user need not say this to be recognized.
Make the Phrase elements containing the following three words optional: see, movies, in.
Make the Group element optional.
This applies the optional attribute to all the elements contained in the group.
The final task in authoring this rule is to identify the recognition result the rule returns. For example, if a user says "I see three movies in a year," the recognition result should be the number three.
To specify the recognition result
Drag and drop a Script Tag element onto the canvas to the right of the Group element.
On the Script Tag element, click the browse (???) button.
In the Semantic Script Builder dialog box, click the Assignment tab, click Rule variable in the Source of semantic result frame, and then select Last Rule ($$) in the list box.
Select the Select property check box, and then click OK.
These settings specify that the recognition result for the grammar rule is the value recognized by the last rule reference. For more information, see Grammar Rule Reference Referencing.
Save the solution.
The grammar that you created should be nearly identical to the one shown in the following illustration, with the exception that the symbols on optional phrases and groups do not appear in the illustration.
The rule that you just created recognizes utterances such as:
- Four each year.
- I see three movies in a year.
- Ten every year.
- Five.
Validating the Grammar File
A grammar file must contain valid XML and follow guidelines defined by the W3C SRGS format. Using Speech Grammar Editor, it is possible to create and save grammars that violate these rules and are therefore invalid. Use tools in Speech Grammar Editor to validate an individual grammar rule or an entire grammar file.
First, try validating a single sentence.
To validate a single sentence
In Grammar Explorer, double-click the MoviesPerYear rule.
In the Recognition string box, type I see three movies each year.
Click Check, and then note the SML text in the Output pane.
Next, validate the entire grammar file.
To validate the entire grammar
In Grammar Explorer, select MoviesPerYear.grxml.
On the Grammar menu, click Validate Grammar.
Check the Result
Open MoviesPerYear.grxml in Notepad or an XML Editor. The result should be identical to the following XML text.
<grammar xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions" xml:lang="en-US" tag-format="semantics-ms/1.0" version="1.0" mode="voice" xmlns="http://www.w3.org/2001/06/grammar">
<rule id="MoviesPerYear" scope="public">
<item repeat="0-1">i</item>
<item repeat="0-1">see</item>
<one-of>
<item>
<?MS_Grammar_Editor GroupWrap?>
<ruleref uri="Library.grxml#Cardinal_0" type="application/srgs+xml"/>
</item>
<item>
<?MS_Grammar_Editor GroupWrap?>
<ruleref uri="Library.grxml#Cardinal_1_to_999" type="application/srgs+xml"/>
</item>
</one-of>
<item repeat="0-1">movies</item>
<item repeat="0-1">
<one-of>
<item>
<?MS_Grammar_Editor GroupWrap?>
<item repeat="0-1">in</item>
<item>a</item>
</item>
<item>each</item>
<item>every</item>
</one-of>
<item>year</item>
</item>
<tag>$._value = $$._value</tag>
</rule>
</grammar>
Next Step
See Also
Other Resources
Lab: Constructing the Survey Tutorial Application
Managed Code Sample Applications