Share via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Microsoft Speech Platform

SRGS XML Grammar Example: Solitaire

Grammar rules define the words and phrases that a speech recognition engine can use to recognize speech input. Phrases and sub-expressions are commonly represented by separate rules and combined into larger phrases and sentences by higher level rules. For more information, see the Grammar rules section.

In addition to specifying the words and phrases that can be recognized, a grammar rule can specify additional information that the speech recognition engine will return when it recognizes a word or phrase. For example, a grammar rule could assign the semantic value "ORD" (the airport code) to the city name "Chicago". The speech recognition engine can return the airport code ("ORD"), as well as the name of the city ("Chicago"), each time it recognizes "Chicago".

Grammar rules can also define an information structure that organizes the rules and their expected results into a semantic hierarchy. Using script in <tag> elements, grammar rules can create the nodes of a semantic hierarchy and assign each node an identifying name (a semantic key), which the speech recognition engine will return with the results of recognition. For example, if "Chicago" is in a list of city names that a user can choose as the origin or the destination for a flight, the grammar rule can assign a semantic key that identifies the selection of "Chicago" as an origin or a destination city. See tag Element (Microsoft.Speech) for more information.

In summary, the definition of a grammar rule can specify the words or phrases that can be recognized, additional information to return when a word or phrase is recognized, and the name of the node that generated the recognition result.

Example

The card game solitaire uses semantic objects such as cards, suits, and ranks, and uses semantic actions, such as "play the <some card>", "move <one card> to <another card>", and "put <some card> on <another card>". The following example illustrates an example implementation of 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), which must be spoken to play the game.

When a command such as "play the three of diamonds" is spoken, the first ruleref element in the rule named "toplevel" references the rule named "PlayCard". The rule named "PlayCard" in turn references the rule named "Card", which returns information about the card to play. An application can obtain this information from the semantic key named "Card", which is specified in the tag element of the rule named "toplevel", as follows: <tag> out.Card = rules.PlayCard; </tag>. This example makes extensive use of the tag element to return semantic values. For more information, see Using the tag Element (Microsoft.Speech).

When a command such as "move the seven of clubs to the six of diamonds" is spoken, the "MoveCard" rule is referenced by the second ruleref element in the "toplevel" rule. The "MoveCard" rule in turn references the "Card" rule twice. These references return semantic information about the card to move and its destination. An application can obtain this information from the semantic key "MoveCard".

The following illustration depicts the hierarchy of the semantic information returned by the grammar. If the user utterance is "play the two of hearts", the semantic key "Card" can be used to determine which card is to be played. The information is present in the "Rank" and "Suit" sub-keys of the semantic key "Card". These sub-keys will contain, respectively, the semantic values "2" and "Hearts".

Semantic Diagram of Solitaire Example

If the user utterance is "move the red Queen to the Jack of clubs," the semantic key "MoveCard" can be used to determine the card to be moved, and the card that is the moved card's destination. The keys of the "Rank" and "Color" sub-keys of the "FromCard" key contain, respectively, "Queen" and "Red'. The keys of the "Rank" and "Suit" sub-keys of the "ToCard" key contain, respectively, "Jack" and "Clubs".

