Creating Grammar Files

  Microsoft Speech Technologies Homepage

The Microsoft Speech Application SDK Version 1.1 (SASDK) provides Speech Grammar Editor to help simplify and expedite the process of creating grammars. Speech Grammar Editor uses connected, graphical elements to represent the grammar XML markup that defines grammar rules. Although grammar authors can create grammars manually using a plain text editor such as Notepad, this approach can become tedious, complex, and more prone to error when creating long grammar files or sets of grammar files. The graphical approach implemented by Speech Grammar Editor enables grammar authors to concentrate on creating the actual grammar rules instead of laborious tasks such as verifying proper element syntax.

Speech Grammar Editor is tightly integrated with Visual Studio .NET 2003, and runs completely within the Visual Studio .NET 2003 development environment.

  • Creating a New Grammar File
  • Working with Grammar Files
  • Working with Grammar Rules
  • Renaming Grammar Files and Rules
  • Adding Grammar Files to a Speech Project

Creating a New Grammar File

Grammar files created with Speech Grammar Editor conform to the World Wide Web Consortium Speech Recognition Grammar Specification Version 1.0 (W3C SRGS). These grammar files use the .grxml extension.

To create a new grammar file

  1. On the File menu, click New, and then click File.
  2. In the New File dialog box, in the Categories pane, select Speech Files.
  3. In the Templates pane, double-click the Grammar File icon.

The new grammar file name appears in Grammar Explorer, with a grammar rule name expanded in tree view under the grammar file. The new grammar file name is Grammar1, and the new rule name is Rule1. The new grammar rule opens in Rule Editor with a label of Grammar1 - Rule1.

New grammar file names appear as Grammarn, such as Grammar1, Grammar2, and Grammar3. New grammar rule names appear as Rulen, such as Rule1, Rule2, and Rule3. The names of all grammar files in a project appear in a tree view in Grammar Explorer, with all grammar rules expanded in tree view under their respective grammar files. Rename grammar files and rules in Grammar Explorer.

Because a grammar file is an XML file, grammar authors can create a grammar file by typing code into a text editor such as Notepad, and saving the file with a .grxml extension. The following code example illustrates sample template code.

  <grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
 lang="en-US" tag-format="semantics-ms/1.0">
    <rule id="Rule1"></rule>
</grammar>

Note  When a .grxml file that contains the previous sample template code loads and opens in Speech Grammar Editor, the Output window displays the message "Incomplete properties on 'Rule1' RULE element: Empty rule". This message does not indicate that the grammar file did not load or open correctly. It only indicates that the file contains an empty rule.

Creating Dynamic Grammars

In a dynamic grammar, rules are built at run-time based on data from the environment. Use a dynamic grammar to present a list of choices that change based on data like the time of day, or a list of services that are currently available. For example, the Contacts sample application uses the list of available services to build a dynamic grammar.

Dynamic Personalized Grammars

A dynamic personalized grammar is a special case of dynamic grammars, where the dynamic grammar is personalized based on some information about the user, or what the user has done or said during the current call. This information might be stored in session state on the Web server, or might be maintained in a database, or by some other means. A Stock Checker by Phone application is an example of an application that uses dynamic personalized grammars. If a user calls into a Stock Checker by Phone application and asks for their portfolio, the application creates a dynamic personalized grammar based on the list of stocks in the user's account.

Dynamic Personalized Grammars in Speech Engine Services (SES)

In SES session state is not useful, so the personalized data has to come from somewhere else. See the Banking Alerts reference application for an example of one way to create dynamic grammars in SES. In Banking Alerts, the list of bills presented to the user is an example of a dynamic personalized grammar. An .aspx page is loaded on the Web server using a URL that contains two query parameters, as shown in the following example.

  https://localhost/BillsGrammar.aspx?UserID=1&BillsCollectionID=2

In this example, the two query parameters contain the session state data needed to build the dynamic grammar.

Selecting a Language for a Grammar

Speech recognition grammars in the SASDK support any of the installed languages. Selecting a language in the Speech Application Project Wizard specifies the language for the initial grammar files. For more information see the topic Using the Project Wizard to Set Application Language. To select the language for a grammar file after running the Speech Application Project Wizard, or to change the language of an existing grammar file, set the Language property for the file. If no language is selected, the default, English(United States), is used.

When grammar files are validated, or when single sentences in a grammar are validated using the Recognition string text box, the validation is performed by the speech recognition engine mapped to the language for the grammar file. For information on selecting speech recognition engines, see the topic Languages and Speech Engines, Options Dialog Box.

Working with Grammar Files

To open an existing grammar file

  1. On the File menu, click Open, and then click File.
  2. In the Open file dialog box, browse to the .grxml file, and then click Open.

The grammar file opens in Grammar Explorer. Only grammar files with the .grxml extension will open in Grammar Explorer.

When a grammar file that does not conform to the W3C SRGS opens, a warning appears, and the unrecognized elements are removed. If the file contains incorrectly formed XML, it is opened as text by the XML editor in Visual Studio .NET 2003.

Grammar files created using Speech Grammar Editor end with the .grxml extension. However, because grammar files are valid XML documents, grammar authors can create grammar files in a plain text editor with the .xml extension. Open .xml grammars in the Visual Studio .NET 2003 XML editor or Speech Grammar Editor.

To open a grammar file with the .xml extension in the Visual Studio .NET 2003 XML editor

  • In Solution Explorer, double-click the file.

To open an existing .grxml file as XML text

