Create an XML Grammar Structure (Microsoft.Speech)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

When you have identified the spoken input that your application will accept, you can enter the words and phrases into an XML grammar structure. This topic shows you how to build a basic XML grammar structure.

The structure of XML-format grammars is defined by the Speech Recognition Grammar Specification (SRGS) Version 1.0. For this example, we will use a basic SRGS-compliant grammar structure, beginning with the grammar element.

The grammar element is the root element of an SRGS grammar. All of the content of an SRGS grammar is contained within the opening and closing tags of the grammar element. See grammar Element (Microsoft.Speech) for more information. For this example, we will specify the following attributes of the grammar element:

  • version. Required identifier that specifies the version of the SRGS specification.

  • xml:lang. Required attribute in voice-mode grammars that specifies the language for the content of the grammar.

  • root. Optional attribute that specifies the name of the rule that is active when the grammar is loaded by a speech recognition engine. Rules that are not the root rule or that are not referenced by the root rule cannot be used for recognition. For this reason, the root rule often contains references to other rules that must be active when the grammar loads.

  • xmlns. Required attribute that specifies the XML namespace.

Using a text editor, enter the grammar element, using the attributes described above with some predefined values:

XML Copy imageCopy Code
<grammar version="1.0" xml:lang="en-US" root="rootRule"
 xmlns="http://www.w3.org/2001/06/grammar">

</grammar>

Now we are ready to add a rule to the grammar. Each grammar must contain at least one rule. A grammar rule contains a word or phrase that a user can speak. See rule Element (Microsoft.Speech) for more information. The rule element has the following required attribute:

  • id. Required attribute that specifies a unique identifier for the rule. You can reference a grammar rule by using its id attribute.

Add the rule element to the grammar example, and set the value of its root attribute to rootRule:

XML Copy imageCopy Code
<grammar version="1.0" xml:lang="en-US" root="rootRule"
 xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="rootRule">
  </rule>

</grammar>

The rule  rootRule will be active when the grammar loads. At this point, the grammar cannot be used for recognition because it does not define any words that a user can speak. For this example, we will add a word to the rule element within an item element. An item element specifies a word or a phrase that a user might say. See item Element (Microsoft.Speech) for more information. A rule element may also contain a word or phrase as unmarked text.

XML Copy imageCopy Code
<grammar version="1.0" xml:lang="en-US" root="rootRule"
 xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="rootRule">
    <item> play </item>
  </rule>

</grammar>

The example is now a complete grammar that can be used to recognize the word "play", for example to initiate playback of a selected audio file in your music player application. See the next topic, Create Variations of User Commands (Microsoft.Speech), to learn how to create rules that can match a variety of spoken input.

See Also