Designing Grammar Rules
A grammar contains a set of rules that specify the words, phrases, or commands that a user can speak to a speech-enabled Web application. These commands are available for recognition by the application. XML elements and plain text are used to create the rules that identify the words or phrases that comprise these commands. The rules used to identify commands are represented by rule elements. In addition to identifying commands, rules provide structure to the commands by allowing encapsulation of semantically related words or phrases. This structure provides logical organization of user commands, and also allows for reuse of rules within containing or external grammars.
A grammar must provide structured, logical speech statements that apply to specific situations. At the same time, the grammar must be general enough to allow slight variations in the statement. This allows a more natural speaking style and provides a better user experience. For example, a coffee ordering application must accept and respond to multiple ways a user can order coffee; "I would like a decaf latte," "I want a decaf latte," or "Gimme a decaf latte." A coffee ordering application must also process and respond to a user ordering multiple types of coffee drinks in a multitude of variations (for example, substitute the word latte for cappuccino or mocha, and insert variations of those drinks such as decaf or iced). However, the application does not need to process and respond to a user ordering airline tickets; thus, a grammar must include words and phrases limited to a specific situation or task.
Define a grammar structure using different elements in a specific sequence to provide the user with logical variations of phrases or single word options. See Grammar XML for specific information on each valid grammar element. The Grammars Overview provides an introduction to grammar elements.
Use the following information to assist in designing grammar rules, and in using rules appropriately within a grammar structure:
To design grammar rules
- Create a List of User Commands
- Create Rules for Command Recognition
- Create Recognizable Sentences for User Commands
- Create a Series of Possible User Commands
- Create Variations of User Commands
Create a List of User Commands
To begin designing a grammar, list obvious user commands. In a simple case, user commands can include a single word, such as "open," or "print."
To create user commands
Use the grammar elements described in the following table to construct a list of commands for a user to speak:
Element Description grammar Every grammar structure must include the grammar element start tag and the grammar element end tag. The grammar element is a container for all rule grammar definitions. The grammar element has the following required attributes to further define the grammar: - version, which is an identifier (default value 1.0) that identifies the version of the XML Speech Recognition Grammar Format.
- xml:lang, which is the language identifier for the grammar or language contained by the document.
- root, which explicitly identifies the name of the default grammar rule.
- xmlns, which identifies the XML namespace (http://www.w3.org/2001/06/grammar).
- tag-format, which is an identifier (default value semantics-ms/1.0) that identifies the content type of all tag elements contained in the grammar.
rule Each rule element must contain a unique identifier that defines the rule. The ID attribute provides this unique identifier. Developers can reference the grammar rule from elsewhere within the containing grammar using the ID attribute. The following example uses the value, "ruleOpen" as the ID attribute. The scope attribute of the rule element designates the grammar public or private, which indicates whether a rule can be referenced from an external grammar structure. item The item element surrounds the word commands from which the user has say. In the following example, the item element contains the user command "open."
Note A rule does not require item elements.Use the following grammar code example to create a grammar structure that contains the user command:
<grammar root="ruleOpen" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en-US" tag-format="semantics-ms/1.0" ... > <rule id="ruleOpen" scope="public"> <item>open</item> </rule> </grammar>
Create rules for command recognition. These steps are covered in the following section.
Create Rules for Command Recognition
A grammar can have one rule, the "root" rule, available to an application for user command recognition use. Each rule is a separate and independent definition, which uniquely identifies the user commands an application uses for recognition.
To create grammar rules for command recognition
Begin by creating two rules that define the content for the two user commands. In the example below, ruleOpen identifies the user command, "open," and rulePrint identifies the user command, "print."
Create a top-level rule containing a one-of list with two items. In the first item, place a ruleref element that refers to the ruleOpen rule. In the second item, place a ruleref element that refers to the rulePrint rule.
Specify the top-level rule as the root rule of the grammar by setting the value of the root attribute in the grammar element to the value of the ID attribute in the top-level rule.
Use the following grammar code example to create the grammar structure used for the rules:
<grammar root="ruleTopLevel" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en-US" tag-format="semantics-ms/1.0" ... > <rule id="ruleTopLevel" scope="public"> <one-of> <item> <ruleref uri="#ruleOpen" /> </item> <item> <ruleref uri="#rulePrint" /> </item> </one-of> </rule> <rule id="ruleOpen" scope="public"> <item>open</item> </rule> <rule id="rulePrint" scope="public"> <item>print</item> </rule> </grammar>
The user can say either "open," or "print," and the application recognizes the commands by matching the appropriate rule identifiers.
Create recognizable sentences for user commands. These steps are covered in the following section.
Create Recognizable Sentences for User Commands
Within a rule, all the words and the sequence or pattern of the words must match for a successful recognition. When using phrases or sentences as the user commands (text or a phrase inside the item elements), developers must include all the words a user needs to say to accomplish the command or task. Developers can also separate item text or phrases into segments.
To create recognizable command sentences
Define the grammar structure for a recognizable command sentence.
Insert a rule element, and then define the ID attribute for this grammar; "ruleCoffee."
Define the spoken content for the item element using a familiar user command; "I would like a coffee." Refer to the following code sample.
<grammar root="ruleCoffee" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en-US" tag-format="semantics-ms/1.0" ... > <rule id="ruleCoffee" scope="public"> <item>I would like a coffee</item> </rule> </grammar>
The user must say the exact phrase, "I would like a coffee," for the application to match the rule and make a successful recognition.
Create a series of possible user commands. These steps are covered in the following section.
Create a Series of Possible User Commands
Often the user is presented with a list of choices; however, these additional choices do not change the basic structure of the request. It is still appropriate for a user to say "I would like a...", and only the last word changes. To start this task, use the following grammar code example, and build on it.
<grammar root="ruleCoffee" version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
xml:lang="en-US" tag-format="semantics-ms/1.0" ... >
<rule id="ruleCoffee" scope="public">
<item>I would like a</item>
<item>coffee</item>
</rule>
</grammar>
To create a series of user commands
Insert the grammar element described in the following table, after the first existing item element already defined for this grammar rule:
Element Description one-of A one-of element presents the user with a selection of choices. It is a subcontainer that can consist of nested item elements. The item elements actually contain the text or phrase that present the user with a series of command options. Nest a series of item elements within the opening and closing one-of elements.
Define the item text or phrase to comprise the series of possible user commands; coffee, mocha, latte, water. Use the following grammar code example to create a grammar structure that presents four phrases for possible user commands:
<grammar root="ruleCoffee" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en-US" tag-format="semantics-ms/1.0" ... > <rule id="ruleCoffee" scope="public"> <item>I would like a</item> <one-of> <item>coffee</item> <item>mocha</item> <item>latte</item> <item>water</item> </one-of> </rule> </grammar>
The application matches the "ruleCoffee" rule if the user says the text or phrase contained in any one of the item elements. The user can now say "I would like a coffee" or "I would like a latte," and the application recognizes either command and matches the rule.
Create variations of user commands. These steps are covered in the following section.
Create Variations of User Commands
To make the user commands more versatile, define the text or phrase for additional grammar one-of and item elements. For reuse and ease of referencing from within a containing grammar or an external grammar, encapsulate these additional sets of one-of and item elements into additional rules.
The instructions below describe in detail how to create variations for user commands, and use the code in the previous example (the grammar containing the ruleCoffee rule) as a starting point.
To create variations for user commands
Add another one-of element below the existing closing one-of element, to allow the user to modify the drink order.
Encapsulate the existing one-of element and nested item elements that define the drink types using another rule element, and then define the ID attribute for reuse and ease of reference; "drinkTypes".
Encapsulate this one-of element and nested item elements using another rule element, and then define the ID attribute to identify the coffee variations for reuse and ease of reference; "drinkVariations."
Nest a series of item elements under the start tag of the one-of element, and then define the item text or phrase using; decaf, hot, and iced. The user can now order a latte, coffee, mocha, or water with a variation.
In the ruleCoffee rule, insert an item element, and then insert the grammar element described in the following table to reference the drinkVariations rule created in Step 3:
Element Description ruleref The ruleref element imports rules from the containing grammar or an external grammar files. The referenced rule is identified by the URI attribute and specifies the rule to which it is pointing. The ruleref element is especially useful for re-using component or predefined rules and grammars. Define the URI attribute for the ruleref element created in Step 5 using the "#drinkVariations" rule identifier, to reference the rule created in Step 3.
Insert another ruleref element after the item element created in Step 5, and then define the URI attribute with the "#drinkTypes" rule identifier to reference the rule created in Step 4.
Use the following grammar code example to create a grammar structure that identifies user command variations, and enables the application to match the rule for a successful recognition:
<grammar root="ruleCoffee" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en-US" tag-format="semantics-ms/1.0" ... > <rule id="ruleCoffee" scope="public"> <item>I would like a</item> <ruleref uri="#drinkVariations"/> <ruleref uri="#drinkTypes"/> <ruleref special="GARBAGE"/> </rule> <rule id="drinkTypes" scope="public"> <one-of> <item>coffee</item> <item>mocha</item> <item>latte</item> <item>water</item> </one-of> </rule> <rule id="drinkVariations" scope="public"> <one-of> <item>decaf</item> <item>hot</item> <item>iced</item> </one-of> </rule> </grammar>
For reuse and ease of referencing the text or phrase, the drinkTypes and drinkVariations rules were created to encapsulate the series of drink types and variations commands. The grammar structure actually references the "drinkVariations" rule and "drinkTypes" rule by inserting the ruleref elements within the "ruleCoffee" rule. The application matches the "ruleCoffee" rule if the user says one of the text or phrase contained in any of the item elements, in the order defined by the placement of the ruleref elements: "I would like a decaf latte," versus saying "I would like a latte decaf."
More Information
These grammar code examples are simple progressions that demonstrate the process for designing grammars and grammar rules, and referencing grammar rules appropriately within a grammar structure. Ultimately the grammar can become complex. For a moderately complex grammar that contains several rules, see the Grammar Example: Solitaire.