Partilhar via


Grammar Rule Name Referencing

  Microsoft Speech Technologies Homepage

ECMAScript expressions that are contained in tag elements generate semantic values that are associated with the Rule Variable of a rule element. Script expressions that are contained in a tag element can evaluate values, as well as assign values to the Rule Variable of the tag element's containing rule element. This Rule Variable of the containing rule element is called the Grammar Rule Name (GRN) Rule Variable. Scripts evaluate or assign values to the GRN Rule Variable using GRN referencing.

The GRN Rule Variable is identified by a dollar sign ("$"), and is predefined as an object before any script expressions are processed. The GRN Rule Variable contains two predefined properties named _attributes and _value (represented by $._attributes and $._value) that can be used to set semantic information to be returned as the text content and element attribute-value pairs in the Semantic Markup Language (SML) output. For more information on how the recognizer serializes the Rule Variable and its properties, refer to Serialization of the Root Rule Variable (RRV) and Serialization of Developer-defined Rule Variable Properties.

Separate tag elements can contain individual script expressions. A single tag element can also contain a concatenated series of multiple script expressions. Script expressions cannot assign values to reserved words.

Creating Child Properties

Because the GRN Rule Variable is predefined as an object, direct child properties of it can be created without using the new operator. Use $.propname (where propname is the name of the property) to identify properties of the GRN Rule Variable.

In the following example, the GRN Rule Variable of the airport rule is predefined as an empty object. The script expressions contained in the tag elements create two properties (code and location) of the airport Rule Variable, and assign values to these properties. The script expressions assign the value, LHR, to the code property, and the value, England, to the location property.

<rule id="airport">
  Heathrow
    <tag>$.code= "LHR";</tag>
    <tag>$.location= "England";</tag>
</rule>

Creating Grandchild Properties

ECMAScript is not a strongly typed language, meaning that when a property is initialized, it is not predefined as a specific type. Instead, assigning a value to the property sets the type of the property to the same type as the assigned value. In the preceding example, because the property location is assigned a string value, the property is defined as a string.

The way that type is set in ECMAScript has implications for creating grandchild properties. Before grandchild properties can be created, script writers must first initialize a child property of the type object in order to contain the grandchild properties. Initialize an object using the new operator ("{}")as in the following example:

<rule id="airport">
  Heathrow
    <tag>$.location={};</tag>
    <tag>$.location.country= "England";</tag>
    <tag>$.location.city= "London";</tag>
    <tag>$.location.elevation= "80 ft. MSL";</tag>
</rule>

In this example, the script initializes the location property as an object. The grandchild properties country, city, and elevation are then initialized as direct properties of the child property location, and assigned the values England, London, and 80 ft. MSL respectively.

Order of Assignment

The way that type is set in ECMAScript also has implications for the order in which script writers can assign values to the Rule Variable or its properties. The previous examples illustrate how a script writer can initialize the location property as either a string or an object. Although the Rule Variable is predefined to be an object, the Rule Variable can also be changed to a different type through simple assignment. In the following example, the script assigns a string value to the Rule Variable, thus changing the type of the Rule Variable from an object to a string:

<rule id="airport">
  Heathrow
    <tag>$ = "A major airport near London";</tag>
</rule>

If the type of a property or the Rule Variable is changed, any values that already exist in the property or Rule Variable will be lost. In the following example, the script initializes the code and location child properties and assigns the values, LHR, and, England, to them respectively. However, in the next line, these properties and their values are destroyed when the Rule Variable is assigned a string value:

<rule id="airport">
  Heathrow
    <tag>$.code = "LHR";</tag>
    <tag>$.location = "England";</tag>
    <tag>$ = "A major airport near London";</tag>
</rule>

In response to the utterance, Heathrow, this rule returns the semantic value, "A major airport near London," and not the values LHR and England.