annyang 2.6.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +55 -31
  3. package/dist/annyang.cjs +354 -0
  4. package/dist/annyang.cjs.map +1 -0
  5. package/dist/annyang.d.cts +288 -0
  6. package/dist/annyang.d.ts +288 -0
  7. package/dist/annyang.iife.min.js +1 -0
  8. package/dist/annyang.js +296 -670
  9. package/dist/annyang.js.map +1 -1
  10. package/package.json +54 -36
  11. package/.editorconfig +0 -13
  12. package/.eslintrc.json +0 -34
  13. package/.gitattributes +0 -1
  14. package/.github/ISSUE_TEMPLATE.md +0 -32
  15. package/.github/PULL_REQUEST_TEMPLATE.md +0 -29
  16. package/.jshintrc +0 -31
  17. package/.travis.yml +0 -20
  18. package/CONTRIBUTING.md +0 -75
  19. package/Gruntfile.js +0 -144
  20. package/bower.json +0 -25
  21. package/demo/css/main.css +0 -152
  22. package/demo/css/main.min.css +0 -1
  23. package/demo/images/flickr.png +0 -0
  24. package/demo/images/footer_background.jpg +0 -0
  25. package/demo/images/icon_js.png +0 -0
  26. package/demo/images/icon_speech.png +0 -0
  27. package/demo/images/icon_user.png +0 -0
  28. package/demo/images/mini_icon_say.png +0 -0
  29. package/demo/images/palette.png +0 -0
  30. package/demo/images/tpscover.jpg +0 -0
  31. package/demo/index.html +0 -225
  32. package/demo/vendor/css/default.css +0 -135
  33. package/demo/vendor/css/github.css +0 -127
  34. package/demo/vendor/css/tomorrow.css +0 -49
  35. package/demo/vendor/html/github-btn.html +0 -243
  36. package/demo/vendor/js/highlight.pack.js +0 -1
  37. package/dist/annyang.min.js +0 -12
  38. package/docs/FAQ.md +0 -223
  39. package/docs/README.md +0 -336
  40. package/require.js +0 -36
  41. package/sites/README.md +0 -6
  42. package/sites/facebook.js +0 -123
  43. package/sites/facebook.min.js +0 -17
  44. package/sites/geektime.js +0 -108
  45. package/sites/geektime.min.js +0 -17
  46. package/src/annyang.js +0 -680
  47. package/test/SpecRunner.html +0 -41
  48. package/test/init_corti.js +0 -1
  49. package/test/spec/.jshintrc +0 -40
  50. package/test/spec/BasicSpec.js +0 -1616
  51. package/test/spec/IssuesSpec.js +0 -41
  52. package/test/vendor/corti.js +0 -202
  53. package/yarn.lock +0 -4190
