Node.js module for creating JSpeech Grammar Formats, JSGF.
Note: jspeech
is just a api for creating JSGF. The format specification can be read here: https://www.w3.org/TR/jsgf.
import jspeech from 'jspeech';
const grammar = jspeech('cockney');
grammar.rule('stairs', 'apples and pears');
grammar.stringify(); // #JSGF V1.0 utf-8 en; grammar cockney; <stairs> = apples and pears;
To create a speech grammar simply call the factory function from jspeech
with the name of the grammar and any header options.
const grammar = jspeech('name', {
version: 'V1.0',
lang: 'en',
encoding: 'utf-8',
});
Add rules to the grammar object via the .rule()
method. A rule must have a name and an valid JSGF rule token(s).
grammar.rule('hello', 'hello'); // <name> = hello'
grammar.rule('greeting', '(<hello> | hey | sup)'); // <greeting> = (<hello> | hey | sup);
grammar.rule('greet', '<greeting> buddy'); // <greet> = <greeting> buddy;
Only public rules are exported to a recogniser. To make a rule public use the .public
API on the grammar object.
grammar.public.rule('friend', 'everyone'); // public <friend> = everyone;
To ensure that a rule keeps a sequence of tokens together use the .word()
method to wrap the tokens in quotes.
grammar.word('nyc', 'New York City'); // <nyc> = "New York City";
Alternatives allow variations of different rules and/or rule tokens. This allows for a more complex grammar format. Use the .alt()
method to create different rule alternatives.
grammar.alt('cities', ['London', 'Sydney', 'Tokyo']); // <cities> = London | Sydney | Tokyo;
grammar.alt('colours', [
['red', 0.5],
['green', 0.3],
['blue', 0.8],
]); // <colours> = /0.5/ red | /0.3/ green | /0.8/ blue;
Alternatives can also accept options which allow token groups.
grammar.alt('answer', ['yes', 'no', 'maybe'], {
group: true, // creates group
optional: true, // wrap in [] instead of ()
}); // <answer> = [yes | no | maybe];
Once a grammar has been created and all rules are defined. It can be used to generate the JSGF for use with a SpeechGrammarList.
import window from 'global/window';
import grammar from './grammar';
const SpeechGrammarList = window.SpeechGrammarList || window.webkitSpeechGrammarList;
const list = new SpeechGrammarList();
list.addFromString(grammar.stringify());
name
String Grammar name.header
Object Grammar header information.
Returns a grammar object.
name
String Name of token.token
String|Array Token or array of alternative tokens.opts
Object Rule options.
Returns Void.
name
String Name of word token.word
String Word token.opts
Object Rule options.
Returns Void.
name
String Name of word token.alternatives
Array Array of alternative tokens.opts
Object Rule options.
Returns Void.
Returns an Array of rule definitions.
Returns a String of the generated JSGF.
Copyright (c) 2017 Christopher Turner