Compartilhar via


Using the tag Element

Script expressions in tag elements generate semantic values for the content of item elements and the target of ruleref elements (referenced rules) within the containing rule element. These values can be associated with the Rule Variable of the containing rule as properties of that Rule Variable. For more information about the various forms of reference to Rule Variables, see Referencing Grammar Rule Variables.

Placement of the tag Element

Author-defined scripts for generating semantic values must be contained inside tag elements.

<tag> out.property="desired value"; </tag>

You can also use an alternate syntax, created by Microsoft, to author the script in tag elements. To use this syntax, set tag-format="semantics-ms/1.0" in the grammar element. See Support for Semantic Markup (Microsoft.Speech).

Using the syntax of semantics-ms/1.0, the example above can be written as follows:

<tag> $.property="desired value"; </tag>

Place tag elements so that they immediately follow the word or phrase for which a semantic value is needed or immediately follow a ruleref that has a Rule Variable that contains information that the script expression is required to read.

Typically, words or phrases that require a semantic value are contained inside an item element. In this case, the tag element is contained within the item element. In World Wide Web Consortium (W3C) grammars, it is possible to include words or phrases in a grammar that are not explicitly contained in an item element, but that are nevertheless treated the same as words or phrases that are contained in an item element. In both cases, the script expression contained in the tag element is executed when the speech recognizer follows a path through the grammar that includes the words or phrases that the tag element follows.

A tag element can also be placed following a ruleref element. In this case, the script expression contained in the tag element is executed after the speech recognizer follows a path through the grammar that leads through the ruleref element. Use a tag element in this position to process the semantic information contained in the Rule Variable of the rule that the ruleref element references.

Values that are generated by scripts are attached to the Rule Variable of the rule that contains the tag element. The value corresponding to the root rule in the grammar is converted into the Semantic Markup Language (SML) output. Script expressions contained in semantic interpretation tags follow the syntax of ECMA-327.

Examples of tag Element Placement

The following examples illustrate tag element placement within a grammar rule.

In the following grammar, a tag element is placed within an item element to explicitly associate the expressions in the tag element with the content of the item element. The script expressions create a daynum property of the Rule Variable and set it to a numeric value.

<rule id="WeekdaySelection">
  <one-of>
    <item> Monday <tag> out.daynum="1"; </tag></item>
    <item> Wednesday <tag> out.daynum="3"; </tag></item>
    <item> Friday <tag> out.daynum="5"; </tag></item>
  </one-of>
</rule>

In response to the utterance "Monday," the speech recognizer selects a path through the item element containing "Monday." Following this path triggers the execution of the script that is contained by the tag element inside the item element containing "Monday." The script generates a property named daynum of the Rule Variable belonging to the WeekdaySelection rule and associates the value 1 with daynum.

Using the syntax of semantics-ms/1.0, the example above can be written as follows:

<rule id="WeekdaySelection">
  <one-of>
    <item> Monday <tag> $.daynum="1"; </tag></item>
    <item> Wednesday <tag> $.daynum="3"; </tag></item>
    <item> Friday <tag> $.daynum="5"; </tag></item>
  </one-of>
</rule>

In the following grammar, a tag element follows a word that is not explicitly contained within an item element (the word "select"). Words or phrases in the grammar that are not explicitly contained in an item element are treated as if they are contained in an item element. The rule below recognizes the utterances "select Monday," "select Wednesday," and "select Friday" and generates semantic values associated with both the word "select" and with the day name.

<rule id="WeekdaySelection">
  Select <tag> out.specifier="on"; </tag>
  <one-of>
    <item> Monday <tag> out.daynum="Day 1"; </tag></item>
    <item> Wednesday <tag> out.daynum="Day 3"; </tag></item>
    <item> Friday <tag> out.daynum="Day 5"; </tag></item>
  </one-of>
</rule>

The spoken phrase "select Wednesday" generates a specifier and a daynum property of the Rule Variable WeekdaySelection and associates the semantic values on and Day 3 with these properties, respectively.

Using the syntax of semantics-ms/1.0, the example above can be written as follows:

<rule id="WeekdaySelection">
  Select <tag> $.specifier="on"; </tag>
  <one-of>
    <item> Monday <tag> $.daynum="Day 1"; </tag></item>
    <item> Wednesday <tag> $.daynum="Day 3"; </tag></item>
    <item> Friday <tag> $.daynum="Day 5"; </tag></item>
  </one-of>
</rule>

In the following grammar, the tag element immediately follows the ruleref element. This placement associates the script expressions contained in the tag element with the value returned by the ruleref element. This rule recognizes the utterances "Saturday morning," "Saturday evening," "Sunday morning," and "Sunday evening" and generates numeric values representing the path that the recognizer follows through the grammar.

<rule id="AppointmentTime">
  <one-of>
    <item> Saturday <tag> out.daynum="6"; </tag></item>
    <item> Sunday <tag> out.daynum="7"; </tag></item>
  </one-of>
  <ruleref uri="#RelativeTime" /><tag> out.TimeRange=rules.RelativeTime; </tag>
</rule>

<rule id="RelativeTime">
  <one-of>
    <item> morning <tag> out.range="1"; </tag></item>
    <item> evening <tag> out.range="2"; </tag></item>
 </one-of>
</rule>

Notice the script contained in the tag element following the ruleref to RelativeTime. The script creates a property named TimeRange of the Rule Variable for the AppointmentTime rule and assigns the contents of the range property of the Rule Variable for RelativeTime to TimeRange.

Using the syntax of semantics-ms/1.0, the example above can be written as follows:

<rule id="AppointmentTime">
  <one-of>
    <item> Saturday <tag> $.daynum="6"; </tag> </item>
    <item> Sunday <tag> $.daynum="7"; </tag> </item>
  </one-of>
  <ruleref uri="#RelativeTime" /> <tag> $.TimeRange=$RelativeTime.range; </tag>
</rule>

<rule id="RelativeTime">
  <one-of>
    <item> morning <tag> $.range="1"; </tag> </item>
    <item> evening <tag> $.range="2"; </tag> </item>
 </one-of>
</rule>

The path followed in response to the utterance "Saturday morning" triggers the scripts associated with "Saturday", "morning", and the ruleref to RelativeTime. This process generates the following data structure.

AppointmentTime => { daynum    =>   6            }
                   { TimeRange => { range => 1 } }

See Also

Concepts

tag Element

Referencing Grammar Rule Variables

Grammar Rule Name Referencing

Grammar Rule Reference Referencing

Support for Semantic Markup