Multiple parses

Ideally, each possible sentence in the grammar has a unique parse. Occasionally, a grammar may allow multiple parses of a single sentence. For example, a grammar might intentionally allow numerous interpretations (parses) of a single string; this would enable the grammar to maintain a rudimentary database of different meanings for the same phrase in different contexts.

The following grammar lets people ask for the scores of sporting events. If the caller says “giants”, it could mean the San Francisco Giants or the New York Giants. More specifically, the value of TEAM can be set to one or the other depending on which path is taken through the grammar.

<?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" scope="public">
  <item> 
   <ruleref uri="#Scores" />
   <tag>TEAM=Scores.TEAM </tag> 
  </item>
 </rule>
 <rule id="Scores">
  <ruleref uri="#Request"/>
  <one-of>
   <item> 
    <ruleref uri="#Bteam" />
    <tag>TEAM=Bteam.TEAM </tag> 
   </item>
   <item> 
    <ruleref uri="#Fteam" />
    <tag>TEAM=Fteam.TEAM </tag> 
   </item>
  </one-of>
 </rule>
 <rule id="Request">
  <item repeat="0-1"> give me </item>
  <item repeat="0-1"> the 
   <item repeat="0-1"> latest </item>
   <item repeat="0-1">
    <item repeat="0-1"> sports </item>
    scores for 
    <item repeat="0-1"> the </item>
   </item>
  </item>
 </rule>
<rule id="Fteam">
  <one-of>
   <item> san francisco </item>
   <item> san francisco giants </item>
   <item> giants </item>
  </one-of>
  <tag>TEAM='San Francisco Giants';</tag>
 </rule>
 <rule id="Bteam">
  <one-of>
   <item> new york giants </item>
   <item> giants </item>
   <item> new york </item>
  </one-of>
  <tag>TEAM='New York Giants';</tag>
 </rule>
</grammar>

Usually, multiple parses indicate an oversight in the design of the grammar that needs correction. But in the grammar above, multiple parses are intentional and useful and there is no reason to remove them. However, you must be careful when your application asks for the value of a key that appears in the parses, because each parse can set the value differently.

You can limit the total number of multiple parses Recognizer will perform by setting swirec_max_parses_per_literal in a <meta> element in the grammar header.