Grammar Example: Solitaire
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Grammar rules define sentence contents and phrase elements. Each grammar specified in a grammar element determines the speech recognition (SR) engine's ability to effectively construct phrases. Phrases and subexpressions are commonly represented by a separate rule and combined into larger phrases and sentences by higher level rules. For more information, see Grammars Overview.
The card game, solitaire, uses semantic objects such as cards, suits, and ranks and uses semantic actions, such as "move SomeCard to AnotherCard" and "new game." The following example illustrates how to implement a grammar for a game of solitaire that supports the previously mentioned semantic objects and actions. The example also specifies the exact voice command phrases, in English (United States), that must be spoken to play the game.
Example
<!-- The grammar tag surrounds the entire CFG description.
The language of the grammar is specified as English-American ('en-US') -->
<grammar root="toplevel" version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
xml:lang="en-US" tag-format="semantics-ms/1.0">
<!-- Define a top-level rule to reference all of the rules
that need to be active, as soon as speech
is activated in the application -->
<!-- $toplevel.Newgame : integer (in the case of NewGame)
$toplevel.Card : $Card (in the case of PlayCard)
$toplevel.FromCard : $Card (in the case of MoveCard)
$toplevel.ToCard : $Card (in the case of MoveCard) -->
<rule id="toplevel" scope="public">
<one-of>
<item> <ruleref uri="#NewGame"/> <tag> $.NewGame = $$ </tag> </item>
<item> <ruleref uri="#PlayCard"/> <tag> $ = $$ </tag> </item>
<item> <ruleref uri="#MoveCard"/> <tag> $ = $$ </tag> </item>
</one-of>
</rule>
<!-- $NewGame._value : string ( "NewGame" ) -->
<rule id="NewGame" scope="public">
<example> new game </example>
<example> new game please </example>
new game
<tag> $._value = "NewGame" </tag>
<!-- Make the last word, please, optional,
to make the command phrasing more flexible -->
<item repeat="0-1"> please </item>
</rule>
<!-- Define 'PlayCard' as a public rule, so that
it can be used to create other solitaire or
card game grammars-->
<!-- $PlayCard.Card = $Card -->
<rule id="PlayCard" scope="public">
<example> please play the red queen </example>
<example> play the ace </example>
<item repeat="0-1"> please </item>
play the
<!-- Allow for extraneous garbage words
from the user. The user could say
"play the little ace of spades" without
breaking the voice command -->
<item repeat="0-1">
<ruleref special="GARBAGE"/>
</item>
<ruleref uri="#Card"/>
<tag> $.Card = $$ </tag>
<item repeat="0-1"> please </item>
</rule>
<!-- Define another public rule to be included in the top-level voice
commands for moving one card to another location.
Note that phrase structure allows for two
types of move-commands by making the ToCard
section optional. The grammar supports
both 'move FromCard to ToCard' and
simply 'move FromCard'. -->
<!-- $MoveCard.FromCard : Card
$MoveCard.ToCard : Card (optional) -->
<rule id="MoveCard">
<example> please move the red queen </example>
<example> put the ace </example>
<example> move the queen of clubs to the black four please </example>
<item repeat="0-1"> please </item>
<one-of>
<item> move </item>
<item> put </item>
</one-of>
the
<ruleref uri="#Card"/>
<!-- The semantic object returned from the Card rule will be stored
under the slot FromCard. By
using semantic properties, the application
can abstract away the exact phrase text
and build application logic based on
the intended action (that is, Action=
moveFromCard=(Rank=Ace, Suit=Hearts) -->
<tag> $.FromCard = $$ </tag>
<item repeat="0-1">
<one-of>
<item> on </item>
<item> to </item>
</one-of>
the
<!-- The semantic object returned from the Card rule this time will
be stored under the slot ToCard. -->
<ruleref uri="#Card"/>
<tag> $.ToCard = $$ </tag>
</item>
<item repeat="0-1"> please </item>
</rule>
<!-- Create a reusable Card grammar that contains
the structure of a card's descriptor (for example,
"red ace", "ace of hearts", or "heart").
The application can decode the card by analyzing the
semantic object from the Rank, Suit, or the Color
slots. -->
<!-- $Card.Color : $Color (optional)
$Card.Suit : $Suit (optional)
$Card.Rank : $Rank (optional) -->
<rule id="Card">
<example> red queen </example>
<example> jack of clubs </example>
<example> ace </example>
<example> spade </example>
<one-of>
<item>
<!-- color and rank form -->
<ruleref uri="#Color"/>
<tag> $.Color = $$ </tag>
<ruleref uri="#Rank"/>
<tag> $.Rank = $$ </tag>
</item>
<item>
<!-- rank and optional suit form -->
<ruleref uri="#Rank"/>
<tag> $.Rank = $$ </tag>
<item repeat="0-1">
of
<ruleref uri="#Suits"/>
<tag> $.Suit = $$ </tag>
</item>
</item>
<item>
<!-- suit only form -->
<ruleref uri="#Suit"/>
<tag> $.Suit = $$ </tag>
</item>
</one-of>
</rule>
<!-- Create a reusable grammar component, called
'Color' that represents the color information.
The application can use the semantic object
called 'Color', which is a string.
$Color._value : string ("Red", "Black")
-->
<rule id="Color">
<example> red </example>
<example> black </example>
<one-of>
<item> red <tag> $._value = "Red" </tag> </item>
<item> black <tag> $._value = "Black" </tag> </item>
</one-of>
</rule>
<!-- Create a reusable grammar component, called
'Suit' that represents the suit information.
The application can use the semantic object
called 'Suit', which is a string.
$Suit._value : string ("Club", "Heart", "Diamond", "Spade")
-->
<rule id="Suit">
<example> spade </example>
<example> club </example>
<one-of>
<item> club <tag> $._value = "Club" </tag> </item>
<item> heart <tag> $._value = "Heart" </tag> </item>
<item> diamond <tag> $._value = "Diamond" </tag> </item>
<item> spade <tag> $._value = "Spade" </tag> </item>
</one-of>
</rule>
<!-- Create a reusable grammar component, called
'Suits' that represents the suit information.
This rule accepts the plural form of the Suits.
$Suits._value : string ("Club", "Heart", "Diamond", "Spade")
-->
<rule id="Suits">
<example> spades </example>
<example> clubs </example>
<one-of>
<item> clubs <tag> $._value = "Club" </tag> </item>
<item> hearts <tag> $._value = "Heart" </tag> </item>
<item> diamonds <tag> $._value = "Diamond" </tag> </item>
<item> spades <tag> $._value = "Spade" </tag> </item>
</one-of>
</rule>
<!-- Create a reusable grammar component, called
'Rank' that represents the numeric rank
of the various cards. Note that each card
has an associated value.
The application can use the semantic object
called 'Rank' (an integer value between 1 to 13)
to abstract program logic from the exact phrasing of
the speech, and use only the numeric rank values.
Note that the words 'king' and 'emperor' both refer
to the value 13. The grammar author can change
or update the text without breaking the
application.
$Rank._value : integer ([1,13])
-->
<rule id="Rank">
<example> ace </example>
<example> five </example>
<example> king </example>
<example> jack </example>
<one-of>
<item> ace <tag> $._value = 1 </tag> </item>
<item> two <tag> $._value = 2 </tag> </item>
<item> three <tag> $._value = 3 </tag> </item>
<item> four <tag> $._value = 4 </tag> </item>
<item> five <tag> $._value = 5 </tag> </item>
<item> six <tag> $._value = 6 </tag> </item>
<item> seven <tag> $._value = 7 </tag> </item>
<item> eight <tag> $._value = 8 </tag> </item>
<item> nine <tag> $._value = 9 </tag> </item>
<item> ten <tag> $._value = 10 </tag> </item>
<item> jack <tag> $._value = 11 </tag> </item>
<item> queen <tag> $._value = 12 </tag> </item>
<item> king <tag> $._value = 13 </tag> </item>
<item> lady <tag> $._value = 12 </tag> </item>
<item> emperor <tag> $._value = 13 </tag> </item>
</one-of>
</rule>
</grammar>