Working with Grammars

Grammars tell the Speech Engine what words and phrases can be recognized by the Engine, and in what order. The LumenVox grammar format is an implementation of Speech Recognition Grammar Specification, published by the W3C. A short tutorial on writing SRGS grammars is provided here.

The Speech Engine also supports an older style of grammars called concept/phrase pairs. We recommend that you use SRGS grammars over concept/phrase, as SRGS provides a more robust method of grammar creation. The example code throughout this guide will assume you are using SRGS grammars.

Loading A Grammar

In order to decode audio, there must be at least one grammar loaded. Grammars can be loaded a variety of ways. When loading a grammar, you must also supply a label for each grammar. You will use this label to refer to the grammar in future functions.

Grammars can have two scopes. Global grammars are available to every port and local grammars are available only for a specific port. Applications will often have some grammars that are always available, such as global menu options and yes/no grammars. Those "always available" options are best put into global grammars.

The Speech Engine comes standard with many built-in grammars. You can load them by supplying builtin:grammar/name as a path, where name is the name of the built-in grammar.

C Code

HPORT hport;

/* Load a grammar into the global (application-level) space, and name it
* nav_menu"
* This grammar will be usable by any speech port on the client machine.
* Any syntax warnings or error messages will be sent to the
* application-level logging callback.
*/

LV_SRE_LoadGlobalGrammar
("nav_menu","c:/MyGrammars/top_level_navigation.gram");

/* Load a built-in grammar into the speech port, name it "yes_no".  
* Syntax error or warning messages
* will be sent to the port's logging callback.
* The hport needs to be open first, of course.
*/

LV_SRE_LoadGrammar
(hport, "yes_no", "builtin:grammar/boolean");

C++ Code

LVSpeechPort port;
port.OpenPort();

LVSpeechPort::LoadGlobalGrammar("nav_menu","c:/MyGrammars/top_level_navigation.gram");

port.LoadGrammar("yes_no", "builtin:grammar/boolean");

Loading Built-in Spanish Grammars

The Speech Engine also includes Spanish built-in Spanish grammars. Loading them is like loading the built-in English grammars, except their names end in _span. So builtin:grammar/boolean would become builtin:grammar/boolean_span.

Activating A Grammar

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_DeactivateGlobalGrammar
(hport, "nav_menu");
 

C++ Code

port.DeactivateGrammar("yes_no");

port.DeactivateGlobalGrammar("nav_menu");

See Also


Complete Help Topic List | Speech Engine Product Information