Voice.createGrammar()
Overview
Creates a new dynamic grammar for the engine to listen for. After the dynamic grammar is created, the engine will try to match speech with the terms in the provided list until the grammar is unloaded. You can have more than one grammar active at a time, so this function does not unload active grammars -- it only adds to the list of active grammars. Use Voice.unloadGrammars() to unload a specific grammar file, or Voice.clearGrammars() to unload all active grammar files.
Added in version 2.1.1
Use Cases
•You want to add terms for the engine to listen for.
•You have a file with terms for which you would like to create grammar.
•You would like to add terms based on dynamic information.
Remarks
If you attempt to create a dynamic grammar with a name that is already active, the engine will overwrite the existing one.
Limiting the number of grammars produces better speech-to-text results, so Wavelink recommends not having more than 4 grammar files active at a time.
Format
Voice.createGrammar(grammar, terms);
Parameter | Description | Type | Required |
---|---|---|---|
grammar | Name of the dynamic grammar. | String | Required |
terms | Terms that the engine will try to match. | Array of strings | Required |
Example 1
/* Loading a grammar for a field/template scope
*/
Voice.createGrammar("fruits", ["apple","pear","orange"]);
// register an event handler that will be called when the current scope is exited
WLEvent.onExitScope(function(){ Voice.unloadGrammars("fruits"); });
Example 2
/* Loading a grammar from a file located in the Download folder on the device in which the terms are separated by comma.
The content of fruits.txt looks like this: "term1,term2,term3,term4,..."
*/
var fileContent = Device.readFile("/sdcard/Download/fruits.txt");
Voice.createGrammar("fruits", fileContent);