tag Element (Microsoft.Speech)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

A tag element contains semantic information, either as a string or as ECMAScript (JavaScript, JScript), which returns additional information when an element or series of elements is recognized. A tag element does not affect the legal word patterns defined by the grammars or the process of recognizing speech or other input given a grammar. For more information, see Using the tag Element (Microsoft.Speech).

Syntax

XML  Copy imageCopy Code
<tag> string </tag>
OR
<tag> out="value"; </tag>
OR
<tag> $="value"; </tag> 
OR 
<tag> out.Property="value"; </tag>
OR 
<tag> $.Property="value"; </tag>

Attributes

None.

Remarks

A tag element is a direct child of an item element or a rule element. A tag element can contain one of two types of content: string literals or ECMAScript. The content type of the tag elements within a grammar must be declared in the tag-format attribute of the grammar element. Grammars of greater complexity typically use the ECMAScript content type (tag-format="semantics/1.0" or tag-format="semantics-ms/1.0").

Script expressions or string literals in tag elements generate semantic values for the content of an item element or the target of a ruleref element within a rule. See Grammar Rule Name Referencing (Microsoft.Speech) and Grammar Rule Reference Referencing (Microsoft.Speech) for more information.

A tag element can contain an individual script expression or a concatenated series of multiple script expressions. Script expressions contained in tag elements follow the syntax of ECMA-327. Script expressions cannot assign values to reserved words. The content of the tag element is CDATA.

Note Note

Using the string literal syntax when the value for tag-format is semantics/1.0 or semantics-ms/1.0 will generally result in a runtime error. However, using the ECMAScript syntax when the value for tag-format is semantics/1.0-literals will not produce a runtime error, but will erroneously populate Rule Variables with ECMAScript code.

Examples

The following provides examples for using tag elements to assign semantic meaning to item elements and to ruleref elements.

Using Tag Elements with Item Elements

The following example shows a use of string literals in the tag element. The tag element assigns a string value to the Rule Variable of the rule "destAirport". When "Heathrow" is recognized, the string "Destination" is returned as a semantic result.

XML  Copy imageCopy Code
<?xml version="1.0" encoding="utf-8"?>
<grammar version="1.0" xml:lang="en-US" root="destAirport"
 xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0-literals">

  <rule id="destAirport">
    <item> Heathrow <tag> Destination </tag> </item>
  </rule>

</grammar>

The next example uses script in the tag element to assign a string value to the Rule Variable of the rule "destAirport".

When "Heathrow" is recognized, the string "Destination" is returned as a semantic result.

XML  Copy imageCopy Code
<?xml version="1.0" encoding="utf-8"?>
<grammar version="1.0" xml:lang="en-US" root="destAirport"
 xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">
   
  <rule id="destAirport">
    <item> Heathrow <tag> out="Destination"; </tag> </item>
  </rule>

</grammar>

The following example is the same as the preceding example, but uses the syntax of tag-format="semantics-ms/1.0".

  • If the value for the tag-format attribute of the grammar element is semantics/1.0, the Rule Variable is named "out".

  • If the value for tag-format is semantics-ms/1.0 the Rule Variable is named "$".

  Copy imageCopy Code
<?xml version="1.0" encoding="utf-8"?>
<grammar version="1.0" xml:lang="en-US" root="destAirport"
 xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics-ms/1.0">
   
  <rule id="destAirport">
    <item> Heathrow <tag> $="Destination"; </tag> </item>
  </rule>

</grammar>

In the following example, the script expression contained in the tag element creates a property named "code" for the Rule Variable of the rule named destAirport and assigns the value "LHR" to the created property. When "Heathrow" is recognized, the string "LHR", which is the airport code for Heathrow, is returned as a semantic result.

  Copy imageCopy Code
