Partilhar via


Note

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

Grammar Example: Solitaire (Microsoft.Speech)

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 Grammars Overview (Microsoft.Speech).

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 (Microsoft.Speech) 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".

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".

Hh378351.8637bf63-f7bc-4ee7-b5be-5ab54dde4f5c(en-us,office.14).jpg

<?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>

    <item repeat="0-1"> please </item>
    <item>play the</item>
    <ruleref uri="#Card"/>
    <tag> out = rules.Card; </tag>
    <item repeat="0-1"> please </item>
  </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>

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

  <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> out.Color = rules.Color; </tag>
        <ruleref uri="#Rank"/>
        <tag> out.Rank = rules.Rank; </tag>
      </item>
      
      <item>
        <!-- rank and optional suit form -->
        <ruleref uri="#Rank"/>
        <tag> out.Rank = rules.Rank; </tag>
        <item repeat="0-1">
          of
          <ruleref uri="#Suits"/>
          <tag> out.Suit = rules.Suits; </tag>
        </item>
      </item>
      
      <item>
        <!-- suit only form -->
        <ruleref uri="#Suit"/>
        <tag> out.Suit = rules.Suit; </tag>
      </item>
    </one-of>
  </rule>
   
  <rule id="Color">
    <example> red </example>
    <example> black </example>

    <one-of>
      <item> red <tag> out = "Red"; </tag> </item>
      <item> black <tag> out = "Black"; </tag> </item>
     </one-of>
  </rule>

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

    <one-of>
      <item> club <tag> out = "Club"; </tag> </item>
      <item> heart <tag> out = "Heart"; </tag> </item>
      <item> diamond <tag> out = "Diamond"; </tag> </item>
      <item> spade <tag> out = "Spade"; </tag> </item>
     </one-of>
  </rule>

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

    <one-of>
      <item> clubs  <tag> out = "Club"; </tag> </item>
      <item> hearts <tag> out = "Heart"; </tag> </item>
      <item> diamonds <tag> out = "Diamond"; </tag> </item>
      <item> spades <tag> out = "Spade"; </tag> </item>
     </one-of>
  </rule>

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

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