// Initialize annyang with a list of commands to recognize.
// e.g. annyang.init({'hello :name': helloFunction})
/**
// annyang understands commands with named variables, splats, and optional words.
* Initialize annyang with a list of commands to recognize.
*
* ### Examples:
*
* var commands = {'hello :name': helloFunction};
* var commands2 = {'hi': helloFunction};
*
* // initialize annyang, overwriting any previously added commands
* annyang.init(commands, true);
* // adds an additional command without removing the previous commands
* annyang.init(commands2, false);
*
* As of v1.1.0 it is no longer required to call init(). Just start() listening whenever you want, and addCommands() whenever, and as often as you like.
*
* @param {Object} commands - Commands that annyang should listen to
* @param {Boolean} [resetCommands=true] - Remove all commands before initializing?
* @method init
* @deprecated
* @see [Commands Object](#commands-object)
*/
init:function(commands,resetCommands){
init:function(commands,resetCommands){
// resetCommands defaults to true
// resetCommands defaults to true
...
@@ -74,8 +109,6 @@
...
@@ -74,8 +109,6 @@
resetCommands=!!resetCommands;
resetCommands=!!resetCommands;
}
}
try{
// Abort previous instances of recognition already running
// Abort previous instances of recognition already running
if(recognition&&recognition.abort){
if(recognition&&recognition.abort){
recognition.abort();
recognition.abort();
...
@@ -85,17 +118,15 @@
...
@@ -85,17 +118,15 @@
recognition=newSpeechRecognition();
recognition=newSpeechRecognition();
// Set the max number of alternative transcripts to try and match with a command
// Set the max number of alternative transcripts to try and match with a command
recognition.maxAlternatives=5;
recognition.maxAlternatives=5;
recognition.continuous=true;
// In HTTPS, turn off continuous mode for faster results.
// In HTTP, turn on continuous mode for much slower results, but no repeating security notices
* @param {String} type - Name of event that will trigger this callback
* @param {Function} callback - The function to call when event is triggered
* @param {Object} [context] - Optional context for the callback function
* @method addCallback
*/
addCallback:function(type,callback,context){
addCallback:function(type,callback,context){
if(callbacks[type]===undefined){
if(callbacks[type]===undefined){
return;
return;
...
@@ -272,3 +392,131 @@
...
@@ -272,3 +392,131 @@
};
};
}).call(this);
}).call(this);
/**
* # Good to Know
*
* ## Commands Object
*
* Both the [init()]() and addCommands() methods receive a `commands` object.
*
* annyang understands commands with `named variables`, `splats`, and `optional words`.
*
* * Use `named variables` for one word arguments in your command.
* * Use `splats` to capture multi-word text at the end of your command (greedy).
* * Use `optional words` or phrases to define a part of the command as optional.
*
* ### Examples:
*
* <script>
* var commands = {
* // annyang will capture anything after a splat (*) and pass it to the function.
* // e.g. saying "Show me Batman and Robin" will call showFlickr('Batman and Robin');
* 'show me *term': showFlickr,
*
* // A named variable is a one word variable, that can fit anywhere in your command.
* // e.g. saying "calculate October stats" will call calculateStats('October');
* 'calculate :month stats': calculateStats,
*
* // By defining a part of the following command as optional, annyang will respond
* // to both: "say hello to my little friend" as well as "say hello friend"
* 'say hello (to my little) friend': greeting
* };
*
* var showFlickr = function(term) {
* var url = 'http://api.flickr.com/services/rest/?tags='+tag;
* $.getJSON(url);
* }
*
* var calculateStats = function(month) {
* $('#stats').text('Statistics for '+month);
* }
*
* var greeting = function() {
* $('#greeting').text('Hello!');
* }
* </script>
*
* ## Languages
*
* While there isn't an official list of supported languages (cultures? locales?), here is a list based on [anecdotal evidence](http://stackoverflow.com/a/14302134/338039).