package/src/annyang.js DELETED
@@ -1,680 +0,0 @@
1
- //! annyang
2
- //! version : 2.6.1
3
- //! author : Tal Ater @TalAter
4
- //! license : MIT
5
- //! https://www.TalAter.com/annyang/
6
- (function (root, factory) {
7
- 'use strict';
8
- if (typeof define === 'function' && define.amd) { // AMD + global
9
- define([], function () {
10
- return (root.annyang = factory(root));
11
- });
12
- } else if (typeof module === 'object' && module.exports) { // CommonJS
13
- module.exports = factory(root);
14
- } else { // Browser globals
15
- root.annyang = factory(root);
16
- }
17
- }(typeof window !== 'undefined' ? window : this, function (root, undefined) {
18
- 'use strict';
19
-
20
- /**
21
- * # Quick Tutorial, Intro and Demos
22
- *
23
- * The quickest way to get started is to visit the [annyang homepage](https://www.talater.com/annyang/).
24
- *
25
- * For a more in-depth look at annyang, read on.
26
- *
27
- * # API Reference
28
- */
29
-
30
- var annyang;
31
-
32
- // Get the SpeechRecognition object, while handling browser prefixes
33
- var SpeechRecognition = root.SpeechRecognition ||
34
- root.webkitSpeechRecognition ||
35
- root.mozSpeechRecognition ||
36
- root.msSpeechRecognition ||
37
- root.oSpeechRecognition;
38
-
39
- // Check browser support
40
- // This is done as early as possible, to make it as fast as possible for unsupported browsers
41
- if (!SpeechRecognition) {
42
- return null;
43
- }
44
-
45
- var commandsList = [];
46
- var recognition;
47
- var callbacks = { start: [], error: [], end: [], soundstart: [], result: [], resultMatch: [], resultNoMatch: [], errorNetwork: [], errorPermissionBlocked: [], errorPermissionDenied: [] };
48
- var autoRestart;
49
- var lastStartedAt = 0;
50
- var autoRestartCount = 0;
51
- var debugState = false;
52
- var debugStyle = 'font-weight: bold; color: #00f;';
53
- var pauseListening = false;
54
- var isListening = false;
55
-
56
- // The command matching code is a modified version of Backbone.Router by Jeremy Ashkenas, under the MIT license.
57
- var optionalParam = /\s*\((.*?)\)\s*/g;
58
- var optionalRegex = /(\(\?:[^)]+\))\?/g;
59
- var namedParam = /(\(\?)?:\w+/g;
60
- var splatParam = /\*\w+/g;
61
- var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#]/g;
62
- var commandToRegExp = function(command) {
63
- command = command.replace(escapeRegExp, '\\$&')
64
- .replace(optionalParam, '(?:$1)?')
65
- .replace(namedParam, function(match, optional) {
66
- return optional ? match : '([^\\s]+)';
67
- })
68
- .replace(splatParam, '(.*?)')
69
- .replace(optionalRegex, '\\s*$1?\\s*');
70
- return new RegExp('^' + command + '$', 'i');
71
- };
72
-
73
- // This method receives an array of callbacks to iterate over, and invokes each of them
74
- var invokeCallbacks = function(callbacks, ...args) {
75
- callbacks.forEach(function(callback) {
76
- callback.callback.apply(callback.context, args);
77
- });
78
- };
79
-
80
- var isInitialized = function() {
81
- return recognition !== undefined;
82
- };
83
-
84
- // method for logging in developer console when debug mode is on
85
- var logMessage = function(text, extraParameters) {
86
- if (text.indexOf('%c') === -1 && !extraParameters) {
87
- console.log(text);
88
- } else {
89
- console.log(text, extraParameters || debugStyle);
90
- }
91
- };
92
-
93
- var initIfNeeded = function() {
94
- if (!isInitialized()) {
95
- annyang.init({}, false);
96
- }
97
- };
98
-
99
- var registerCommand = function(command, callback, originalPhrase) {
100
- commandsList.push({ command, callback, originalPhrase });
101
- if (debugState) {
102
- logMessage('Command successfully loaded: %c'+originalPhrase, debugStyle);
103
- }
104
- };
105
-
106
- var parseResults = function(results) {
107
- invokeCallbacks(callbacks.result, results);
108
- var commandText;
109
- // go over each of the 5 results and alternative results received (we've set maxAlternatives to 5 above)
110
- for (let i = 0; i<results.length; i++) {
111
- // the text recognized
112
- commandText = results[i].trim();
113
- if (debugState) {
114
- logMessage('Speech recognized: %c'+commandText, debugStyle);
115
- }
116
-
117
- // try and match recognized text to one of the commands on the list
118
- for (let j = 0, l = commandsList.length; j < l; j++) {
119
- var currentCommand = commandsList[j];
120
- var result = currentCommand.command.exec(commandText);
121
- if (result) {
122
- var parameters = result.slice(1);
123
- if (debugState) {
124
- logMessage('command matched: %c'+currentCommand.originalPhrase, debugStyle);
125
- if (parameters.length) {
126
- logMessage('with parameters', parameters);
127
- }
128
- }
129
- // execute the matched command
130
- currentCommand.callback.apply(this, parameters);
131
- invokeCallbacks(callbacks.resultMatch, commandText, currentCommand.originalPhrase, results);
132
- return;
133
- }
134
- }
135
- }
136
- invokeCallbacks(callbacks.resultNoMatch, results);
137
- };
138
-
139
- annyang = {
140
-
141
- /**
142
- * Initialize annyang with a list of commands to recognize.
143
- *
144
- * #### Examples:
145
- * ````javascript
146
- * var commands = {'hello :name': helloFunction};
147
- * var commands2 = {'hi': helloFunction};
148
- *
149
- * // initialize annyang, overwriting any previously added commands
150
- * annyang.init(commands, true);
151
- * // adds an additional command without removing the previous commands
152
- * annyang.init(commands2, false);
153
- * ````
154
- * 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.
155
- *
156
- * @param {Object} commands - Commands that annyang should listen to
157
- * @param {boolean} [resetCommands=true] - Remove all commands before initializing?
158
- * @method init
159
- * @deprecated
160
- * @see [Commands Object](#commands-object)
161
- */
162
- init: function(commands, resetCommands = true) {
163
- // Abort previous instances of recognition already running
164
- if (recognition && recognition.abort) {
165
- recognition.abort();
166
- }
167
-
168
- // initiate SpeechRecognition
169
- recognition = new SpeechRecognition();
170
-
171
- // Set the max number of alternative transcripts to try and match with a command
172
- recognition.maxAlternatives = 5;
173
-
174
- // In HTTPS, turn off continuous mode for faster results.
175
- // In HTTP, turn on continuous mode for much slower results, but no repeating security notices
176
- recognition.continuous = root.location.protocol === 'http:';
177
-
178
- // Sets the language to the default 'en-US'. This can be changed with annyang.setLanguage()
179
- recognition.lang = 'en-US';
180
-
181
- recognition.onstart = function() {
182
- isListening = true;
183
- invokeCallbacks(callbacks.start);
184
- };
185
-
186
- recognition.onsoundstart = function() {
187
- invokeCallbacks(callbacks.soundstart);
188
- };
189
-
190
- recognition.onerror = function(event) {
191
- invokeCallbacks(callbacks.error, event);
192
- switch (event.error) {
193
- case 'network':
194
- invokeCallbacks(callbacks.errorNetwork, event);
195
- break;
196
- case 'not-allowed':
197
- case 'service-not-allowed':
198
- // if permission to use the mic is denied, turn off auto-restart
199
- autoRestart = false;
200
- // determine if permission was denied by user or automatically.
201
- if (new Date().getTime()-lastStartedAt < 200) {
202
- invokeCallbacks(callbacks.errorPermissionBlocked, event);
203
- } else {
204
- invokeCallbacks(callbacks.errorPermissionDenied, event);
205
- }
206
- break;
207
- }
208
- };
209
-
210
- recognition.onend = function() {
211
- isListening = false;
212
- invokeCallbacks(callbacks.end);
213
- // annyang will auto restart if it is closed automatically and not by user action.
214
- if (autoRestart) {
215
- // play nicely with the browser, and never restart annyang automatically more than once per second
216
- var timeSinceLastStart = new Date().getTime()-lastStartedAt;
217
- autoRestartCount += 1;
218
- if (autoRestartCount % 10 === 0) {
219
- if (debugState) {
220
- logMessage('Speech Recognition is repeatedly stopping and starting. See http://is.gd/annyang_restarts for tips.');
221
- }
222
- }
223
- if (timeSinceLastStart < 1000) {
224
- setTimeout(function() {
225
- annyang.start({ paused: pauseListening });
226
- }, 1000-timeSinceLastStart);
227
- } else {
228
- annyang.start({ paused: pauseListening });
229
- }
230
- }
231
- };
232
-
233
- recognition.onresult = function(event) {
234
- if(pauseListening) {
235
- if (debugState) {
236
- logMessage('Speech heard, but annyang is paused');
237
- }
238
- return false;
239
- }
240
-
241
- // Map the results to an array
242
- var SpeechRecognitionResult = event.results[event.resultIndex];
243
- var results = [];
244
- for (let k = 0; k<SpeechRecognitionResult.length; k++) {
245
- results[k] = SpeechRecognitionResult[k].transcript;
246
- }
247
-
248
- parseResults(results);
249
- };
250
-
251
- // build commands list
252
- if (resetCommands) {
253
- commandsList = [];
254
- }
255
- if (commands.length) {
256
- this.addCommands(commands);
257
- }
258
- },
259
-
260
- /**
261
- * Start listening.
262
- * It's a good idea to call this after adding some commands first, but not mandatory.
263
- *
264
- * Receives an optional options object which supports the following options:
265
- *
266
- * - `autoRestart` (boolean, default: true) Should annyang restart itself if it is closed indirectly, because of silence or window conflicts?
267
- * - `continuous` (boolean) Allow forcing continuous mode on or off. Annyang is pretty smart about this, so only set this if you know what you're doing.
268
- * - `paused` (boolean, default: true) Start annyang in paused mode.
269
- *
270
- * #### Examples:
271
- * ````javascript
272
- * // Start listening, don't restart automatically
273
- * annyang.start({ autoRestart: false });
274
- * // Start listening, don't restart automatically, stop recognition after first phrase recognized
275
- * annyang.start({ autoRestart: false, continuous: false });
276
- * ````
277
- * @param {Object} [options] - Optional options.
278
- * @method start
279
- */
280
- start: function(options) {
281
- initIfNeeded();
282
- options = options || {};
283
- if (options.paused !== undefined) {
284
- pauseListening = !!options.paused;
285
- } else {
286
- pauseListening = false;
287
- }
288
- if (options.autoRestart !== undefined) {
289
- autoRestart = !!options.autoRestart;
290
- } else {
291
- autoRestart = true;
292
- }
293
- if (options.continuous !== undefined) {
294
- recognition.continuous = !!options.continuous;
295
- }
296
-
297
- lastStartedAt = new Date().getTime();
298
- try {
299
- recognition.start();
300
- } catch(e) {
301
- if (debugState) {
302
- logMessage(e.message);
303
- }
304
- }
305
- },
306
-
307
- /**
308
- * Stop listening, and turn off mic.
309
- *
310
- * Alternatively, to only temporarily pause annyang responding to commands without stopping the SpeechRecognition engine or closing the mic, use pause() instead.
311
- * @see [pause()](#pause)
312
- *
313
- * @method abort
314
- */
315
- abort: function() {
316
- autoRestart = false;
317
- autoRestartCount = 0;
318
- if (isInitialized()) {
319
- recognition.abort();
320
- }
321
- },
322
-
323
- /**
324
- * Pause listening. annyang will stop responding to commands (until the resume or start methods are called), without turning off the browser's SpeechRecognition engine or the mic.
325
- *
326
- * Alternatively, to stop the SpeechRecognition engine and close the mic, use abort() instead.
327
- * @see [abort()](#abort)
328
- *
329
- * @method pause
330
- */
331
- pause: function() {
332
- pauseListening = true;
333
- },
334
-
335
- /**
336
- * Resumes listening and restores command callback execution when a result matches.
337
- * If SpeechRecognition was aborted (stopped), start it.
338
- *
339
- * @method resume
340
- */
341
- resume: function() {
342
- annyang.start();
343
- },
344
-
345
- /**
346
- * Turn on output of debug messages to the console. Ugly, but super-handy!
347
- *
348
- * @param {boolean} [newState=true] - Turn on/off debug messages
349
- * @method debug
350
- */
351
- debug: function(newState = true) {
352
- debugState = !!newState;
353
- },
354
-
355
- /**
356
- * Set the language the user will speak in. If this method is not called, defaults to 'en-US'.
357
- *
358
- * @param {String} language - The language (locale)
359
- * @method setLanguage
360
- * @see [Languages](https://github.com/TalAter/annyang/blob/master/docs/FAQ.md#what-languages-are-supported)
361
- */
362
- setLanguage: function(language) {
363
- initIfNeeded();
364
- recognition.lang = language;
365
- },
366
-
367
- /**
368
- * Add commands that annyang will respond to. Similar in syntax to init(), but doesn't remove existing commands.
369
- *
370
- * #### Examples:
371
- * ````javascript
372
- * var commands = {'hello :name': helloFunction, 'howdy': helloFunction};
373
- * var commands2 = {'hi': helloFunction};
374
- *
375
- * annyang.addCommands(commands);
376
- * annyang.addCommands(commands2);
377
- * // annyang will now listen to all three commands
378
- * ````
379
- *
380
- * @param {Object} commands - Commands that annyang should listen to
381
- * @method addCommands
382
- * @see [Commands Object](#commands-object)
383
- */
384
- addCommands: function(commands) {
385
- var cb;
386
-
387
- initIfNeeded();
388
-
389
- for (let phrase in commands) {
390
- if (commands.hasOwnProperty(phrase)) {
391
- cb = root[commands[phrase]] || commands[phrase];
392
- if (typeof cb === 'function') {
393
- // convert command to regex then register the command
394
- registerCommand(commandToRegExp(phrase), cb, phrase);
395
- } else if (typeof cb === 'object' && cb.regexp instanceof RegExp) {
396
- // register the command
397
- registerCommand(new RegExp(cb.regexp.source, 'i'), cb.callback, phrase);
398
- } else {
399
- if (debugState) {
400
- logMessage('Can not register command: %c'+phrase, debugStyle);
401
- }
402
- continue;
403
- }
404
- }
405
- }
406
- },
407
-
408
- /**
409
- * Remove existing commands. Called with a single phrase, array of phrases, or methodically. Pass no params to remove all commands.
410
- *
411
- * #### Examples:
412
- * ````javascript
413
- * var commands = {'hello': helloFunction, 'howdy': helloFunction, 'hi': helloFunction};
414
- *
415
- * // Remove all existing commands
416
- * annyang.removeCommands();
417
- *
418
- * // Add some commands
419
- * annyang.addCommands(commands);
420
- *
421
- * // Don't respond to hello
422
- * annyang.removeCommands('hello');
423
- *
424
- * // Don't respond to howdy or hi
425
- * annyang.removeCommands(['howdy', 'hi']);
426
- * ````
427
- * @param {String|Array|Undefined} [commandsToRemove] - Commands to remove
428
- * @method removeCommands
429
- */
430
- removeCommands: function(commandsToRemove) {
431
- if (commandsToRemove === undefined) {
432
- commandsList = [];
433
- } else {
434
- commandsToRemove = Array.isArray(commandsToRemove) ? commandsToRemove : [commandsToRemove];
435
- commandsList = commandsList.filter(command => {
436
- for (let i = 0; i<commandsToRemove.length; i++) {
437
- if (commandsToRemove[i] === command.originalPhrase) {
438
- return false;
439
- }
440
- }
441
- return true;
442
- });
443
- }
444
- },
445
-
446
- /**
447
- * Add a callback function to be called in case one of the following events happens:
448
- *
449
- * * `start` - Fired as soon as the browser's Speech Recognition engine starts listening
450
- * * `soundstart` - Fired as soon as any sound (possibly speech) has been detected.
451
- * This will fire once per Speech Recognition starting. See https://is.gd/annyang_sound_start
452
- * * `error` - Fired when the browser's Speech Recogntion engine returns an error, this generic error callback will be followed by more accurate error callbacks (both will fire if both are defined)
453
- * Callback function will be called with the error event as the first argument
454
- * * `errorNetwork` - Fired when Speech Recognition fails because of a network error
455
- * Callback function will be called with the error event as the first argument
456
- * * `errorPermissionBlocked` - Fired when the browser blocks the permission request to use Speech Recognition.
457
- * Callback function will be called with the error event as the first argument
458
- * * `errorPermissionDenied` - Fired when the user blocks the permission request to use Speech Recognition.
459
- * Callback function will be called with the error event as the first argument
460
- * * `end` - Fired when the browser's Speech Recognition engine stops
461
- * * `result` - Fired as soon as some speech was identified. This generic callback will be followed by either the `resultMatch` or `resultNoMatch` callbacks.
462
- * Callback functions for to this event will be called with an array of possible phrases the user said as the first argument
463
- * * `resultMatch` - Fired when annyang was able to match between what the user said and a registered command
464
- * Callback functions for this event will be called with three arguments in the following order:
465
- * * The phrase the user said that matched a command
466
- * * The command that was matched
467
- * * An array of possible alternative phrases the user might have said
468
- * * `resultNoMatch` - Fired when what the user said didn't match any of the registered commands.
469
- * Callback functions for this event will be called with an array of possible phrases the user might've said as the first argument
470
- *
471
- * #### Examples:
472
- * ````javascript
473
- * annyang.addCallback('error', function() {
474
- * $('.myErrorText').text('There was an error!');
475
- * });
476
- *
477
- * annyang.addCallback('resultMatch', function(userSaid, commandText, phrases) {
478
- * console.log(userSaid); // sample output: 'hello'
479
- * console.log(commandText); // sample output: 'hello (there)'
480
- * console.log(phrases); // sample output: ['hello', 'halo', 'yellow', 'polo', 'hello kitty']
481
- * });
482
- *
483
- * // pass local context to a global function called notConnected
484
- * annyang.addCallback('errorNetwork', notConnected, this);
485
- * ````
486
- * @param {String} type - Name of event that will trigger this callback
487
- * @param {Function} callback - The function to call when event is triggered
488
- * @param {Object} [context] - Optional context for the callback function
489
- * @method addCallback
490
- */
491
- addCallback: function(type, callback, context) {
492
- var cb = root[callback] || callback;
493
- if (typeof cb === 'function' && callbacks[type] !== undefined) {
494
- callbacks[type].push({callback: cb, context: context || this});
495
- }
496
- },
497
-
498
- /**
499
- * Remove callbacks from events.
500
- *
501
- * - Pass an event name and a callback command to remove that callback command from that event type.
502
- * - Pass just an event name to remove all callback commands from that event type.
503
- * - Pass undefined as event name and a callback command to remove that callback command from all event types.
504
- * - Pass no params to remove all callback commands from all event types.
505
- *
506
- * #### Examples:
507
- * ````javascript
508
- * annyang.addCallback('start', myFunction1);
509
- * annyang.addCallback('start', myFunction2);
510
- * annyang.addCallback('end', myFunction1);
511
- * annyang.addCallback('end', myFunction2);
512
- *
513
- * // Remove all callbacks from all events:
514
- * annyang.removeCallback();
515
- *
516
- * // Remove all callbacks attached to end event:
517
- * annyang.removeCallback('end');
518
- *
519
- * // Remove myFunction2 from being called on start:
520
- * annyang.removeCallback('start', myFunction2);
521
- *
522
- * // Remove myFunction1 from being called on all events:
523
- * annyang.removeCallback(undefined, myFunction1);
524
- * ````
525
- *
526
- * @param type Name of event type to remove callback from
527
- * @param callback The callback function to remove
528
- * @returns undefined
529
- * @method removeCallback
530
- */
531
- removeCallback: function(type, callback) {
532
- var compareWithCallbackParameter = function(cb) {
533
- return cb.callback !== callback;
534
- };
535
- // Go over each callback type in callbacks store object
536
- for (let callbackType in callbacks) {
537
- if (callbacks.hasOwnProperty(callbackType)) {
538
- // if this is the type user asked to delete, or he asked to delete all, go ahead.
539
- if (type === undefined || type === callbackType) {
540
- // If user asked to delete all callbacks in this type or all types
541
- if (callback === undefined) {
542
- callbacks[callbackType] = [];
543
- } else {
544
- // Remove all matching callbacks
545
- callbacks[callbackType] = callbacks[callbackType].filter(compareWithCallbackParameter);
546
- }
547
- }
548
- }
549
- }
550
- },
551
-
552
- /**
553
- * Returns true if speech recognition is currently on.
554
- * Returns false if speech recognition is off or annyang is paused.
555
- *
556
- * @return boolean true = SpeechRecognition is on and annyang is listening
557
- * @method isListening
558
- */
559
- isListening: function() {
560
- return isListening && !pauseListening;
561
- },
562
-
563
- /**
564
- * Returns the instance of the browser's SpeechRecognition object used by annyang.
565
- * Useful in case you want direct access to the browser's Speech Recognition engine.
566
- *
567
- * @returns SpeechRecognition The browser's Speech Recognizer currently used by annyang
568
- * @method getSpeechRecognizer
569
- */
570
- getSpeechRecognizer: function() {
571
- return recognition;
572
- },
573
-
574
- /**
575
- * Simulate speech being recognized. This will trigger the same events and behavior as when the Speech Recognition
576
- * detects speech.
577
- *
578
- * Can accept either a string containing a single sentence, or an array containing multiple sentences to be checked
579
- * in order until one of them matches a command (similar to the way Speech Recognition Alternatives are parsed)
580
- *
581
- * #### Examples:
582
- * ````javascript
583
- * annyang.trigger('Time for some thrilling heroics');
584
- * annyang.trigger(
585
- * ['Time for some thrilling heroics', 'Time for some thrilling aerobics']
586
- * );
587
- * ````
588
- *
589
- * @param string|array sentences A sentence as a string or an array of strings of possible sentences
590
- * @returns undefined
591
- * @method trigger
592
- */
593
- trigger: function(sentences) {
594
- if(!annyang.isListening()) {
595
- if (debugState) {
596
- if (!isListening) {
597
- logMessage('Cannot trigger while annyang is aborted');
598
- } else {
599
- logMessage('Speech heard, but annyang is paused');
600
- }
601
- }
602
- return;
603
- }
604
-
605
- if (!Array.isArray(sentences)) {
606
- sentences = [sentences];
607
- }
608
-
609
- parseResults(sentences);
610
- }
611
- };
612
-
613
- return annyang;
614
-
615
- }));
616
-
617
- /**
618
- * # Good to Know
619
- *
620
- * ## Commands Object
621
- *
622
- * Both the [init()]() and addCommands() methods receive a `commands` object.
623
- *
624
- * annyang understands commands with `named variables`, `splats`, and `optional words`.
625
- *
626
- * * Use `named variables` for one word arguments in your command.
627
- * * Use `splats` to capture multi-word text at the end of your command (greedy).
628
- * * Use `optional words` or phrases to define a part of the command as optional.
629
- *
630
- * #### Examples:
631
- * ````html
632
- * <script>
633
- * var commands = {
634
- * // annyang will capture anything after a splat (*) and pass it to the function.
635
- * // e.g. saying "Show me Batman and Robin" will call showFlickr('Batman and Robin');
636
- * 'show me *tag': showFlickr,
637
- *
638
- * // A named variable is a one word variable, that can fit anywhere in your command.
639
- * // e.g. saying "calculate October stats" will call calculateStats('October');
640
- * 'calculate :month stats': calculateStats,
641
- *
642
- * // By defining a part of the following command as optional, annyang will respond
643
- * // to both: "say hello to my little friend" as well as "say hello friend"
644
- * 'say hello (to my little) friend': greeting
645
- * };
646
- *
647
- * var showFlickr = function(tag) {
648
- * var url = 'http://api.flickr.com/services/rest/?tags='+tag;
649
- * $.getJSON(url);
650
- * }
651
- *
652
- * var calculateStats = function(month) {
653
- * $('#stats').text('Statistics for '+month);
654
- * }
655
- *
656
- * var greeting = function() {
657
- * $('#greeting').text('Hello!');
658
- * }
659
- * </script>
660
- * ````
661
- *
662
- * ### Using Regular Expressions in commands
663
- * For advanced commands, you can pass a regular expression object, instead of
664
- * a simple string command.
665
- *
666
- * This is done by passing an object containing two properties: `regexp`, and
667
- * `callback` instead of the function.
668
- *
669
- * #### Examples:
670
- * ````javascript
671
- * var calculateFunction = function(month) { console.log(month); }
672
- * var commands = {
673
- * // This example will accept any word as the "month"
674
- * 'calculate :month stats': calculateFunction,
675
- * // This example will only accept months which are at the start of a quarter
676
- * 'calculate :quarter stats': {'regexp': /^calculate (January|April|July|October) stats$/, 'callback': calculateFunction}
677
- * }
678
- ````
679
- *
680
- */