Importing grammars with different tag formats
This topic describes permutations of grammar imports when the syntax of semantic tags in a child grammar is different from the syntax in the parent.
Recognizer supports semantic tags in three tag-formats (semantics/1.0, semantics/1.0-literals, and swi-semantics/1.0). A mixed-format import can include any combination of these syntaxes, as well as a wordlist grammar (see Wordlist (directory-assistance) grammars).
The grammar compiler accepts such mixed-format imports, and handles them in a consistent way for each particular combination of formats.

This example shows a grammar import where the parent uses SISR script tag syntax and the child uses swi tag syntax.
Here is the imported grammar (sub.grxml). The swi tag syntax is the default tag-format for grammars (swi-semantics/1.0). The system assumes this format if the grammar omits the tag-format attribute.
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="SUBROOT"
tag-format="swi-semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="SUBROOT">
elephant
<tag>
A = "mammal"
SWI_meaning = "animal"
</tag>
</rule>
</grammar>
Below is the parent grammar (main.grxml). Note the following:
- The SISR script tag syntax is specified as 'semantics/1.0'.
- SWIrules.current() is undefined, and SWIrules.latest() holds the value of the SWI variable of the last referenced rule. This behavior is similar to the SISR rules variable in that rules.current() is undefined and rules.latest() holds the value of the Rule Variable of the last referenced rule.
After referencing a swi syntax rule, meta.latest() is undefined because the swi rule has no meta variable.
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="ROOT"
tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="main">
<ruleref uri="#one"/>
<ruleref uri="sub.grxml"/>
flies
<tag>
if (SWIrules.current() == undefined &&
rules.current() == undefined &&
meta.latest() == undefined) {
SWI.SWI_meaning = SWIrules.one.SWI_meaning + " " +
SWIrules.latest().SWI_meaning;
out.other = rules.SUBROOT.A;
<!-- above could be written: rules.latest().A -->
}
</tag>
</rule>
<rule id="one">
blue
<tag>SWI.SWI_meaning="colorful"</tag>
</rule>
</grammar>
Here is the relevant portion of the XML recognition result. We assume the utterance "blue elephant flies" was spoken (this example assumes that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal):
<instance>
<other confidence="97">mammal</other>
<SWI_meaning>colorful animal</SWI_meaning>
<SWI_literal>Blue elephant flies</SWI_literal>
</instance>

This example shows a grammar import where the parent uses SISR script tag syntax and the child uses SISR string literals tag syntax. The subgrammar:
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="SUBROOT"
tag-format="semantics/1.0-literals"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="SUBROOT">
elephant
<tag>animal </tag>
</rule>
</grammar>
Below is the parent grammar (main.grxml). Note the following:
- The SISR script tag syntax is defined as 'semantics/1.0'
- After referencing a string literals rule, SWIrules.latest().SWI_literal (or alternatively, SWIrules.rulename.SWI_literal) is defined. The grammar parser automatically sets the keys SWI_confidence, SWI_literal and SWI_spoken on SWIrules.latest().
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="main"
tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="main">
blue
<ruleref uri="sub.grxml"/>
flies
<tag>
SWI.SWI_meaning = SWIrules.latest().SWI_literal + " " + meta.latest().text;
out.other = rules.SUBROOT;
</tag>
</rule>
</grammar>
Here is the relevant portion of the XML recognition result. We assume the utterance "blue elephant flies" was spoken (this example assumes that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal):
<instance>
<other confidence="97">animal</other>
<SWI_meaning>elephant elephant</SWI_meaning>
<SWI_literal>Blue elephant flies</SWI_literal>
<SWI_spoken>Blue elephant flies</SWI_spoken>
</instance>

This example shows a grammar import where the parent uses SISR string literals tag syntax and the child uses swi tag syntax.
Here is the imported grammar (sub.grxml). The swi tag syntax is defined as 'swi-semantics/1.0'. The system assumes this format if the grammar omits the tag-format attribute:
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="SUBROOT"
tag-format="swi-semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="SUBROOT">
elephant
<tag>
A = "mammal";
SWI_meaning = "animal";
</tag>
</rule>
</grammar>
Below is the parent grammar (main.grxml). Note the following:
- The SISR string literals tag syntax is defined as 'semantics/1.0-literals'
- SWI_meaning is set to the value of the top-level grammar's Rule Variable.
<?xml version="1.0" encoding="UTF-8"?>
<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0-literals"
root="main" xml:lang="en-US">
<rule id="main">
blue
<ruleref uri="sub.grxml"/>
flies
<tag>myliteral</tag>
</rule>
</grammar>
Here is the relevant portion of the XML recognition result. We assume the utterance "blue elephant flies" was spoken (this example assumes that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal):
<instance>
myliteral
<SWI_meaning>myliteral</SWI_meaning>
<SWI_literal>Blue elephant flies</SWI_literal>
</instance>

