jspeech
v0.1.1
Published
Create JSpeech Grammar Formats (JSGF).
Downloads
368
Readme
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
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;
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 = jspeech('name', {
version: 'V1.0',
lang: 'en',
encoding: 'utf-8',
});
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.rule('hello', 'hello'); // <name> = hello'
grammar.rule('greeting', '(<hello> | hey | sup)'); // <greeting> = (<hello> | hey | sup);
grammar.rule('greet', '<greeting> buddy'); // <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.
grammar.public.rule('friend', 'everyone'); // 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.word('nyc', 'New York City'); // <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.
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];
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.
import window from 'global/window';
import grammar from './grammar';
const SpeechGrammarList = window.SpeechGrammarList || window.webkitSpeechGrammarList;
const list = new SpeechGrammarList();
list.addFromString(grammar.stringify());
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