Working with Grammars

When a grammar is loaded, it is compiled into a format usable by the Engine. But to use the grammar for a decode you must activate it. You may activate multiple grammars for a single decode; the results will contain which grammar was matched.

Because loading a grammar takes longer than activating it, it is often desirable to load all the grammars you will need for an application in advance, and activate and deactivate grammars as needed for a given decode (loading grammars will be faster the second time by default because the Engine will cache grammars). It is good practice to only activate the grammars you need for a specific decode, as having extra vocabulary items in the active grammar can reduce recognition accuracy.

A good idea is to build different grammars for specific uses in your application. For instance, when building a telephony IVR, you might use a different grammar for each prompt, with a few global grammars for the always-available options. You could load all the grammars into a port at the start of the application, and as the user moves through prompts you could activate the grammar for a specific prompt and then deactivate it when the user moves to the next menu.

When you ask the Engine to perform a decode, you will have to specify the grammar to be used (this is mainly when using the older concept/phrase grammar type). When using SRGS grammars, the collection of all the active grammars is called the active grammar set; to decode against this set you provide LV_ACTIVE_GRAMMAR_SET as the grammar name.

C Code

/*
* Activates the "yes_no" grammar that was loaded above.
* Activate searches for a grammar named "yes_no" in its port,
* then searches the global space if it can't find it.
*/
LV_SRE_ActivateGrammar (hport, "yes_no");

/*
* Although ActivateGrammar will activate global grammars, it is a good
* practice to specifically activate global grammars if you want to load
* the global grammar, in case you have a conflict between local and global
* grammar labels. Note that a global grammar is only active on the port
* for which it was activated.
*/
LV_SRE_ActivateGlobalGrammar (hport, "nav_menu");


C++ Code

port.ActivateGrammar ("yes_no");

port.ActivateGlobalGrammar ("nav_menu");

Deactivating Grammars

In the same way that you can activate grammars, you can deactivate grammars.

C Code

LV_SRE_DeactivateGrammar (hport, "yes_no");

LV_SRE_DeactivateGrammars ();


C++ Code

port.DeactivateGrammar ("yes_no");

port.DeactivateGrammars ();

See Also

© 2012 LumenVox LLC. All rights reserved.