`

<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US" mode="voice" root="toplevel"
xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">
<rule id="toplevel" scope="public">
<one-of>
<item> <ruleref uri="#PlayCard"/> <tag> out.Card = rules.PlayCard; </tag> </item>
<item> <ruleref uri="#MoveCard"/> <tag> out.MoveCard = rules.MoveCard; </tag> </item>
</one-of>
</rule>

<rule id="PlayCard" scope="public"> <example> please play the red queen </example> <example> play the ace </example> <example> play the five of diamonds please </example>

&lt;item repeat="0-1"&gt; please &lt;/item&gt;
&lt;item&gt; play the &lt;/item&gt;
&lt;ruleref uri="#Card"/&gt;
&lt;tag&gt; out = rules.Card; &lt;/tag&gt;
&lt;item repeat="0-1"&gt; please &lt;/item&gt;

</rule>

<rule id="MoveCard"> <example> move the five of clubs to the six of hearts </example> <example> please put the jack of hearts on the ten of clubs </example>

&lt;item repeat="0-1"&gt; please &lt;/item&gt;
&lt;one-of&gt;
  &lt;item&gt; move the &lt;/item&gt;
  &lt;item&gt; put the &lt;/item&gt;
&lt;/one-of&gt;
&lt;ruleref uri="#Card"/&gt;
&lt;tag&gt; out.FromCard = rules.Card; &lt;/tag&gt;
&lt;item repeat="0-1"&gt;
  &lt;one-of&gt;
    &lt;item&gt; on the &lt;/item&gt;
    &lt;item&gt; to the &lt;/item&gt;
  &lt;/one-of&gt;
  &lt;ruleref uri="#Card"/&gt;
  &lt;tag&gt; out.ToCard = rules.latest(); &lt;/tag&gt; 
&lt;/item&gt;
&lt;item repeat="0-1"&gt; please &lt;/item&gt;

</rule>

<rule id="Card"> <example> red queen </example> <example> jack of clubs </example> <example> ace </example> <example> spade </example>

&lt;one-of&gt;
  &lt;item&gt;
    &lt;!-- color and rank form --&gt;
    &lt;ruleref uri="#Color"/&gt;
    &lt;tag&gt; out.Color = rules.Color; &lt;/tag&gt;
    &lt;ruleref uri="#Rank"/&gt;
    &lt;tag&gt; out.Rank = rules.Rank; &lt;/tag&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
    &lt;!-- rank and optional suit form --&gt;
    &lt;ruleref uri="#Rank"/&gt;
    &lt;tag&gt; out.Rank = rules.Rank; &lt;/tag&gt;
    &lt;item repeat="0-1"&gt;
      of
      &lt;ruleref uri="#Suits"/&gt;
      &lt;tag&gt; out.Suit = rules.Suits; &lt;/tag&gt;
    &lt;/item&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
    &lt;!-- suit only form --&gt;
    &lt;ruleref uri="#Suit"/&gt;
    &lt;tag&gt; out.Suit = rules.Suit; &lt;/tag&gt;
  &lt;/item&gt;
&lt;/one-of&gt;

</rule>

<rule id="Color"> <example> red </example> <example> black </example>

&lt;one-of&gt;
  &lt;item&gt; red &lt;tag&gt; out = "Red"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; black &lt;tag&gt; out = "Black"; &lt;/tag&gt; &lt;/item&gt;
 &lt;/one-of&gt;

</rule>

<rule id="Suit"> <example> spade </example> <example> club </example>

&lt;one-of&gt;
  &lt;item&gt; club &lt;tag&gt; out = "Club"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; heart &lt;tag&gt; out = "Heart"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; diamond &lt;tag&gt; out = "Diamond"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; spade &lt;tag&gt; out = "Spade"; &lt;/tag&gt; &lt;/item&gt;
 &lt;/one-of&gt;

</rule>

<rule id="Suits"> <example> spades </example> <example> clubs </example>

&lt;one-of&gt;
  &lt;item&gt; clubs  &lt;tag&gt; out = "Club"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; hearts &lt;tag&gt; out = "Heart"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; diamonds &lt;tag&gt; out = "Diamond"; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; spades &lt;tag&gt; out = "Spade"; &lt;/tag&gt; &lt;/item&gt;
 &lt;/one-of&gt;

</rule>

<rule id="Rank"> <example> ace </example> <example> five </example> <example> king </example> <example> jack </example>

&lt;one-of&gt;
  &lt;item&gt; ace &lt;tag&gt; out = 1; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; two &lt;tag&gt; out = 2; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; three &lt;tag&gt; out = 3; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; four &lt;tag&gt; out = 4; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; five &lt;tag&gt; out = 5; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; six &lt;tag&gt; out = 6; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; seven &lt;tag&gt; out = 7; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; eight &lt;tag&gt; out = 8; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; nine &lt;tag&gt; out = 9; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; ten &lt;tag&gt; out = 10; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; jack &lt;tag&gt; out = 11; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; queen &lt;tag&gt; out = 12; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; king &lt;tag&gt; out = 13; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; lady &lt;tag&gt; out = 12; &lt;/tag&gt; &lt;/item&gt;
  &lt;item&gt; emperor &lt;tag&gt; out = 13; &lt;/tag&gt; &lt;/item&gt;
&lt;/one-of&gt;

</rule> </grammar>

`

See Also