Adding Semantic Interpretation Information
Use Semantic Script Editor to associate semantic interpretation information (semantic information) with relevant elements in a grammar rule. Semantic Script Editor enables grammar authors to manipulate the Semantic Markup Language (SML) text results returned by the recognition engine and sent to an application.
This editor is part of Speech Grammar Editor, and is available when a rule is open in Rule Editor.
Associating Script with an Element
Use the Script Tag element to associate a semantic information script with a rule element, and to open Semantic Script Editor.
To associate a semantic script with an element
- From the Grammar Toolbox, drag a Script Tag element onto the Rule Editor pane, and drop it next to an element that should contain semantic information.
By default, the new Script Tag element contains a string value.
If the Script Tag element is dropped next to a Phrase element that contains a value, the value in the Script Tag appears as follows.
If the Script Tag element is dropped next to a RuleRef element, the value in the Script Tag appears as follows.
If the element is dropped next to any other element, the value in the Script Tag appears as follows.
Note The Script Tag element does not necessarily need to appear directly next to an element. For example, drop a Script Tag element onto the design canvas of an otherwise empty grammar rule to contain initialization scripts. The only exception is that a Script Tag element must appear immediately after, or to the right, of a RuleRef element that returns semantic results.
The tag grammar XML markup element represents a semantic information script in the text of a grammar file, as illustrated in the following sample.
<rule id="Tickets" scope="private">
<item>
<item>one</item>
<tag>$._value = "1"</tag>
</item>
<item>
<item>two</item>
<tag>$._value = "2"</tag>
</item>
</rule>
Creating Simple Variable Assignments
Use simple variable assignments to create SML results that contain sub-properties or attributes whose values are set to a constant value or the value of another rule.
To assign a constant value to an element
- From the Grammar Toolbox, drag a Script Tag element onto the Rule Editor pane, and drop it next to an element that should contain semantic information.
- Click the Script Tag element, click it again, and then click the ellipses (...) button to open Semantic Script Editor.
- On the Assignment tab, in the Source of semantic result frame, select Constant, and in the Enter value text box, type a text value enclosed in quote marks for the element.
- - or -
- From the Grammar Toolbox, drag a Script Tag element onto the Rule Editor pane, and drop it next to an element that should contain semantic information.
- Click the Script Tag element, click it again, and then type a text value within the quotes.
When using Semantic Script Editor to assign a constant value to an element, Semantic Script Editor displays a portion of the resulting SML output.
To assign a variable to a constant value
- From the Grammar Toolbox, drag a Script Tag element onto the Rule Editor pane, and drop it next to an element that should contain semantic information.
- Click the Script Tag element, click it again, and then click the ellipses (...) button to open Semantic Script Editor.
- On the Assignment tab, in the Assign a semantic result to this Rule's semantic output object ($) frame, do one of the following.
- To create a new SML node whose value is the returned value, select Return result in a sub-property of this Rule, and then in the Name of property box, type or select the name of the new SML node.
- To create a new SML node that has an attribute whose value is the returned value:
- Select Return result in a sub-property of this Rule, and in the Name of property box, type or select the name of the new SML node.
- Select Return result as an attribute of this Rule or sub-property, and in the Name of attribute box, type or select the name of the new attribute.
- To create an attribute on the root SML node whose value is the returned value, select Return result as an attribute of this Rule or sub-property, and in the Name of attribute box, type or select the name of the new attribute.
- In the Source of semantic result frame, select Constant, and in the Enter value text box, type a text value enclosed in quote marks.
Use the RuleRef element to refer to other rules.
To assign a variable to another rule value
- From the Grammar Toolbox, drag a Script Tag element onto the Rule Editor pane, and drop it to the right of a RuleRef element.
- Click the Script Tag element, click it again, and then click the ellipses (...) button to open Semantic Script Editor.
- On the Assignment tab, in the Assign a semantic result to this Rule's semantic output object ($) frame, do one of the following.
- To create a new SML node whose value is the returned value, select Return result in a sub-property of this Rule, and then in the Name of property box, type or select the name of the new SML node.
- To create a new SML node that has an attribute whose value is the returned value:
- Select Return result in a sub-property of this Rule, and in the Name of property box, type or select the name of the new SML node.
- Select Return result as an attribute of this Rule or sub-property, and in the Name of attribute box, type or select the name of the new attribute.
- To create an attribute on the root SML node whose value is the returned value, select Return result as an attribute of this Rule or sub-property, and in the Name of attribute box, type or select the name of the new attribute.
- In the Source of semantic result frame, select Rule Variable.
- In the Rule list, click the rule that contains the source value.
- If applicable, select Select property, and then select the property or attribute on the rule that contains the source value.
Every grammar rule has a single Rule Variable that holds a semantic value. Refer to the Rule Variable using the name of the rule. Assign values to the Rule Variable and reference it by associating scripts to tags within that rule.
Two forms of Rule Variable referencing are available.
- A Grammar Rule Name (GRN) reference is a reference to the Rule Variable of the rule in which the tag grammar XML markup element is contained.
- A Grammar Rule Reference (GRR) reference is a reference to the Rule Variable of a previous rule which occurred before that of the current tag grammar XML markup element.
Use the items in the following table to make simple variable assignments. For more information about creating proper assignment syntax, see Semantic Interpretation Markup.
Item | Example | Description |
---|---|---|
Grammar Rule Name (GRN) Rule Variable | $ | Rule Variable |
$.Prop | The Prop property of the Rule Variable | |
Grammar Rule Reference (GRR) Rule Variable | $RuleName | The RuleName Rule Variable |
$RuleName.Prop | The Prop property of the RuleName Rule Variable | |
$$ | The Rule Variable from the last rule reference | |
$$.Prop | The Prop property of the RuleName Rule Variable from the last rule reference | |
Serialization Variables | _attributes | Set the XML attribute on a variable when it is converted to an XML node in the resulting SML output. |
_attributes.text | The name of the XML attribute. | |
_value | Enables variables such as text and confidence to be set on the generated XML node. Also enables an XML node to have both text content and additional child nodes. | |
Recognizer Text and Confidence Variables | $recognized.text | The recognition text of the current rule. This is not necessarily the text of the complete spoken utterance. |
$recognized.confidence | The confidence score for the current tag grammar XML markup element. Valid values range from 0.0 to 1.0. | |
$recognized.unknownConfidence | Set to Yes if a confidence score for a current tag grammar XML markup element cannot be calculated. | |
JScript variable | VarName | VarName variable |
String | "string value" | The quoted string value |
Digits | 3, 14, 50 | Integers |
Decimals | 7.5, 20.25, 50.75 | Non-integers |
Arithmetic operators | + | Add |
- | Subtract | |
* | Multiply | |
/ | Divide | |
& | Logical AND | |
| | Logical OR |
Note An invalid entry in the Constant section converts to an empty literal (""), which produces a JScript compilation error during manual validation of a rule.
Creating Complex Operations
Write custom JScript code to handle more complex tasks that are not simple assignments. For example:
- if statements
- for, while loops
- Mathematical and string operations
- Function and variable declarations
To create a custom JScript operation
- From the Grammar Toolbox, drag a Script Tag element onto the Rule Editor pane, and drop it next to an element that should contain semantic information.
- Click the Script Tag element, click it again, and then click the ellipses (...) button to open Semantic Script Editor.
- On the Script tab, type custom JScript code.
Valid left side expressions include JScript variables and GRN Rule Variables, for example $.Prop. Valid right side expressions include GRN and JScript variables, as well as strings, arithmetic operators, and GRR Rule Variables. The following tables list accepted and rejected forms of script.
Accepted Forms | Description |
---|---|
$ = "tea" | Set the Rule Variable to a string |
$.drink = "tea" | Set the Rule Variable property to a string |
$.drink = $beverage | Set the GRN Rule Variable property drink to a previous GRR Rule Variable |
$.drink = $beverage.favorite | Set the GRN Rule Variable property drink to a previous GRR Rule Variable property |
$ = $$ | Set the Rule Variable to the last GRR Rule Variable |
$ = $$.favorite | Set the Rule Variable to the last GRR Rule Variable property |
$ = beverage | Set the Rule Variable to a JScript variable value |
beverage = $ | Set the JScript variable value to the Rule Variable |
$ = "206" + "555" + "5555" | Set the Rule Variable to "2065555555" |
$ = price + discount - -10.00 | Subtract a decimal minus ten |
$ = 0xABCD | Set the Rule Variable to a hex number |
Rejected Forms | Description |
---|---|
$Drink = "tea" | Invalid syntax |
$.attribute.text | Sub-properties not supported |
$ = 1A | Not a decimal or a digit; variables cannot start with a number |
Note An unparseable entry on the Script tab disables the Assignment tab and displays the message "The Grammar Editor could not form simple assignments from this semantic script." on the Assignment tab. This message only means that Speech Grammar Editor cannot parse the entry; it does not necessarily mean the entry is invalid.
For more information about writing semantic scripts, see Semantic Interpretation Markup.
Note The model and syntax of semantic interpretation implemented in the Microsoft Speech Application SDK Version 1.1 (SASDK) is based loosely on the World Wide Web Consortium working draft of Semantic Interpretation for Speech Recognition (W3C SISR). The SASDK does not recognize use of the shorthand notation $$$.text (the text property of the last rule reference). The scripting language supported by this specification is ECMA-327, which the SASDK implements using the Microsoft JScript interpreter.
See Also
Enabling Speech Recognition | Creating Grammars | Grammar Design