Note

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

Create Variations of User Commands (Microsoft.Speech)

To make the user commands more versatile, create rules that each contain a set of alternative choices that can be referenced by another rule. A rule can contain a reference to another rule that must also be matched in order to match the current rule. Rules that contain only alternative choices can be referenced from within a containing grammar or an external grammar.

To reference another rule element from within a rule element, use the ruleref element. See ruleref Element (Microsoft.Speech) for more information. The ruleref element has one required attribute:

  • Uri. The id of the referenced rule element. If the reference is to a rule element in an external grammar, the name of the grammar file and the id of the rule.

In this topic, you will learn how to create rules that contain multiple components, including a reference to another rule.

First, create a rule element that contains list of alternative words for the play action. Give the rule element an id and add each alternative word to an item element within a one-of element.

  <rule id="playAction">
    <one-of>
      <item> play </item>
      <item> start </item>
      <item> begin </item>
    </one-of>
  </rule>

Next, create a rule element that contains list of alternative words for the file that is the object of a play action.

  <rule id="fileWords">
    <one-of>
      <item> song </item>
      <item> tune </item>
      <item> track </item>
      <item> item </item>
    </one-of>
  </rule>

Now create a top-level rule element that references both the playAction and fileWords rules to create a flexible collection of commands that an application can recognize and use to play a selected music file. The elements within the top-level rule element must be listed in the order that the user will speak the command. This rule element will be the root rule of the grammar.

  <rule id="playCommands">
    <ruleref uri="#playAction" />
    <item> the</item>
    <ruleref uri="#fileWords" />
  </rule>

Now build the grammar by adding the rules you created. Within the opening and closing tags of the grammar element, add the rule element named playCommands, and set the value of the grammar element's root attribute to playCommands. Add the rule element named playAction, and then add the rule element named fileWords.

<grammar version="1.0" xml:lang="en-US" root="playCommands"
 xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="playCommands">
    <ruleref uri="#playAction" />
    <item> the </item>
    <ruleref uri="#fileWords" />
  </rule>

  <rule id="playAction">
    <one-of>
      <item> play </item>
      <item> start </item>
      <item> begin </item>
    </one-of>
  </rule>

  <rule id="fileWords">
    <one-of>
      <item> song </item>
      <item> tune </item>
      <item> track </item>
      <item> item </item>
    </one-of>
  </rule>

</grammar>

The resulting grammar can be used to recognize phrases such as "play the track", "begin the song", and "start the tune". The application can use the recognition of any phrase that can be created by a combination of the elements in the rule named playCommands, as long as the elements are used in the order listed. The grammar will not recognize "the start item".

To this point, all the examples have focused on only one end result: to begin playback of a selected music file. However, you may want to have multiple rules, which are active simultaneously, that play, pause, and stop music playback. That is the focus of the next topic: Create Grammars with Multiple Active Rules (Microsoft.Speech).

See Also

Concepts

ruleref Element (Microsoft.Speech)