Share via


Complete Example Grammars with SI Markup

  Microsoft Speech Technologies Homepage

Two complete examples of grammars that contain markup for semantic interpretation (SI) with the Microsoft Speech Application SDK Version 1.1 (SASDK) are presented. Unlike the simplified grammar examples in other parts of the semantic interpretation help documents, the first eight lines in these examples contain all of the attribute-value pairs that are either required or recommended by the World Wide Web Consortium (W3C) specification for grammars. The example contains line numbers in order to simplify the identification of specific lines for comment in the notes sections. Also note that the grammar in plan_itinerary.grxml contains rule references to the grammar in time_information.grxml.

plan_itinerary.grxml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0/EN"
          "http://www.w3.org/TR/speech-grammar/grammar.dtd">
                      
<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
         xml:base="https://www.adventure-works.com/project"
         root="itinerary" xml:lang="en-US"
         mode="voice" tag-format="semantics-ms/1.0">
  <rule id="itinerary">
    <item>book a flight departing from</item>
    <ruleref uri="#airports" /><tag>$.departfrom=$airports;</tag>
    <item>on</item>
    <ruleref uri="time_information.grxml" /><tag>$.departday=$flightdays;</tag>
    <item>in the</item>
    <ruleref uri="time_information.grxml#flighttimes" /><tag>$.arriverelativetime=$flighttimes;</tag>
    <ruleref special="GARBAGE" />
    <item>and arriving at</item>
    <ruleref uri="#airports" /><tag>$.arrivein=$airports;</tag>
    <item>on</item>
    <ruleref uri="time_information.grxml" /><tag>$.arriveday=$flightdays;</tag>
    <item>in the</item> 
    <ruleref uri="time_information.grxml#flighttimes" /><tag>$.departrelativetime=$flighttimes;</tag>
  </rule>
  
  <rule id="airports">
    <one-of>
      <item>Heathrow<tag>$.code={}; $.code._value= "LHR";</tag></item>
      <item>Gatwick<tag>$.code={}; $.code._value="LGW";</tag></item>
      <item>Washington National<tag>$.code={}; $.code._value="DCA";</tag></item>
    </one-of>
  </rule>
</grammar>

Notes on plan_itinerary.grxml:

Line 6: A base URI is recommended, but is not required.
 
Line 7: Specifying a root rule is required by the SASDK. Grammars for which a root rule is not specified will compile, but will not trigger recognition.
 
Line 8: The tag-format attribute must have the value "semantics-ms/1.0" to activate semantic interpretation in the SASDK.
 
Line 12: The #airports value for the ruleref URI indicates reference to a rule that is located within the same grammar and that has the id airports.
 
The expression, $.departfrom=$airports, assigns whatever value is given to $airports to the departfrom property of the Rule Variable that is associated with the itinerary rule ($itinerary.departfrom=$airport). The value of $airports is generated by the tag elements in the airports rule.
 
Line 14: Because the example grammar specifies that the flightdays rule is the root rule in the grammar time_information.grxml, it is automatically a public-scoped rule. A ruleref that specifies only the bare URI of this grammar references this rule by default.
 
Line 16: The flighttimes rule in the grammar time_information.grxml is referenced in Line 16. Although it is not the root rule in the time_information.grxml grammar, a value can be read from the flighttimes Rule Variable because the scope of flighttimes is set to public.
 
Line 17: The special rulename, GARBAGE, matches any speech until the next rule match, the next token, or until the end of spoken input. In this example, the speaker can say anything in between saying a relative time, such as "Afternoon," and the phrase "and arriving at."
 
Line 26: Because scope is not explicitly specified for the airports rule, the scope is set to private by default. As a consequence, values generated by this rule can be referenced only by rules contained within the same grammar, and not by rules contained in other grammars.

time_information.grxml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0/EN"
          "http://www.w3.org/TR/speech-grammar/grammar.dtd">
                      
<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
         xml:base="https://www.adventure-works.com/project"
         root="flightdays" xml:lang="en-US"
         mode="voice" tag-format="semantics-ms/1.0">
  <rule id="flightdays">
    <one-of>
      <item weight="1">Monday<tag>$.selectedday={}; $.selectedday._value= "1";</tag></item>
      <item weight="4">Wednesday<tag>$.selectedday={}; $.selectedday._value="3";</tag></item>
      <item weight="9">Friday<tag>$.selectedday={}; $.selectedday._value="5";</tag></item>
    </one-of>
  </rule>
  <rule id="flighttimes" scope="public">
    <one-of>
      <item>Morning<tag>$.selectedtime={}; $.selectedtime._value= "AM";</tag></item>
      <item>Afternoon<tag>$.selectedtime={}; $.selectedtime._value="Midday";</tag></item>
      <item>Evening<tag>$.selectedtime={}; $.selectedtime._value="PM";</tag></item>
    </one-of>
  </rule>
  </grammar>

Notes on time_information.grxml:

Line 18: Because the scope of this rule has been set to public, values generated by this rule may be read by rules outside of this grammar.

SML Output

Based on the preceding grammars, saying Book a flight departing from Heathrow on Monday in the evening and arriving at Washington National on Wednesday in the morning, produces the following SML output.

<SML confidence="0.847" text="book a flight departing from Heathrow on Monday in the Evening
     and arriving at Washington National on Wednesday in the Morning" utteranceConfidence="0.847">
  <departfrom confidence="0.864">
    <code confidence="0.864">LHR</code> 
  </departfrom>
  <departday confidence="0.848">
    <selectedday confidence="0.848">1</selectedday> 
  </departday>
  <arriverelativetime confidence="0.876">
    <selectedtime confidence="0.876">PM</selectedtime> 
  </arriverelativetime>
  <arrivein confidence="0.863">
    <code confidence="0.863">DCA</code> 
  </arrivein>
  <arriveday confidence="0.852">
    <selectedday confidence="0.852">3</selectedday> 
  </arriveday>
  <departrelativetime confidence="0.842">
    <selectedtime confidence="0.842">AM</selectedtime> 
  </departrelativetime>
</SML>

For information on how the recognizer serializes results into SML output, refer to the SML Reference.