Creating Grammars

  Microsoft Speech Technologies Homepage

This topic is the second of seven tutorial topics covering tools in the Microsoft Speech Application SDK Version 1.1 (SASDK). This section of the tutorial uses Speech Grammar Editor, the tool for creation and graphical representation of speech grammars.

For reference information on Speech Grammar Editor, see Enabling Speech Recognition.

This sequence of tutorials demonstrates how to build a simple voice-only ASP.NET Web application using the SASDK. Specifically, the tutorials demonstrate building a Start page of an imaginary pizza ordering service, for use by telephony Speech Application Language Tags (SALT) clients.

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

In order 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. This SASDK supports the World Wide Web Consortium Speech Recognition Grammar Specification (W3C SRGS) Version 1.0. For a detailed specification of the supported grammar format, see the W3C SRGS Format.

The grammar file is an XML file, and when saved using Speech Grammar Editor, has a .grxml extension. Speech Grammar Editor displays a graphical representation of the relationships within the grammar. The grammar can be compiled into a context-free grammar (.cfg) using the grammar compiler (SrGSGc.exe) provided.

To create a grammar in a language other than English, 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, U.S. English. For more information on developing grammars in other languages, see the topic Creating Grammar Files.

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 on 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 cursors indicate valid and invalid drop locations. Use them to determine where valid drop locations are located.

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

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

.

A grammar with rules for each of those 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.

To build a rule to recognize a pizza size

  1. In Solution Explorer, under Grammars, double-click PizzaOrder.grxml to open Grammar Explorer and display it there.
  2. In Grammar Explorer, right-click Rule1, select Rename, and then rename the rule to Size.
  3. Double-click Size to display Speech Grammar Editor.
  4. In the Toolbox click the Grammar tab to display the grammar elements.

  1. Drag a List onto the design canvas and then scroll it to the center of the canvas. The List has a single Phrase element attached to it. This first List element will contain preamble text that the customer may speak before selecting pizza size. Lists represent alternative phrases that the grammar recognizes in that position.

  2. Drag three new Phrase elements onto the canvas, and drop them in the space between the List element and the top Phrase element.

  3. Double-click each Phrase in turn, and type:

    I would like
    I'd like
    Give me
    I'll have

    into the text fields, as shown in the following illustration.

List element

  1. To complete the preamble, from the Toolbox, drag a Phrase and place it to the right of the List node, and type a.

  2. Drag another List onto the canvas and drop it to the right of the last Phrase.

  3. Drag two new Phrase elements onto the canvas, and drop them in the space between the List element and the top Phrase element.

  4. Double-click each Phrase in turn, and type:

    small
    medium
    large

    into the text fields. 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.

  5. Add two Phrase elements to the right of the last List element on the canvas.

  6. Type pizza into the next and please into the last one. This step completes the basic structure of the rule.

  7. To account for the fact that a user may or may not start speaking the size with a preamble, some of the phrases should be optional. To make them optional, right-click the first List element, and from the context menu, select 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. Set each of the other Phrase elements to optional in the same way. Do not set the List element with the sizes to optional.

The created rule should look like the following illustration:

Size rule

The rule just created recognizes phrases such as I would like a small pizza please, a medium pizza please, or large. All those phrases are natural responses to the question "What size would you like?"

Building a rule to recognize a phone number

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

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

  3. Drag a List element onto the canvas and drop it on the design canvas.

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

  5. Double-click each Phrase in turn, and type:

    It's
    My phone number is
    My number is
    My phone is

    into the text fields.

  6. Make these phrases optional by right-clicking the List element, and selecting Make Optional from the context menu. Notice the 0-1 repeat icon that appears on the element.

  7. Drag a RuleRef element onto the canvas and drop it to the right of the List. A RuleRef element inserts the functionality of an existing rule into a grammar. In this case, the grammar will refer to a rule from Library.grxml, the default voice mode grammar library.
    Library.grxml has a number of commonly used rules that applications can refer to. See the Voice Mode Grammar Library topic for more information.

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

  9. In the Projects 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 USPhoneNumber, and click Set Target Rule. The USPhoneNumber rule recognizes phone numbers used in the United States of America, optionally including international dialing prefixes and area codes, and always including local seven-digit numbers.

The created rule should look like the following illustration:

PhoneNumber rule

This rule recognizes 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, or five seven five five five five oh one two three. All those 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 confirmation rule

  1. In Grammar Explorer, right-click PizzaOrder.grxml, select New Rule, and then rename the rule to Confirm.
  2. In the Toolbox click the Grammar tab to display the grammar elements.
  3. Drag a List onto the canvas and drop it on the design canvas.
  4. Delete the Phrase element from the List. The List element now has the label <EMPTY LIST>.
  5. Drag two RuleRef elements onto the canvas and drop them on the design canvas under the List element to make them a part of the list.
  6. Right-click the first RuleRef element, and from Set Target Rule on the context menu, select Browse.
  7. In the Projects pane, click Grammars, to display the grammars included in the project in the contents pane.
  8. Click Library.grxml, and then click OK. The Rule Browser appears.
  9. In the list of rules, select Yes, and click Set Target Rule.
  10. Repeat steps 6 through 9 to set the second RuleRef element to refer to the No rule in the default grammar.
  11. Save the grammar file.

The created rule should look like the following illustration:

Confirm rule

This rule recognizes phrases such as yes, yes, please, yeah, affirmative, no, negative, or no, I don't think so. All those phrases are natural responses to the prompt "Is that correct?"

Building a cancellation rule

  1. In Grammar Explorer, right-click PizzaOrder.grxml, select New Rule, and then rename the rule to Cancel.
  2. Drag a RuleRef onto the canvas and drop it on the design canvas.
  3. Right-click the RuleRef element, and from Set Target Rule on the context menu, select Browse.
  4. In the Projects pane, click Grammars, to display the grammars included in the project in the contents pane.
  5. Click Library.grxml, and then click OK. The Rule Browser appears.
  6. In the list of rules, select Cancel, and click Set Target Rule.
  7. Save the grammar file.

The created rule should look like the following illustration:

Cancel rule

This rule recognizes phrases such as Cancel and Stop.

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 invalid. To ensure that grammar files are valid, use the tools described in Validating Grammars. It is possible to check an individual rule or to validate the entire grammar.

Manually Testing a Grammar Using the Recognition String Window

At the top of the Rule Editor window is a Recognition string textbox. Type a sample sentence that a user might speak into this textbox, and determine if the rule will recognize it.

Recognition String Window

To validate individual sentences

  1. Double-click the Size rule in Grammar Explorer.

  2. Click the Recognition string text box and type:

    I'd like a small pizza please

Caution  Punctuation must match in the grammar and the recognition string—a comma between pizza and please, for instance, causes the recognition to fail.

  1. 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, as shown in the following illustration:

recognition string

To validate a grammar file

  1. Select PizzaOrder.grxml in Grammar Explorer.
  2. On the Grammar menu, select Validate Grammar. (Make sure that any previous validation path in any displayed rule is not still selected.)

The Output window displays the validation results, as shown in the following illustration:

Output window

The validation runs in three passes. First, Speech Grammar Editor checks for valid references in RuleRef elements. In the second pass the grammar parser checks for W3C compliance. In the third pass Speech Grammar Editor attempts to compile the file. If no errors are found, the SR engine validates the grammar. Any errors appear in the Output window along with a validation-completed message.

To See
Go to the next step Creating the Dialogue Framework
Start from the beginning Creating a Speech Project
Get more information on grammars Enabling Speech Recognition
Get more information on validating grammars Validating Grammars