Compiling Grammars

When your speech application first begins the process of decoding audio, it sends a grammar with the audio to the Speech Engine. In order for the Engine to read the grammar, the grammar has to be compiled into a binary format that the Engine can read.

It used to be that the Engine had to load and compile its grammars each time that a grammar was used, which was time consuming and used up significant resources in the process. Grammars with thousands of words can take several seconds to compile.

If you are using a large grammar, you probably do not want to wait several seconds to load it every time your application needs to use it. This is especially true for telephony applications, where you cannot have a caller waiting while the Engine compiles a grammar.

In Speech Engine version 8.0 and later, the Engine has incorporated a feature which keeps compiled grammars in its cache automatically, allowing you to use large grammars without having to wait for them to be compiled after the very first time a grammar is loaded.

Grammars, once loaded, do not need to be compiled again if no changes have been made to them.

Understanding this process is made easier if you understand the distinction between the LumenVox client and server. The client is the application which uses our SpeechPort library — it may be a custom application you have built using our API, our own MRCP server, or a voice platform that has done an API-level integration of LumenVox.

The client loads grammars and audio and sends them in a packet to the speech server, which is the process that uses the grammar and audio to perform decodes. It is the server that needs the compiled grammar.

Because clients and servers may reside on different machines using different environments and architectures, the speech server performing the decode must compile the grammar. A binary grammar file compiled on a Sun SPARC machine running Solaris will not work with one compiled on an Intel machine running Windows.

Before the server compiles the grammar, it takes a hash of the grammar(a hash is a way to quickly create a unique identifier for a file, and if the contents of a file change even slightly, so does the hash).

The server saves the hash and compiles the grammar, writing a copy of the compiled grammar to disk.

The next time a grammar is passed to the server, the server takes a hash of the grammar and checks to see if it already has a compiled version of it in cache. If so, it uses the precompiled one, and saves itself the resources and time of compilation.

If the hashes don't match, then there has been a change to the grammar, and the Engine compiles the new one.

All of this means that there will only be a delay the first time a grammar is loaded. After that, grammar caching ensures the Engine does not recompile the same grammar.

Grammar Loader

If you wish to avoid any grammar-compilation delay in the decode process, you may pre-compile the grammars using the grammar loader tool provided with Speech Engine version 10.0 and later. This tool separates the process of compiling grammars from the decode process, allowing you to control when grammar compilation occurs.

On Windows, the tool is called GrammarLoader, and is located at

%LVBIN%\GrammarLoader

On Linux, the tool is calls lv_grammar_loader, and is located at

/usr/bin/lv_grammar_loader

To use the grammar loader, specify a grammar, the IP address of a machine running the SRE, and an optional timeout value in seconds. If you run the tool without any arguments, a usage message will remind you of these arguments.

Usage: GrammarLoader.exe <grammar-file> <server-ip> [timeout]
timeout (in seconds) indicates the timeout for the grammar load call
Defaults to 3600 (1 hour) if none specified

If a timeout value is not given, the grammar is compiled synchronously, and the tool will return after compilation is complete or after 1 hour has passed (whichever is earlier). If a timeout is given, and the grammar compilation process takes longer than the time specified, the tool will print a message and return before grammar compilation is complete, however the compilation process will continue at the server.

If you load a grammar that has already been compiled, a success message will be printed and the tool will return immediately. In this way, you can verify that a previously submitted grammar has been compiled successfully.

Example on Windows, loading digits.gram on an SRE running on the local machine, with a timeout of 30 seconds. The grammar is compiled before the timeout is exceeded, and the success message is printed.

C:\Program Files\LumenVox\Engine\GrammarLoader digits.gram 127.0.0.1 30
Loaded grammar digits.gram successfully!
© 2012 LumenVox LLC. All rights reserved.