SWI_listClass
In grammars that have one or more rules composed of large lists of phrases referenced in multiple places, you can define the rule as a SWI_listClass rule. Such a SWI_listClass rule is useful in these scenarios:
- You can reduce memory usage by defining list classes for large lists that are used repeatedly within the grammar.
- You can improve recognition accuracy by defining list classes and using n-grams to declare an associated language model.
The grammar compiler optimizes grammars to improve runtime performance. However, sometimes compilation speed and memory usage are more important restrictions. To balance these competing needs, you can use the optimization level parameter to improve runtime performance and/or define a rule as a SWI_listClass. For example, the itinerary grammar for a travel application might use all the major cities in the US. The grammar might look like this:
<rule id='itinerary'>
from <ruleref uri='#city'/> to <ruleref uri='#city'/>
</rule>
<rule id='city'>
<one-of>
<item>boston</item>
<item>chicago</item>
<item>san francisco</item>
...lots more phrases...
</one-of>
</rule>
In this case, the rule "city" is a list of phrases. It is probably large, and is referenced twice (once as the 'from' city and once as the 'to' city), which requires two copies of the "city" grammar in memory. This example is a prime candidate for declaring the rule a SWI_listClass. This is done by setting the attribute SWI_listClass to 1 in the rule definition. For example:
<rule id='city' SWI_listClass="1">
<one-of>
<item>boston</item>
<item>chicago</item>
<item>san francisco</item>
...lots more phrases...
</one-of>
</rule>
Now only one copy of "city" is kept in memory (with portions of it dynamically instantiated during runtime).
Rules that are declared as a SWI_listClass may not reference (import) other rules; only words recognizable to Recognizer (that is, non-terminal symbols) are allowed in a SWI_listClass rule. Specifically, a SWI_listClass rule can contain the following structures:
- <one-of>: A single <one-of> is allowed directly inside the <rule>.
- <item>: Only words are allowed inside an <item>. No nested elements are allowed (for example, no <ruleref> or <one-of> elements can appear inside an <item>).
Note that the potential penalties and gains from this trade-off are quite complex to determine and therefore need to be done carefully. One rule of thumb is to use this declaration when there are multiple references to a conforming rule.