<?xml version="1.0" encoding="utf-8"?>
<grammar version="1.0" xml:lang="en-US" root="destAirport"
 xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">

  <rule id="destAirport">
    <item> Heathrow <tag> out.code = "LHR"; </tag> </item>
  </rule>

</grammar>

Using Tag Elements with RuleRef Elements

The examples above show the use of the tag element as a child of the item element to assign a semantic value to the item element's contents. Another common use for the tag element is to assign a semantic meaning to the recognition result from a rule that is the target specified by a ruleref element.

The following example assigns the semantic key "LeavingFrom" to recognition results from the first ruleref element to the rule named flightCities. The example assigns the semantic key "GoingTo" to recognition results from the second rule reference to the rule named flightCities. The example uses the tag element in each item element of the rule named flightCities to assign a semantic value that is the airport code to each item element that contains the name of a city or a city's airport. The speech recognition engine will return the airport code when a city or airport name is recognized, and whether the city is the "LeavingFrom" city or the "GoingTo" city for the flight. Note that the rule flightCities accepts multiple speech inputs for the Seattle and Dallas city/airports, but returns only one semantic result for each. Here is a sample recognition result from the grammar that follows:

Recognized phrase: I want to fly from Boston to Sea-Tac

Leaving from: BOS

Going to: SEA

  Copy imageCopy Code
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="flightBooker" 
tag-format="semantics/1.0" version="1.0" 
xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="flightBooker" scope="public">
    <item> I want to fly from </item>
    <ruleref uri="#flightCities" />
    <tag> out.LeavingFrom=rules.latest(); </tag>
    <item> to </item>
    <ruleref uri="#flightCities" />
    <tag> out.GoingTo=rules.latest(); </tag>
  </rule>

  <rule id="flightCities" scope="private">
    <one-of>
      <item> <tag> out="SEA"; </tag>
        <one-of>
          <item> Seattle-Tacoma </item>
          <item> Sea-Tac </item>
          <item> Seattle </item>
        </one-of>
      </item>
      <item> Boston <tag> out="BOS"; </tag> </item>
      <item> Miami <tag> out="MIA"; </tag> </item>
      <item> <tag> out="DFW"; </tag>
        <one-of>
          <item> Dallas </item>
          <item> Fort Worth </item>
          <item> Dallas-Fort Worth </item>
        </one-of>
      </item>
    </one-of>
  </rule>

</grammar>

The preceding example uses the latest() function on the rules object to reference the last ruleref element to be used in the rule that matches the utterance.

The following example is the same as the preceding example, but uses the syntax of tag-format="semantics-ms/1.0". In the syntax of semantics-ms/1.0, the result from the latest referenced rule that matches the utterance can be represented by the shorthand symbol "$$".

  Copy imageCopy Code
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="flightBooker" 
tag-format="semantics-ms/1.0" version="1.0" 
xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="flightBooker" scope="public">
    <item> I want to fly from </item>
    <ruleref uri="#flightCities" />
    <tag> $.LeavingFrom=$$; </tag>
    <item> to </item>
    <ruleref uri="#flightCities" />
    <tag> $.GoingTo=$$; </tag>
  </rule>

  <rule id="flightCities" scope="private">
    <one-of>
      <item> <tag> $="SEA"; </tag>
        <one-of>
          <item> Seattle-Tacoma </item>
          <item> Sea-Tac </item>
          <item> Seattle </item>
        </one-of>
      </item>
      <item> Boston <tag> $="BOS"; </tag> </item>
      <item> Miami <tag> $="MIA"; </tag> </item>
      <item> <tag> $="DFW"; </tag>
        <one-of>
          <item> Dallas </item>
          <item> Fort Worth </item>
          <item> Dallas-Fort Worth </item>
        </one-of>
      </item>
    </one-of>
  </rule>

</grammar>
Note Note

Scripts in tag elements are executed only if the item or rule containing it provides a match.

For an example of an event handler that will retrieve semantic results, see

SpeechRecognizedEventArgs.

See Also