A Simple Grammar

The fastest way to learn SRGS is to write a grammar. (If you have not already done so, see the SRGS Introduction for background on this tutorial.)

We will begin our look at writing SRGS grammars with a simple grammar that lets the Engine recognize the words "yes" or "no." Yes/no grammars are the "hello world" of grammar writing.

Example

ABNF format:

#ABNF 1.0 UTF-8;
language en-US; //use the American English pronunciation dictionary.
mode voice; //the input for this grammar will be spoken words.

root $yesorno;

$yes = yes;
$no = no;
$yesorno = $yes | $no;

Equivalent grammar in GrXML format:

<?xml version="1.0" encoding="UTF-8" ?>
<grammar xmlns="http://www.w3.org/2001/06/grammar"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.w3.org/2001/06/grammar
                             http://www.w3.org/TR/speech-grammar/grammar.xsd"
         xml:lang="en-US" version="1.0"
         root="yesorno"
         mode="voice"
         tag-format="semantics/1.0.2006">

         <rule id="yes">
               <item>yes</item>
         </rule>

         <rule id="no">
               <item>no</item>
         </rule>

         <rule id="yesorno">
               <one-of>
                     <item><ruleref uri="#yes"/></item>
                     <item><ruleref uri="#no"/></item>
               </one-of>
         </rule>
</grammar> 

The Grammar Identifier

Any SRGS grammar written in ABNF notation must begin with a line like:

#ABNF 1.0 UTF-8;

This identifies to the grammar compiler that the file being read is an ABNF grammar. Immediately following the grammar type is an optional declaration that indicates the character encoding — e.g. UTF-8, UTF-16, or ISO-8859-1. The line ends with a semicolon, as do all lines in an ABNF grammar.

By contrast, an SRGS GrXML grammar must begin with an XML prolog element followed by a grammar element:

<?xml version="1.0" encoding="UTF-8" ?>
<grammar ...>

...

</grammar>

Immediately following the XML version is an optional declaration that indicates the character encoding — e.g. UTF-8, UTF-16, or ISO-8859-1.

The Grammar Header

Following the identifier, a well-formed grammar contains information about the language the grammar is written in, the expected interaction mode (voice or DTMF), and the name of the rule where the Engine begins its search (the root rule). The header may also contain one or more tags and an identifier describing the tag format for the grammar. Tags are discussed later in this tutorial.

The contents of the grammar header may be in any order, but no header data may occur after the first rule is written. The Speech Engine requires only the identifier line in the header; if interaction mode, language, or tag format are left blank, the ASR Engine uses default values — voice for the interaction mode, en-US for the language, and semantics/1.0 for the tag format. If no root rule is specified, all rules are treated as root, meaning the grammar is matched if any rule is matched.

It is good practice to always assign the header information explicitly rather than relying on the defaults.

Comments

ABNF grammars may contain comments anywhere in the body except the first line (the grammar identifier). The comment format is the same as the C, C++, and Java programming languages.

GrXML grammars, being XML, support comments in the form <!-- comment -->.

Rules

A grammar's rules specify which words and combinations of words the ASR Engine can recognize. They are the heart of the grammar.

In ABNF format, each rule has a name on the left side of an = sign and a rule expansion on the right. A rule name starts with a $ character, immediately followed by the rule's name, which must start with a letter and may be followed by additional letters, numbers, or underscores. The first rule in the ABNF grammar above is:

$yes = yes;

In GrXML format, a rule is defined within a rule element containing one or more item child elements (one per word or phrase). The first rule in the GrXML grammar above is:

<rule id="yes">
<item>yes</item>
</rule>

The rule expansion describes which sequences of words will match the rule. In the rule above, the expansion is simply the word "yes," so the rule is matched when "yes" is spoken. An entire grammar is matched only if its root rule is matched.

The second rule is matched if the word "no" is detected.

In ABNF format, the third rule ($yesorno) contains a pipe symbol (|), a logical "or" operator, so it is matched if either the $yes rule or the $no rule is matched.

In GrXML format, the third rule (yesorno) contains a <one-of> element with items containing <ruleref> references to the "yes" or "no" rules, allowing either one to be matched.

How the Speech Engine Uses a Grammar

When the ASR Engine begins decoding your audio, it starts at the root rule of the grammar (here, $yesorno) and steps through all legal expansions. It moves into the $yes and $no rules, since it can match against either. Because the first words in those rules are "yes" and "no," the Engine knows it is allowed to recognize either word.

If the Engine detects "yes" as a possibility, it looks for the next word it can recognize in the $yes rule. Since there are no more words in that rule, the rule is matched — and because the $yes rule is matched, the $yesorno root rule is matched, so the entire grammar is matched.

Building more complex rules via rule expansions is the next step in this SRGS tutorial.


Related Articles


Was this article helpful?