├── annyang.min.js ├── asistente.html └── asistente.js /annyang.min.js: -------------------------------------------------------------------------------- 1 | "use strict"; var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) { return typeof e } : function (e) { return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e }; 2 | //! annyang 3 | //! version : 2.6.1 4 | //! author : Tal Ater @TalAter 5 | //! license : MIT 6 | //! https://www.TalAter.com/annyang/ 7 | //! annyang 8 | //! version : 2.6.1 9 | //! author : Tal Ater @TalAter 10 | //! license : MIT 11 | //! https://www.TalAter.com/annyang/ 12 | !function (e, n) { "function" == typeof define && define.amd ? define([], function () { return e.annyang = n(e) }) : "object" === ("undefined" == typeof module ? "undefined" : _typeof(module)) && module.exports ? module.exports = n(e) : e.annyang = n(e) }("undefined" != typeof window ? window : void 0, function (r, i) { var t, o = r.SpeechRecognition || r.webkitSpeechRecognition || r.mozSpeechRecognition || r.msSpeechRecognition || r.oSpeechRecognition; if (!o) return null; var a, c, s = [], u = { start: [], error: [], end: [], soundstart: [], result: [], resultMatch: [], resultNoMatch: [], errorNetwork: [], errorPermissionBlocked: [], errorPermissionDenied: [] }, f = 0, l = 0, d = !1, p = "font-weight: bold; color: #00f;", g = !1, m = !1, h = /\s*\((.*?)\)\s*/g, y = /(\(\?:[^)]+\))\?/g, b = /(\(\?)?:\w+/g, v = /\*\w+/g, w = /[\-{}\[\]+?.,\\\^$|#]/g, S = function (e) { for (var n = arguments.length, t = Array(1 < n ? n - 1 : 0), o = 1; o < n; o++)t[o - 1] = arguments[o]; e.forEach(function (e) { e.callback.apply(e.context, t) }) }, e = function () { return a !== i }, k = function (e, n) { -1 !== e.indexOf("%c") || n ? console.log(e, n || p) : console.log(e) }, x = function () { e() || t.init({}, !1) }, R = function (e, n, t) { s.push({ command: e, callback: n, originalPhrase: t }), d && k("Command successfully loaded: %c" + t, p) }, P = function (e) { var n; S(u.result, e); for (var t = 0; t < e.length; t++) { n = e[t].trim(), d && k("Speech recognized: %c" + n, p); for (var o = 0, r = s.length; o < r; o++) { var i = s[o], a = i.command.exec(n); if (a) { var c = a.slice(1); return d && (k("command matched: %c" + i.originalPhrase, p), c.length && k("with parameters", c)), i.callback.apply(this, c), void S(u.resultMatch, n, i.originalPhrase, e) } } } S(u.resultNoMatch, e) }; return t = { init: function (e) { var n = !(1 < arguments.length && arguments[1] !== i) || arguments[1]; a && a.abort && a.abort(), (a = new o).maxAlternatives = 5, a.continuous = "http:" === r.location.protocol, a.lang = "en-US", a.onstart = function () { m = !0, S(u.start) }, a.onsoundstart = function () { S(u.soundstart) }, a.onerror = function (e) { switch (S(u.error, e), e.error) { case "network": S(u.errorNetwork, e); break; case "not-allowed": case "service-not-allowed": c = !1, (new Date).getTime() - f < 200 ? S(u.errorPermissionBlocked, e) : S(u.errorPermissionDenied, e) } }, a.onend = function () { if (m = !1, S(u.end), c) { var e = (new Date).getTime() - f; (l += 1) % 10 == 0 && d && k("Speech Recognition is repeatedly stopping and starting. See http://is.gd/annyang_restarts for tips."), e < 1e3 ? setTimeout(function () { t.start({ paused: g }) }, 1e3 - e) : t.start({ paused: g }) } }, a.onresult = function (e) { if (g) return d && k("Speech heard, but annyang is paused"), !1; for (var n = e.results[e.resultIndex], t = [], o = 0; o < n.length; o++)t[o] = n[o].transcript; P(t) }, n && (s = []), e.length && this.addCommands(e) }, start: function (e) { x(), g = (e = e || {}).paused !== i && !!e.paused, c = e.autoRestart === i || !!e.autoRestart, e.continuous !== i && (a.continuous = !!e.continuous), f = (new Date).getTime(); try { a.start() } catch (e) { d && k(e.message) } }, abort: function () { c = !1, l = 0, e() && a.abort() }, pause: function () { g = !0 }, resume: function () { t.start() }, debug: function () { var e = !(0 < arguments.length && arguments[0] !== i) || arguments[0]; d = !!e }, setLanguage: function (e) { x(), a.lang = e }, addCommands: function (e) { var n, t; for (var o in x(), e) if (e.hasOwnProperty(o)) if ("function" == typeof (n = r[e[o]] || e[o])) R((t = (t = o).replace(w, "\\$&").replace(h, "(?:$1)?").replace(b, function (e, n) { return n ? e : "([^\\s]+)" }).replace(v, "(.*?)").replace(y, "\\s*$1?\\s*"), new RegExp("^" + t + "$", "i")), n, o); else { if (!("object" === (void 0 === n ? "undefined" : _typeof(n)) && n.regexp instanceof RegExp)) { d && k("Can not register command: %c" + o, p); continue } R(new RegExp(n.regexp.source, "i"), n.callback, o) } }, removeCommands: function (t) { t === i ? s = [] : (t = Array.isArray(t) ? t : [t], s = s.filter(function (e) { for (var n = 0; n < t.length; n++)if (t[n] === e.originalPhrase) return !1; return !0 })) }, addCallback: function (e, n, t) { var o = r[n] || n; "function" == typeof o && u[e] !== i && u[e].push({ callback: o, context: t || this }) }, removeCallback: function (e, n) { var t = function (e) { return e.callback !== n }; for (var o in u) u.hasOwnProperty(o) && (e !== i && e !== o || (u[o] = n === i ? [] : u[o].filter(t))) }, isListening: function () { return m && !g }, getSpeechRecognizer: function () { return a }, trigger: function (e) { t.isListening() ? (Array.isArray(e) || (e = [e]), P(e)) : d && k(m ? "Speech heard, but annyang is paused" : "Cannot trigger while annyang is aborted") } } }); -------------------------------------------------------------------------------- /asistente.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Asistente Virtual El Taller De TD 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /asistente.js: -------------------------------------------------------------------------------- 1 | /* 2 | Programa creado por youtube.com/ElTallerDeTD 3 | 20/2/20 4 | https://github.com/imTDB 5 | */ 6 | 7 | if (annyang) { 8 | 9 | //Variable para almacenar las voces de nuestro sistema. 10 | var voices; 11 | 12 | //Inicializamos utter. 13 | var utter = new SpeechSynthesisUtterance(); 14 | utter.rate = 1; 15 | utter.pitch = 0.5; 16 | utter.lang = 'es-AR'; 17 | 18 | //Cargamos las voces que tenemos en nuestro sistema y las mostarmos en un arreglo por consola. 19 | window.speechSynthesis.onvoiceschanged = function () { 20 | voices = window.speechSynthesis.getVoices(); 21 | console.log(voices); 22 | }; 23 | 24 | //Definimos los comandos a utilizar. 25 | var commands = { 26 | 'hola maria': function () { 27 | utter.text = 'Hola usuario'; 28 | //Setea la voz que queremos usar en base a nuestra lista. 29 | utter.voice = voices[2]; 30 | window.speechSynthesis.speak(utter); 31 | }, 32 | 'como estas': function () { 33 | utter.text = 'Muy bien!'; 34 | utter.voice = voices[2]; 35 | window.speechSynthesis.speak(utter); 36 | }, 37 | 'hola': function () { 38 | utter.text = 'hola, cual es tu nombre?'; 39 | utter.voice = voices[2]; 40 | window.speechSynthesis.speak(utter); 41 | //Guarda el nombre que le decimos por voz. 42 | annyang.addCallback('result', function (phrases) { 43 | //Imprime el nombre por consola. 44 | console.log("Nombre: ", phrases[0]); 45 | //Para el evento result. 46 | annyang.removeCallback('result'); 47 | //Nos dice hola + el nombre que le digamos por voz. 48 | utter.text = 'Hola, ' + phrases[0]; 49 | window.speechSynthesis.speak(utter); 50 | }); 51 | }, 52 | //Array que devuelve aleatoriamente un elemento del array, en este caso un chiste. 53 | 'cuentame un chiste': function () { 54 | chistes = ['Por qué las focas del circo miran siempre hacia arriba? Porque es donde están los focos.', 55 | 'Estas obsesionado con la comida! No se a que te refieres croquetamente.', 56 | 'Por que estás hablando con esas zapatillas? Porque pone "converse"', 57 | 'Buenos dias, me gustaria alquilar "Batman Forever". No es posible, tiene que devolverla tomorrow.' 58 | ]; 59 | utter.text = chistes[Math.floor(Math.random() * chistes.length)] 60 | utter.voice = voices[2]; 61 | window.speechSynthesis.speak(utter); 62 | } 63 | }; 64 | 65 | //Esto nos sirve para ver que escucha el programa en tiempo real. 66 | /* 67 | annyang.addCallback('result', function(phrases) { 68 | console.log("I think the user said: ", phrases[0]); 69 | console.log("But then again, it could be any of the following: ", phrases); 70 | }); 71 | */ 72 | 73 | 74 | //Sumamos todos los comandos a annyang. 75 | annyang.addCommands(commands); 76 | 77 | //Annyang comienza a escuchar. 78 | annyang.start({ autoRestart: false, continuous: true }); 79 | } --------------------------------------------------------------------------------