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());nameString Grammar name.headerObject Grammar header information.
Returns a grammar object.
nameString Name of token.tokenString|Array Token or array of alternative tokens.optsObject Rule options.
Returns Void.
nameString Name of word token.wordString Word token.optsObject Rule options.
Returns Void.
nameString Name of word token.alternativesArray Array of alternative tokens.optsObject Rule options.
Returns Void.
Returns an Array of rule definitions.
Returns a String of the generated JSGF.
Copyright (c) 2017 Christopher Turner