Complete Example Grammars with SI Markup (Microsoft.Speech)

This page contains two complete examples of grammars that contain markup for semantic interpretation (SI). Unlike the simplified grammar examples in other parts of the SI Help documents, the first six lines in these examples contain all the attribute-value pairs that are required or recommended by the World Wide Web Consortium (W3C) Speech Recognition Grammar Specification (SRGS) Version 1.0. Also, the grammar in Plan_itinerary.grxml contains rule references to the grammar in Time_information.grxml. It is an accepted convention to use the .grxml file extension for XML-based grammar documents that conform to the SRGS specification. The examples contain line numbers to simplify the identification of specific lines for comment in the notes sections.


1   <?xml version="1.0" encoding="UTF-8"?>
2   <!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0/EN"
3             "">
5   <grammar version="1.0" xml:lang="en-US" mode="voice" root="itinerary" 
6     xmlns="" xml:base="" tag-format="semantics/1.0">
8     <rule id="itinerary">
9       <item> book a flight departing from </item>
10       <ruleref uri="#airports" /><tag> out.departFrom=rules.airports; </tag>
11       <item> on </item>
12       <ruleref uri="time_information.grxml" /><tag> out.departDay=rules.flightDays; </tag>
13       <item> in the </item>
14       <ruleref uri="time_information.grxml#flighttimes" /><tag> out.arriveRelativeTime=rules.flightTimes; </tag>
15       <ruleref special="GARBAGE" />
16       <item> and arriving at </item>
17       <ruleref uri="#airports" /><tag> out.arriveIn=rules.airports; </tag>
18       <item> on </item>
19       <ruleref uri="time_information.grxml" /><tag> out.arriveDay=rules.flightDays; </tag>
20       <item> in the </item> 
21       <ruleref uri="time_information.grxml#flightTimes" /><tag> out.departRelativeTime=rules.flightTimes; </tag>
22     </rule>
24     <rule id="airports">
25       <one-of>
26         <item> Heathrow <tag> out.code=new Object(); out.code="LHR"; </tag></item>
27         <item> Gatwick <tag> out.code=new Object(); out.code="LGW"; </tag></item>
28         <item> Washington National <tag> out.code= new Object(); out.code._value="DCA"; </tag></item>
29       </one-of>
30     </rule>
32   </grammar>

Notes on Plan_itinerary.grxml

Line 5:

A grammar must specify a root rule.

Line 6:

A base URI (xml:base) is recommended, but is not required.

Line 6:

The tag-format attribute must have the value "semantics/1.0" or "semantics-ms/1.0" to activate semantic interpretation.

Line 10:

The airports value for the uri attribute of the ruleref element indicates a reference to a rule element with its id attribute set to airports that is located within the same grammar.

Line 10:

The expression, out.departFrom=rules.airports;, assigns the value of the Rule Variable for the rule named airports to the property named departFrom of the Rule Variable for the rule named itinerary. The value of Rule Variable for the rule named airports is generated by the tag elements in the airports rule.

Line 12:

Because the example grammar file Time_information.grxml (below) specifies flightDays as its root rule, flightDays is automatically a publicly-scoped rule. A ruleref element that specifies only the bare URI of a grammar file without specifying a rule within the grammar file references its root rule by default.

Line 14:

The flightTimes rule in the Time_information.grxml grammar file (below) is referenced in line 13. Although it is not the root rule in the Time_information.grxml grammar file, a value can be read from the flightTimes Rule Variable because the scope of flightTimes is set to public.

Line 15:

The special rule name, GARBAGE, matches any speech until the next rule match, the next token, or the end of spoken input. In this example, the speaker can say anything in between saying a relative time, such as "Afternoon" and "and arriving at."

Line 24:

Because the scope attribute 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.


1   <?xml version="1.0" encoding="UTF-8"?>
2   <!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0/EN"
3     "">
5   <grammar version="1.0" xml:lang="en-US" mode="voice" root="flightdays"
6     xmlns="" xml:base="" tag-format="semantics/1.0">
8     <rule id="flightDays" scope=="public">
9       <one-of>
10         <item weight="1"> Monday <tag> out.selectedDay=new Object(); out.selectedDay= "1"; </tag></item>
11         <item weight="4"> Wednesday <tag> out.selectedDay=new Object(); out.selectedDay="3"; </tag></item>
12         <item weight="9"> Friday <tag> out.selectedDay=new Object(); out.selectedDay="5"; </tag></item>
13       </one-of>
14     </rule>
16     <rule id="flightTimes" scope="public">
17       <one-of>
18         <item> Morning <tag> out.selectedTime=new Object(); out.selectedTime="AM"; </tag></item>
19         <item> Afternoon <tag> out.selectedTime=new Object(); out.selectedTime="Midday"; </tag></item>
20         <item> Evening <tag> out.selectedTime=new Object(); out.selectedTime="PM"; </tag></item>
21       </one-of>
22     </rule>
24     </grammar>

Notes on Time_information.grxml

Line 16:

Because the scope of the flightTimes rule has been set to public, values generated by this rule can be read by rules outside of this grammar.

See Also


Semantic Interpretation Markup (Microsoft.Speech)

Using the tag Element (Microsoft.Speech)

tag Element (Microsoft.Speech)

ruleref Element (Microsoft.Speech)