Last reviewed: 3/23/2024 8:40:41 AM

Microsoft SAPI 5 Speech Recognition XML Grammar

Microsoft SAPI 5 specifies a context-free grammar structure and grammar rule format using Extensible Markup Language (XML). A grammar compiler transforms the XML grammar into a SAPI 5 binary format for SAPI 5 compliant speech recognition engines.

A SAPI 5 grammar text file is comprised of XML elements and attributes that express one or more rules (i.e., recognizable utterances). An example SAPI5 compatible grammar file of rules is as follows:

<GRAMMAR LANGID="409">
   <RULE NAME="aRule" TOPLEVEL="ACTIVE">
      <L>
         <P>WORD</P>
         <P>OR PHRASE</P>
         <P><RULEREF NAME="anotherRule"/></P>
         <P>PHRASE WITH <RULEREF NAME="aDynamicRule"/></P>
      </L>
   </RULE>
   <RULE NAME="anotherRule">
     <L>
         <P>ANOTHER PHRASE</P>
         <P>AN ADDITIONAL PHRASE</P>
         <P>ANOTHERWORD</P>
      </L>
   </RULE>
   <RULE NAME="aDynamicListRule" DYNAMIC="TRUE">
         <P>aPlaceHolderForSubstitutionAtRuntime</P>
   </RULE>
</GRAMMAR>

The outermost tags, <grammar></grammar>, define the bounds of the grammar. You specify the language of your grammar in the grammar tag. Within the grammar, you define one or more rules using the <rule></rule> tags. At least one rule should be designated TOPLEVEL="ACTIVE" indicating that this rule is activated when the grammar is enabled. The EnableResource method enables all top level rules. You may have additional TOPLEVEL rules and subordinate rules defined in the grammar.

Each rule must have one or more phrases defined using the <p> tags. You can define a list of alternate phrases using the <l></l> tags.

You reference subordinate rules using the <ruleref name="rulename"></ruleref> tag.

A question mark (?) preceding a word indicates that the word is optional.

To substitute a dynamic list of words or phrases, define a rule with the DYNAMIC="TRUE" property and a placeholder phrase.

Refer to the Microsoft Speech SDK 5.x for more information about SAPI 5 grammars and grammar compiler.

The following topics provide a quick reference for SAPI 5 speech recognition grammar format: