jspeech
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.
Usage
; const grammar = ; grammar; grammar; // #JSGF V1.0 utf-8 en; grammar cockney; <stairs> = apples and pears;
Creating Grammar
To create a speech grammar simply call the factory function from jspeech
with the name of the grammar and any header options.
const grammar = ;
Adding Rules
Add rules to the grammar object via the .rule()
method. A rule must have a name and an valid JSGF rule token(s).
grammar; // <name> = hello'grammar; // <greeting> = (<hello> | hey | sup);grammar; // <greet> = <greeting> buddy;
Public Rules
Only public rules are exported to a recogniser. To make a rule public use the .public
API on the grammar object.
grammarpublic; // public <friend> = everyone;
Sequences
To ensure that a rule keeps a sequence of tokens together use the .word()
method to wrap the tokens in quotes.
grammar; // <nyc> = "New York City";
Alternatives, Weights and Groups
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.
grammaralt'cities' 'London' 'Sydney' 'Tokyo'; // <cities> = London | Sydney | Tokyo;grammaralt'colours' 'red' 05 'green' 03 'blue' 08; // <colours> = /0.5/ red | /0.3/ green | /0.8/ blue;
Alternatives can also accept options which allow token groups.
grammaralt'answer' 'yes' 'no' 'maybe' group: true // creates group optional: true // wrap in [] instead of (); // <answer> = [yes | no | maybe];
Generating Format
Once a grammar has been created and all rules are defined. It can be used to generate the JSGF for use with a SpeechGrammarList.
;; const SpeechGrammarList = windowSpeechGrammarList || windowwebkitSpeechGrammarList; const list = ;list;
API
#jspeech(<name>, [header])
name
String Grammar name.header
Object Grammar header information.
Returns a grammar object.
Grammar Object
grammar.rule(<name>, [token, [opts]])
grammar.public.rule(<name>, [token, [opts]])
name
String Name of token.token
String|Array Token or array of alternative tokens.opts
Object Rule options.
Returns Void.
grammar.word(<name>, [word, [opts]])
grammar.public.word(<name>, [word, [opts]])
name
String Name of word token.word
String Word token.opts
Object Rule options.
Returns Void.
grammar.alt(<name>, [alternatives, [opts]])
grammar.public.alt(<name>, [alternatives, [opts]])
name
String Name of word token.alternatives
Array Array of alternative tokens.opts
Object Rule options.
Returns Void.
grammar.tokens()
Returns an Array of rule definitions.
grammar.stringify()
Returns a String of the generated JSGF.
License
Copyright (c) 2017 Christopher Turner