Delen via


Note

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

tag Element (Microsoft.Speech)

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

<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

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 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 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 "$".

<?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.

<?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 creates a grammar for choosing the cities for a flight. The speech recognition engine will return an 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.

The example uses the tag element to create properties ("LeavingFrom" and "GoingTo") on the Rule Variable for the rule named flightBooker. The tag element that follows each ruleref element assigns the semantic results from the rule reference to a property on the Rule Variable. The Rule Variable is identified by "out".

In the rule named flightCities, the tag element within each item element assigns a semantic value (an airport code) to each item element that contains the name of a city or a city's airport. The speech recognition engine returns the semantic value (the contents of the tag element) as well as the recognized speech (the textual contents of an item element) when it recognizes the name of a city or airport.

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

Semantic results:

  • Leaving from: BOS

  • Going to: SEA

<?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 "$$". The Rule Variable of the rule that contains the rule reference is identified by "$".

<?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

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

Concepts

Using the tag Element (Microsoft.Speech)

Support for Semantic Markup (Microsoft.Speech)

Referencing Grammar Rule Variables (Microsoft.Speech)

Grammar Rule Name Referencing (Microsoft.Speech)

Grammar Rule Reference Referencing (Microsoft.Speech)

rule Element (Microsoft.Speech)

item Element (Microsoft.Speech)

ruleref Element (Microsoft.Speech)