Creating Grammars

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 second of seven tutorial topics covering tools in Speech Server. This section of the tutorial uses Speech Grammar Editor, the tool for the creation and graphical representation of speech grammars.

For reference information about Speech Grammar Editor, see Use Speech Grammar Editor to Create Grammars.

This sequence of tutorials demonstrates how to build a simple Web-based voice response application using Speech Server Developer Tools. Specifically, the tutorials demonstrate building a Start page of an imaginary pizza ordering service for use by a telephony Speech Application Language Tags (SALT) client.

The procedures for creating the pizza ordering service application build on each other. Performing the procedures in sequence is therefore important.

Building a Grammar for the Project

For the speech recognition (SR) engine to return a recognition from audio input, a grammar must be added to each Speech Control. In simple terms, a grammar is a representation of everything the user can be expected to say, with certain selections tagged with semantic properties and values. Speech Server Developer Tools support the World Wide Web Consortium Speech Recognition Grammar Specification (W3C SRGS) Version 1.0. For a detailed specification of the supported grammar format, see W3C SRGS Format.

The grammar file is an XML file, and when saved using Speech Grammar Editor, has a .grxml file name extension. Speech Grammar Editor displays a graphical representation of the relationships within the grammar.

To create a grammar in a language other than English (United States), set the Language property for the grammar file to any of the installed languages. This tutorial develops a speech application and grammar files using the default language setting, which is English (United States). For more information about developing grammars in other languages, see Recognizing and Prompting in Multiple Languages.

Authoring the Grammar

Grammar files include one or more rules. Double-click a grammar file in Solution Explorer to display it in Grammar Explorer. Double-click a rule in Grammar Explorer to display it in the Rule Editor window, where you can add, delete, and manipulate grammar recognition elements. Add grammar elements to a rule by dragging them from the Grammar tab in the Toolbox.

Tip

Experiment with drag and drop in the Rule Editor window by placing a List element onto the canvas, and then dragging a second element around on the canvas. Note that the cursor indicates valid and invalid drop locations.

In the Pizza Ordering application, the application welcomes the caller, and then prompts the caller for information in the following order:

  1. Size of the pizza
  2. Telephone number for identification

A grammar with rules for each of these cases is necessary to recognize the responses that a caller might make to those prompts. The prompts should contain enough information to constrain the caller's responses, and the grammars should contain enough elements to recognize common phrases that a caller might naturally add to the response.

The following steps create a rule to recognize phrases such as:

  • "I would like a small pizza please"
  • "a medium pizza please"
  • "large"

These phrases are natural responses to the question "What size would you like?".

To build a rule to recognize a pizza size

  1. In Solution Explorer, double-click PizzaOrder.grxml under Grammars to open Grammar Explorer.

  2. In Grammar Explorer, right-click Rule1, click Rename, and then rename the rule to Size.

  3. Double-click Size to display Speech Grammar Editor.

  4. Click the Toolbox tab to display the grammar elements.

    You can click the pin icon at the top of the Toolbox tab to keep it visible.

  5. Drag and drop a List element on the center of the design canvas.

    The List element has a single Phrase element attached to it. This first List element contains preamble text that the customer might speak before selecting a pizza size. List elements represent alternative phrases that the grammar recognizes in that position.

  6. Drag and drop three new Phrase elements onto the canvas between the List element and the top Phrase element.

  7. Double-click each Phrase element in turn, and then type the following text in the text boxes:

    • I would like
    • I'd like
    • Give me
    • I'll have
  8. To complete the preamble, drag and drop Phrase element to the right of the List node, and then type a.

  9. Drag and drop another List element onto the canvas to the right of the last Phrase element.

  10. Drag and drop two new Phrase elements onto the canvas between the List element and the top Phrase element.

  11. Double-click each Phrase element in turn, and then type the following in the text boxes:

    • small
    • medium
    • large

    This is the information that the application needs to recognize and capture. A subsequent section of the tutorial adds the tags to capture the spoken phrase.

  12. Drag and drop two Phrase elements onto the canvas to the right of the last List element.

  13. Type pizza in the first Phrase element and please in the second Phrase element.

    This step completes the basic structure of the rule.

  14. To make some of the phrases optional, right-click the first List element, and then click Make Optional.

    This sets the Max Repeat property to 1 and Min Repeat to 0 in the Property pane. Notice the 0-1 repeat icon that appears on the element.

    Because a user might start speaking the size without a preamble, some of the phrases should be optional.

  15. Set the other Phrase elements to optional in the same way.

    Do not set the List element with the sizes to optional.

The following steps create a rule to recognize phrases such as:

  • "It's five one four five five five oh nine zero nine"
  • "My number is seven one four five five five zero two zero zero"
  • "five seven five five five five oh one two three"

All these phrases are natural responses to the prompt "Please say your phone number, area code first."

Note

When later validating the grammars with text input in the Recognition String window, remember that the grammars recognize the spoken or text representations of the numbers, not the numeric representations (for example, four, not 4).