This example shows a grammar import where the parent uses swi tag syntax and the child uses SISR script syntax.
Here is the imported grammar (sub.grxml). The SISR script tag syntax is defined as 'semantics/1.0':
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="SUBROOT"
tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="SUBROOT">
<tag>
SWI.SWI_meaning = meta.current().text;
out.A = SWI_vars.X
</tag>
elephant
</rule>
</grammar>
Below is the parent grammar (main.grxml). Note the following:
- The swi tag syntax is defined as 'swi-semantics/1.0'. The system assumes this format if the grammar omits the tag-format attribute.
- SWI_vars can be used with SISR grammars.
The properties that were set on the out and SWI objects are all accessible in the parent swi grammar.
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="main"
tag-format="swi-semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="main">
blue
<ruleref uri='sub.grxml?SWI_vars.X=hello'/>
<tag>
SWI_meaning = SUBROOT.SWI_meaning;
answer = SUBROOT.A;
</tag>
flies
</rule>
</grammar>
Here is the relevant portion of the XML recognition result. We assume the utterance "blue elephant flies" was spoken (this example assumes that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal):
<instance>
<answer confidence="99">hello</answer>
<SWI_meaning>elephant</SWI_meaning>
<SWI_literal>Blue elephant flies</SWI_literal>
</instance>

This example shows a grammar import where the parent uses swi tag syntax and the child uses SISR string literals syntax.
Here is the imported grammar (sub.grxml). The SISR string literals tag syntax is defined as 'semantics/1.0-literals':
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="SUBROOT"
tag-format="semantics/1.0-literals"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="SUBROOT">
<tag>animal</tag>
elephant
</rule>
</grammar>
Below is the parent grammar (main.grxml). Note the following:
- The swi tag syntax is defined as 'swi-semantics/1.0'. The system assumes this format if the grammar omits the tag-format attribute.
- The parent swi grammar cannot access the Rule Variable of the referenced string literals rule (because the Rule Variable is a string rather than an object with a property).
- The grammar parser automatically sets SWI_confidence, SWI_literal and SWI_spoken in the child grammar. It does not set SWI_meaning.
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="head"
tag-format="swi-semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="head">
blue
<ruleref uri='sub.grxml'/>
flies
<tag>
if (SUBROOT.SWI_meaning == undefined) {
answer =SUBROOT.SWI_literal;
}
</tag>
</rule>
</grammar>
Here is the relevant portion of the XML recognition result. We assume the utterance "blue elephant flies" was spoken (this example assumes that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal):
<instance>
<answer>elephant</answer>
<SWI_meaning>{answer:elephant}</SWI_meaning>
<SWI_literal>Blue elephant flies</SWI_literal>
<SWI_spoken>Blue elephant flies</SWI_spoken>
</instance>

The SISR specification describes “default assignment”, a situation that arises when there are no semantics scripts to execute for a given parse through a rule. If there is a <ruleref> in the parse, then the Rule Variable is assigned the value of the Rule Variable of the last <ruleref>. Otherwise, it is assigned the value of the text meta variable.
This example shows default assignment for the case where the parent grammar uses SISR script tag syntax and the child uses swi tag syntax The SWI keys of the child rule are copied to the SWI variable of the parent rule. The remaining (non-SWI) keys are copied to the Rule Variable of the parent rule.
Here is the imported grammar (sub.grxml). The swi tag syntax is defined as 'swi-semantics/1.0' (the system assumes this format if the grammar omits the tag-format attribute):
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="ROOT"
tag-format="swi-semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="ROOT">
elephant
<tag>
A = "mammal";
SWI_meaning = "animal";
</tag>
</rule>
</grammar>
Below is the parent grammar (main.grxml). Note the following:
- The SISR script tag syntax is defined as 'semantics/1.0'
- The root rule contains no script.
The root rule contains two <ruleref> elements. Per the SISR specification, the default assignment is made from the second <ruleref>.
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="main"
tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="main">
<ruleref uri="#one"/>
blue
<ruleref uri="sub.grxml"/>
flies
</rule>
<rule id="one">
blue
<tag>SWI.SWI_meaning="not returned"</tag>
</rule>
</grammar>
Here is the relevant portion of the XML recognition result. We assume the utterance "blue elephant flies" was spoken (this example assumes that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal):
<instance>
<A confidence="97">mammal</answer>
<SWI_meaning>animal</SWI_meaning>
<SWI_literal>Blue elephant flies</SWI_literal>
</instance>

These examples show a grammar import where the parent uses SISR script tag syntax and the child is a wordlist grammar (see Wordlist (directory-assistance) grammars).
Although wordlist grammars never contain semantic scripts, the parse result returned from a wordlist changes depending on the tag syntax of the wordlist.
If the wordlist uses SISR tag syntax (either script or string literals syntax), it returns a string containing the recognized text. It also sets meta.current().score and meta.current().text, which can then be accessed by a SISR tag syntax grammar that imports the wordlist, as meta.latest().score and meta.latest().text.
Here is the parent grammar:
<?xml version='1.0' encoding='UTF-8'?>
<grammar xml:lang="en-US" version="1.0" root="ROOT"
tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="ROOT">
<ruleref uri="wordlist.txt"/>
</rule>
</grammar>
Here is the relevant portion of the XML recognition result when the imported grammar (wordlist.txt) uses SISR tag syntax. We assume the utterance "mark fanta" was spoken, and that swirec_extra_nbest_keys is set to SWI_meaning and SWI_literal:
<instance>
mark_fanta
<SWI_literal>Mark_fanta</SWI_literal>
<SWI_meaning>mark_fanta</SWI_meaning>
</instance>
Here is the relevant portion of the same recognition result when the imported grammar (wordlist.txt) uses swi tag syntax:
<instance>
<SWI_literal>Mark_fanta</SWI_literal>
<SWI_meaning>mark_fanta</SWI_meaning>
</instance>
Related topics
Related topics
Reference
Related parameters