If the .grxml file is in a Visual Studio .NET 2003 project

  1. Right-click the file in Solution Explorer, and then click Open With.
  2. In the Open With dialog box, click HTML/XML Editor as the program to open the file, and click Open.

otherwise

  1. On the File menu, click Open, and then click File.
  2. In the Open File dialog box, click the .grxml file, click the down arrow on the Open button, and then click Open With.
  3. In the Open With dialog box, select HTML/XML Editor, and then click Open.

To close a .grxml file

  • In Grammar Explorer, right-click a .grxml file, and click Close.
  • - or -
  • On the File menu, click Close Solution.

Working with Grammar Rules

To add a new rule to a grammar

  • In Grammar Explorer, right-click a .grxml file, and then on the shortcut menu, click New Rule.

To add a description to a rule

It is useful to add descriptions to rules so that it is easy to maintain and reuse grammars and their rules. Without a description, it is sometimes difficult to determine what a rule does without working through the graphical representation or the XML representation. Descriptions also make it easier for other users to reuse rules.

  1. Select a rule in Grammar Explorer.
  2. In the Rule Properties pane, enter a description in the Description property field.

To open a rule in Rule Editor

  1. Open an existing grammar file or create a new grammar file.
  2. In Grammar Explorer, double-click a rule.

Each grammar rule appears in a separate window in Rule Editor.

Note  A large rule may not be completely visible all at once in Rule Editor. Use either of the following methods to scroll the Rule Editor window.

  • Use the scrollbars on the right and bottom edges of Rule Editor.
  • - or -
  • Hold down CTRL and click the right mouse button. A scroll icon appears in Rule Editor. Move the mouse to scroll the window.

Activating Rules

A grammar file contains a single rule that is loaded upon activation of a control, such as a Speech QA (QA) control. This rule defines the top-level syntax of spoken input.

The single rule that is loaded upon activation of a control is designated as either an active rule or a root rule. Although both types of rules define the same thing, they are used in different situations.

  • Use an active rule when the grammar contains more than one rule that can define the top-level syntax of spoken input. For example, if two QAs use different rules in the library grammar file (Library.grxml) to define top-level syntax, designate an appropriate rule as the active rule for each control.
  • Use a root rule when the grammar contains only one rule that can define the top-level syntax of spoken input. For example, when referencing a custom grammar that always uses the same rule to define top-level syntax, designate that rule as the root rule.

A rule is designated as an active rule or a root rule when using Speech Control Editor to associate an existing speech or DTMF grammar with a control.

To delete a rule from a grammar

  • In Grammar Explorer, right-click a rule, and then on the shortcut menu, click Delete.

When a grammar file closes, Grammar Explorer remains open. When Visual Studio .NET 2003 closes and then reopens, Grammar Explorer remains open but is behind Solution Explorer. Click the Grammar Explorer tab to bring Grammar Explorer into focus.

Renaming Grammar Files and Rules

To rename a grammar file

  1. In Grammar Explorer, right-click the file name, and then on the shortcut menu, click Save GrammarName As.
  2. On the Save File As dialog box, type a file name, and then click Save.

To rename a grammar rule

  • In Grammar Explorer, right-click the rule name, and then on the shortcut menu, click Rename.
  • - or -
  • In Grammar Explorer, select the rule name, and then in the Properties window, type a new value for the Name property.
  • - or -
  • Click the rule name text field, click the field again, and then type a new name.
  • - or -
  • Click the rule name text field, and press F2.

Adding Grammar Files to a Speech Project

A speech project created using Visual Studio .NET 2003 is essentially a standard ASP.NET Web project that contains speech-related components such as speech controls, speech class libraries, and other components. Developers add these speech-related components to a standard ASP.NET Web project manually to create a speech project. Because standard ASP.NET Web projects do not automatically contain speech-related components, they also do not automatically contain grammar files. Because a speech application must contain at least one grammar file to recognize user speech, developers must manually add grammar files to projects.

Create grammar files in Visual Studio .NET 2003 and add them to a speech project using any of the following methods.

  • Create grammar files independent of a speech project, and then add the files to a project later.
  • Create grammar files while a speech project is open in Visual Studio .NET 2003, and then add the files to that project while it is still open.
  • Create grammar files while a speech project is open in Visual Studio .NET 2003, and then add the files to that project or another project later.

To add an existing grammar file to a speech project

  1. Open a project in Visual Studio .NET 2003.
  2. In Solution Explorer, right-click the project name, on the shortcut menu point to Add, and then click Add Existing Item.
  3. On the Add Existing Item dialog box, browse to the location of the existing grammar file, and then double-click the appropriate grammar file.
  4. On the File menu, click Save ProjectName to save the project.
  5. - or -
  6. On the Standard toolbar, click Save.

To add a new grammar file to a speech project

  1. Open a project in Visual Studio .NET 2003.
  2. In Solution Explorer, right-click the project name, on the shortcut menu point to Add, and then click Add New Item.
  3. On the Add New Item dialog box, in the Categories pane, click Web Project Items. On the Name text box, optionally type a new name for the grammar file, and then in the Templates pane, double-click Grammar File.
  4. On the File menu, click Save ProjectName to save the project.
  5. - or -
  6. On the Standard toolbar, click Save.

Adding a new grammar file

Note  If a grammar author or developer does not save the project after adding a grammar file to it, the project will not contain the grammar file the next time it opens in Visual Studio .NET 2003.

See Also

Enabling Speech Recognition | Creating Grammars | Grammar Design