From 9b2f95f163fb0b340ea0414931a1284dde7d4141 Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Sun, 10 Apr 2016 12:55:22 +0200 Subject: [PATCH] Added annyang.trigger() command Simulates speech being recognized. This will trigger the same events and behavior as when the Speech Recognition detects speech. Can accept either a string containing a single sentence, or an array containing multiple sentences to be checked in order until one of them matches a command (similar to the way Speech Recognition Alternatives are parsed) Examples: ````javascript annyang.trigger('Time for some thrilling heroics'); annyang.trigger( ['Time for some thrilling heroics', 'Time for some thrilling aerobics'] ); ```` --- annyang.js | 103 ++++++++++++++++++++++++++++------------- annyang.min.js | 2 +- docs/README.md | 24 ++++++++++ sites/facebook.min.js | 2 +- sites/geektime.min.js | 2 +- test/spec/BasicSpec.js | 11 ++--- 6 files changed, 104 insertions(+), 40 deletions(-) diff --git a/annyang.js b/annyang.js index 6a91503..0573856 100644 --- a/annyang.js +++ b/annyang.js @@ -94,6 +94,39 @@ } }; + var parseResults = function(results) { + invokeCallbacks(callbacks.result, results); + var commandText; + // go over each of the 5 results and alternative results received (we've set maxAlternatives to 5 above) + for (var i = 0; ia?setTimeout(c.start,1e3-a):c.start()}},e.onresult=function(a){if(l)return j&&console.log("Speech heard, but annyang is paused"),!1;for(var b=a.results[a.resultIndex],c=[],d=0;di;i++){var n=g[i],o=n.command.exec(e);if(o){var p=o.slice(1);return j&&(console.log("command matched: %c"+n.originalPhrase,k),p.length&&console.log("with parameters",p)),n.callback.apply(this,p),t(h.resultMatch,e,n.originalPhrase,c),!0}}}return t(h.resultNoMatch,c),!1},o&&(g=[]),n.length&&this.addCommands(n)},start:function(a){l=!1,v(),a=a||{},f=a.autoRestart!==b?!!a.autoRestart:!0,a.continuous!==b&&(e.continuous=!!a.continuous),i=(new Date).getTime();try{e.start()}catch(c){j&&console.log(c.message)}},abort:function(){f=!1,u()&&e.abort()},pause:function(){l=!0},resume:function(){c.start()},debug:function(a){j=arguments.length>0?!!a:!0},setLanguage:function(a){v(),e.lang=a},addCommands:function(b){var c;v();for(var d in b)if(b.hasOwnProperty(d))if(c=a[b[d]]||b[d],"function"==typeof c)w(s(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){j&&console.log("Can not register command: %c"+d,k);continue}w(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(a){return a===b?void(g=[]):(a=Array.isArray(a)?a:[a],void(g=g.filter(function(b){for(var c=0;cd;d++){var f=g[d],i=f.command.exec(b);if(i){var l=i.slice(1);return j&&(console.log("command matched: %c"+f.originalPhrase,k),l.length&&console.log("with parameters",l)),f.callback.apply(this,l),void t(h.resultMatch,b,f.originalPhrase,a)}}}t(h.resultNoMatch,a)};return c={init:function(k,n){n=n===b?!0:!!n,e&&e.abort&&e.abort(),e=new d,e.maxAlternatives=5,e.continuous="http:"===a.location.protocol,e.lang="en-US",e.onstart=function(){m=!0,t(h.start)},e.onerror=function(a){switch(t(h.error),a.error){case"network":t(h.errorNetwork);break;case"not-allowed":case"service-not-allowed":f=!1,t((new Date).getTime()-i<200?h.errorPermissionBlocked:h.errorPermissionDenied)}},e.onend=function(){if(m=!1,t(h.end),f){var a=(new Date).getTime()-i;1e3>a?setTimeout(c.start,1e3-a):c.start()}},e.onresult=function(a){if(l)return j&&console.log("Speech heard, but annyang is paused"),!1;for(var b=a.results[a.resultIndex],c=[],d=0;d0?!!a:!0},setLanguage:function(a){v(),e.lang=a},addCommands:function(b){var c;v();for(var d in b)if(b.hasOwnProperty(d))if(c=a[b[d]]||b[d],"function"==typeof c)w(s(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){j&&console.log("Can not register command: %c"+d,k);continue}w(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(a){return a===b?void(g=[]):(a=Array.isArray(a)?a:[a],void(g=g.filter(function(b){for(var c=0;ca?setTimeout(c.start,1e3-a):c.start()}},e.onresult=function(a){if(l)return j&&console.log("Speech heard, but annyang is paused"),!1;for(var b=a.results[a.resultIndex],c=[],d=0;di;i++){var n=g[i],o=n.command.exec(e);if(o){var p=o.slice(1);return j&&(console.log("command matched: %c"+n.originalPhrase,k),p.length&&console.log("with parameters",p)),n.callback.apply(this,p),t(h.resultMatch,e,n.originalPhrase,c),!0}}}return t(h.resultNoMatch,c),!1},o&&(g=[]),n.length&&this.addCommands(n)},start:function(a){l=!1,v(),a=a||{},f=a.autoRestart!==b?!!a.autoRestart:!0,a.continuous!==b&&(e.continuous=!!a.continuous),i=(new Date).getTime();try{e.start()}catch(c){j&&console.log(c.message)}},abort:function(){f=!1,u()&&e.abort()},pause:function(){l=!0},resume:function(){c.start()},debug:function(a){j=arguments.length>0?!!a:!0},setLanguage:function(a){v(),e.lang=a},addCommands:function(b){var c;v();for(var d in b)if(b.hasOwnProperty(d))if(c=a[b[d]]||b[d],"function"==typeof c)w(s(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){j&&console.log("Can not register command: %c"+d,k);continue}w(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(a){return a===b?void(g=[]):(a=Array.isArray(a)?a:[a],void(g=g.filter(function(b){for(var c=0;cd;d++){var f=g[d],i=f.command.exec(b);if(i){var l=i.slice(1);return j&&(console.log("command matched: %c"+f.originalPhrase,k),l.length&&console.log("with parameters",l)),f.callback.apply(this,l),void t(h.resultMatch,b,f.originalPhrase,a)}}}t(h.resultNoMatch,a)};return c={init:function(k,n){n=n===b?!0:!!n,e&&e.abort&&e.abort(),e=new d,e.maxAlternatives=5,e.continuous="http:"===a.location.protocol,e.lang="en-US",e.onstart=function(){m=!0,t(h.start)},e.onerror=function(a){switch(t(h.error),a.error){case"network":t(h.errorNetwork);break;case"not-allowed":case"service-not-allowed":f=!1,t((new Date).getTime()-i<200?h.errorPermissionBlocked:h.errorPermissionDenied)}},e.onend=function(){if(m=!1,t(h.end),f){var a=(new Date).getTime()-i;1e3>a?setTimeout(c.start,1e3-a):c.start()}},e.onresult=function(a){if(l)return j&&console.log("Speech heard, but annyang is paused"),!1;for(var b=a.results[a.resultIndex],c=[],d=0;d0?!!a:!0},setLanguage:function(a){v(),e.lang=a},addCommands:function(b){var c;v();for(var d in b)if(b.hasOwnProperty(d))if(c=a[b[d]]||b[d],"function"==typeof c)w(s(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){j&&console.log("Can not register command: %c"+d,k);continue}w(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(a){return a===b?void(g=[]):(a=Array.isArray(a)?a:[a],void(g=g.filter(function(b){for(var c=0;ca?setTimeout(c.start,1e3-a):c.start()}},e.onresult=function(a){if(l)return j&&console.log("Speech heard, but annyang is paused"),!1;for(var b=a.results[a.resultIndex],c=[],d=0;di;i++){var n=g[i],o=n.command.exec(e);if(o){var p=o.slice(1);return j&&(console.log("command matched: %c"+n.originalPhrase,k),p.length&&console.log("with parameters",p)),n.callback.apply(this,p),t(h.resultMatch,e,n.originalPhrase,c),!0}}}return t(h.resultNoMatch,c),!1},o&&(g=[]),n.length&&this.addCommands(n)},start:function(a){l=!1,v(),a=a||{},f=a.autoRestart!==b?!!a.autoRestart:!0,a.continuous!==b&&(e.continuous=!!a.continuous),i=(new Date).getTime();try{e.start()}catch(c){j&&console.log(c.message)}},abort:function(){f=!1,u()&&e.abort()},pause:function(){l=!0},resume:function(){c.start()},debug:function(a){j=arguments.length>0?!!a:!0},setLanguage:function(a){v(),e.lang=a},addCommands:function(b){var c;v();for(var d in b)if(b.hasOwnProperty(d))if(c=a[b[d]]||b[d],"function"==typeof c)w(s(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){j&&console.log("Can not register command: %c"+d,k);continue}w(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(a){return a===b?void(g=[]):(a=Array.isArray(a)?a:[a],void(g=g.filter(function(b){for(var c=0;cd;d++){var f=g[d],i=f.command.exec(b);if(i){var l=i.slice(1);return j&&(console.log("command matched: %c"+f.originalPhrase,k),l.length&&console.log("with parameters",l)),f.callback.apply(this,l),void t(h.resultMatch,b,f.originalPhrase,a)}}}t(h.resultNoMatch,a)};return c={init:function(k,n){n=n===b?!0:!!n,e&&e.abort&&e.abort(),e=new d,e.maxAlternatives=5,e.continuous="http:"===a.location.protocol,e.lang="en-US",e.onstart=function(){m=!0,t(h.start)},e.onerror=function(a){switch(t(h.error),a.error){case"network":t(h.errorNetwork);break;case"not-allowed":case"service-not-allowed":f=!1,t((new Date).getTime()-i<200?h.errorPermissionBlocked:h.errorPermissionDenied)}},e.onend=function(){if(m=!1,t(h.end),f){var a=(new Date).getTime()-i;1e3>a?setTimeout(c.start,1e3-a):c.start()}},e.onresult=function(a){if(l)return j&&console.log("Speech heard, but annyang is paused"),!1;for(var b=a.results[a.resultIndex],c=[],d=0;d0?!!a:!0},setLanguage:function(a){v(),e.lang=a},addCommands:function(b){var c;v();for(var d in b)if(b.hasOwnProperty(d))if(c=a[b[d]]||b[d],"function"==typeof c)w(s(d),c,d);else{if(!("object"==typeof c&&c.regexp instanceof RegExp)){j&&console.log("Can not register command: %c"+d,k);continue}w(new RegExp(c.regexp.source,"i"),c.callback,d)}},removeCommands:function(a){return a===b?void(g=[]):(a=Array.isArray(a)?a:[a],void(g=g.filter(function(b){for(var c=0;c