To build a rule to recognize a phone number

  1. In Grammar Explorer, right-click PizzaOrder.grxml, click New Rule, and then rename the rule to PhoneNumber.

  2. In the Toolbox, click the Grammar tab to display the grammar elements.

  3. Drag and drop a List element onto the canvas.

  4. Drag and drop three new Phrase elements onto the canvas between the List element and the top Phrase element in the list.

  5. Double-click each Phrase element in turn, and then type the following in the text boxes:

    • It's
    • My phone number is
    • My number is
    • My phone is
  6. Make these phrases optional by right-clicking the List element, and then clicking Make Optional.

    Notice the 0-1 repeat icon that appears on the element.

  7. Drag and drop a RuleRef element onto the canvas to the right of the List element.

    A RuleRef element inserts the functionality of an existing rule into a grammar. In this case, the grammar refers to a rule from Library.grxml, which is the default voice mode grammar library. Library.grxml has a number of commonly used rules to which applications can refer. For more information, see Voice Mode Grammar Library for English (United States).

  8. Right-click the RuleRef element, point to Set Target Rule, and then click Browse.

  9. In the Project folders pane, click Grammars to display the grammars included in the project in the contents pane.

  10. Click Library.grxml, and then click OK.

    The Rule Browser appears.

  11. Scroll down in the list of rules, select PhoneNumber_US_AreaCode, and then click Set Target Rule.

    The PhoneNumber_US_AreaCode rule recognizes a three-digit area codes used in the United States.

  12. Repeat steps 7 through 11, except select PhoneNumber_US_LocalNumber.

    The PhoneNumber_US_LocalNumber rule recognizes a seven-digit local phone number used in the United States.

The following steps create a rule to recognize phrases such as:

  • "yes"
  • "yeah"
  • "no"
  • "nope"

All these phrases are natural responses to the prompt "Is that correct?"

To build a confirmation rule

  1. In Grammar Explorer, right-click PizzaOrder.grxml, click New Rule, and then rename the rule to Confirm.

  2. In the Toolbox, click the Grammar tab to display the grammar elements.

  3. Drag and drop a List onto the canvas.

  4. Delete the Phrase element from the List element.

    The List element now has the label <EMPTY LIST>.

  5. Drag and drop two RuleRef elements onto the canvas under the List element, one below the other, to make them part of the list.

  6. Right-click the first RuleRef element, click Set Target Rule, and then click Browse.

  7. In the Project folders pane, click Grammars in the contents pane to display the grammars included in the project.

  8. Click Library.grxml, and then click OK.

    The Rule Browser appears.

  9. In the list of rules, select Confirmation_Yes, and then click Set Target Rule.

  10. Repeat steps 6 through 9 to set the second RuleRef element to refer to the Confirmation_No rule in the default grammar.

  11. Save the grammar file.

To build a cancellation rule

  1. In Grammar Explorer, right-click PizzaOrder.grxml, click New Rule, and then rename the rule to Cancel.

  2. Drag and drop a RuleRef element onto the canvas.

  3. Right-click the RuleRef element, click Set Target Rule, and then click Browse.

  4. In the Project folders pane, click Grammars in the contents pane to display the grammars included in the project.

  5. Click Library.grxml, and then click OK.

    The Rule Browser appears.

  6. In the list of rules, select Confirmation_Cancel, and then click Set Target Rule.

  7. Save the grammar file.

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. To ensure that grammar files are valid, use the tools described in How to: Validate a .grxml Grammar File. It is possible to check an individual rule or validate the entire grammar.

Manually Testing a Grammar Using the Recognition String Window

At the top of the Rule Editor window is a Recognition box. Type a sample sentence that a user might speak into this box and determine whether the rule recognizes it.

To validate individual sentences

  1. In Grammar Explorer, double-click the Size rule.

  2. In the Recognition string box, type I'd like a small pizza please.

    Warning

    Punctuation in the grammar and the recognition string must match (for example, a comma between pizza and please causes the recognition to fail).

  3. Click Check.

The recognition path is selected in the Size rule in the Rule Editor window, and the generated Speech Markup Language (SML) elements appear in the Output window.

To validate the entire grammar

  1. In Grammar Explorer, select PizzaOrder.grxml.

  2. On the Grammar menu, click Validate Grammar.

    Ensure that any previous validation path in any displayed rule is not selected.

The Error List window displays error or warning messages about the grammar. These messages also appear in the Output window along with a validation-completed message.

The warning messages indicate areas where the grammar might be revised for better performance. In this case, there are two warnings about Phrase elements. These warnings refer to optional elements one after the other, both preceding and following the size List element. In some cases, a structure like this recognizes unlikely utterances. In this case, for example, recognizing "I'd like a large, please," "I'd like a large pizza," "I'd like a large pizza, please," and "I'd like a large" are all likely utterances.

To See

Go to the next step

Creating the Dialog Framework

Start from the beginning

Creating a Speech Project

Get more information about grammars

Use Speech Grammar Editor to Create Grammars

Get more information about validating grammars

How to: Validate a .grxml Grammar File

See Also

Other Resources

Voice Mode Grammar Library for English (United Kingdom)
Voice Mode Grammar Library for French (Canada)
Voice Mode Grammar Library for Spanish (United States)