ng-voice-inputs
Version:
This library provides Voice Recognition service to fill the form input fields (text, datepicker, textarea, button-click) for Angular application.
2 lines • 15.5 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("rxjs"),require("moment/moment"),require("@angular/common"),require("@angular/core")):"function"==typeof define&&define.amd?define("ng-voice-inputs",["exports","rxjs","moment/moment","@angular/common","@angular/core"],e):e(t["ng-voice-inputs"]={},t.rxjs,t.moment_,t.ng.common,t.ng.core)}(this,function(t,e,i,n,o){"use strict";var a;function r(t,e){var i="function"==typeof Symbol&&t[Symbol.iterator];if(!i)return t;var n,o,r=i.call(t),s=[];try{for(;(void 0===e||0<e--)&&!(n=r.next()).done;)s.push(n.value)}catch(a){o={error:a}}finally{try{n&&!n.done&&(i=r["return"])&&i.call(r)}finally{if(o)throw o.error}}return s}function s(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(r(arguments[e]));return t}var c=i,l={zero:0,first:1,one:1,second:2,two:2,third:3,thirteenth:13,thirteen:13,three:3,fourth:4,fourteenth:14,fourteen:14,four:4,fifteenth:15,fifteen:15,fifth:5,five:5,sixth:6,sixteenth:16,sixteen:16,six:6,seventeenth:17,seventeen:17,seventh:7,seven:7,eighteenth:18,eighteen:18,eighth:8,eight:8,nineteenth:19,nineteen:19,ninth:9,nine:9,tenth:10,ten:10,eleventh:11,eleven:11,twelfth:12,twelve:12,a:1},u={ten:10,eleven:11,twelve:12,thirteen:13,fourteen:14,fifteen:15,sixteen:16,seventeen:17,eighteen:18,nineteen:19},p={twenty:20,twentieth:20,thirty:30,thirtieth:30,forty:40,fortieth:40,fifty:50,fiftieth:50,sixty:60,sixtieth:60,seventy:70,seventieth:70,eighty:80,eightieth:80,ninety:90,ninetieth:90},f={hundred:100,hundredth:100,thousand:1e3,million:1e6,billion:1e9,trillion:1e12,quadrillion:1e15,quintillion:1e18,sextillion:1e21,septillion:1e24,octillion:1e27,nonillion:1e30,decillion:1e33},h=(a=function(){return(a=Object.assign||function a(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var o in e=arguments[i])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)})({},l,p,f),d=["st","nd","rd","th"],m=["JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"],g=["forward","next"],v=["back","backward","previous"],y=["clear","clean","delete","remove"],b=["click","submit","expand","open","show"],w=["stop","end","done"],x=["scroll up","go up","up"],D=["scroll down","go down","down"],R=["scroll right","go right","right"],M=["scroll left","go left","left"],S=["scroll top","go top","top"],O=["scroll bottom","go bottom","bottom"],C=["keep scrolling","scroll","scrolling","continue scrolling"],k=s(x,D,R,M,S,O,C),I=s(["goto","switch","go"],y,b,k),k=s(g,v,w,k,["first","last"]),T=s(I,k),N={next:g,previous:v,first:["first"],last:["last"],clear:y,click:b,stop:w,scrollUp:x,scrollDown:D,scrollLeft:M,scrollRight:R,scrollTop:S,scrollBottom:O,scrollContinue:C},E=(L.prototype.isInputSwitch=function(t,e){return!("address"==e&&!t.some(function(t){return I.includes(t)}))&&!!t.some(function(t){return T.includes(t)})},L.prototype.findInstructionType=function(t,e){var i=(t=t.trim().toLocaleLowerCase()).trim().toLocaleLowerCase().split(" ");return this.isInputSwitch(i,e)?(e=Object.keys(N).find(function(e){return i.some(function(t){return N[e].includes(t)})}))?"COMMAND_"+e.toUpperCase():"":"INPUT"},L.prototype.interpretSpeech=function(t,e){var i=this,n=this.findInstructionType(t,e);if("INPUT"!=n)return n;if("text"==e||"address"==e)return t;if("number"==e)return this.wordsToNumber(t);if("date"==e){var t=(t=t.replace("from","")).trim().split("to "),o=[];if(t.forEach(function(t){o.push(i.dateParser(t))}),o[0].isValid())return o}return null},L.prototype.dateParser=function(t){var e=this,i=t,n=t.trim().split(" "),o={},r=!1,t=[];if(r=3==n.length?(t=n.map(function(t){return e.eliminateString(t)})).every(function(t){return Number.isInteger(t)}):r)i=t.join("-");else{var s=c();switch(n.forEach(function(t){o=e.parseDatePart(t,o)}),Object.keys(o).join("-")){case"date":o.month=s.format("MM");case"date-month":o.year=s.format("YYYY");break;case"year":o.month="01";case"month-year":o.date="01";break;case"month":o.year=s.format("YYYY"),o.date="01"}i=this.dateBuilder(o)}return c(i).toDate()},L.prototype.dateBuilder=function(t){return t.year+"-"+t.month+"-"+t.date},L.prototype.parseDatePart=function(t,e){var i;return this.isYearType(t)?(i=this.wordsToNumber(t),e.year=i):this.isDateType(t)?(i=this.wordsToNumber(t),e.date=i):this.isMonthType(t)&&(t=m.indexOf(t.toUpperCase())+1,e.month=t<10?"0"+t:t),e},L.prototype.isDateType=function(t){var e=!1;return(e=/\d/.test(t)&&d.includes(t.substr(t.length-2)))||0<(t=this.wordsToNumber(t))&&t<=31&&(e=!0),e},L.prototype.parseDateVal=function(e,t){var i=!1,n=0;(i=d.some(function(t){return e.includes(t)}))||0<(n=this.wordsToNumber(e))&&n<=31&&(i=!0),i&&(t.date=n)},L.prototype.isMonthType=function(t){return-1<m.indexOf(t.toUpperCase())},L.prototype.parseMonthVal=function(t,e){t=m.indexOf(t.toUpperCase());-1<t&&(e.month=t<10?"0"+t:t)},L.prototype.isYearType=function(t){return 1e3<this.wordsToNumber(t)},L.prototype.parseYearVal=function(t,e){t=this.wordsToNumber(t);1e3<t&&(e.year=t)},L.prototype.wordsToNumber=function(t){var e=this;if(!Number.isNaN(parseInt(t)))return parseInt(t);var t=(t=t.replace(/and/g,"")).toLowerCase().trim().split(" "),i=0;return(t=this.parseColloquialWords(t)).forEach(function(t){"number"==typeof(t=e.eliminateString(t))?i=t:l[t]?i+=h[t]:p[t]?i+=p[t]:f[t]&&(i*=f[t])}),i},L.prototype.parseColloquialWords=function(t){return 2==t.length&&p[t[0]]&&(p[t[1]]||u[t[1]])&&t.splice(1,0,"hundred"),f[t[0]]&&t.splice(0,0,"one"),t},L.prototype.eliminateString=function(t){return/\d/.test(t)&&d.includes(t.substr(t.length-2))&&(t=t.substr(0,t.length-2),t=parseInt(t)),t},L.decorators=[{type:o.Injectable,args:[{providedIn:"root"}]}],L.ngInjectableDef=o.defineInjectable({factory:function(){return new L},token:L,providedIn:"root"}),L);function L(){this.response=new e.Subject,this.inputRefs=[],this.currentRef=0}var Y=i,P={type:"text",format:"DD/MM/YYYY"},C=(A.prototype.ngOnInit=function(){this.options=a({},P,this.options),this.el.options=this.options},A.prototype.formatDate=function(t){return Y(t).format(this.options.format)},A.decorators=[{type:o.Directive,args:[{selector:"[vuiInput]"}]}],A.ctorParameters=function(){return[{type:E},{type:o.ElementRef}]},A.propDecorators={options:[{type:o.Input,args:["vuiInput"]}]},A);function A(t,e){var i=this;this.vuiService=t,this.el=e,this.el.nativeElement.setAttribute("vui-ref",this.vuiService.inputRefs.length),this.vuiService.inputRefs.push(this.el),this.el.nativeElement.addEventListener("focus",function(t){i.vuiService.currentRef=parseInt(t.target.getAttribute("vui-ref"))})}var z=function(t,e){this.type=t||"text",this.value=e},B=i,U={containerClass:"centered",iconStart:"icon icon-mic",iconParse:"icon icon-mic",iconStop:"icon icon-mic",animationParse:"parsing",animationListen:"listening"},V="vertical",i=(j.prototype.ngOnInit=function(){this.style=a({},U,this.style),this.scrollOffset=this.scrollOffset||200,this.scrollDuration=this.scrollDuration||300;try{this.initVoiceRecognition()}catch(t){console.error("Failed to initialize or parse"),this.stopRecognition()}this.vuiService.response.subscribe(this.vuiResponseSubscription.bind(this))},j.prototype.vuiResponseSubscription=function(t){this.transcript="";var e,i,n=this.vuiService.currentRef;"COMMAND_NEXT"==t.type?n=++this.vuiService.currentRef:"COMMAND_PREVIOUS"==t.type?n=--this.vuiService.currentRef:"COMMAND_FIRST"==t.type?n=0:"COMMAND_LAST"==t.type?n=this.vuiService.inputRefs.length-1:"COMMAND_CLEAR"==t.type?this.vuiService.inputRefs[n].nativeElement.value="":"COMMAND_CLICK"==t.type?this.vuiService.inputRefs[n].nativeElement.click():t.type.includes("COMMAND_SCROLL")?this.scroll[t.type.replace("COMMAND_","")].call():"COMMAND_STOP"==t.type?this.stopRecognition():(e=(i=this.vuiService.inputRefs[n]).nativeElement,i=i.options,t.value&&t.value[0]instanceof Date?"date"==e.type||"datetime"==e.type?e.value=this.formatDate(t.value[0],"YYYY-MM-DD"):e.value=this.formatDate(t.value[0],i&&i.format):e.value=t.value,e.dispatchEvent(new Event("input")),this.onValueChange.emit(t)),this.vuiService.inputRefs[n].nativeElement.focus(),this.setProcess("listening")},j.prototype.setProcess=function(t){this.process=t,this.ref.detectChanges()},j.prototype.formatDate=function(t,e){return B(t).format(e||"DD/MM/YYYY")},j.prototype.initVoiceRecognition=function(){var r=this;this.recognition=new window.webkitSpeechRecognition,this.recognition.continuous=!0,this.recognition.interimResults=!0,this.recognition.onstart=function(t){r.setProcess("listening")},this.recognition.onresult=function(t){var e=r.vuiService.inputRefs[r.vuiService.currentRef].options;r.setProcess("parsing");for(var i,n,o=t.resultIndex;o<t.results.length;++o)t.results[o].isFinal&&(r.transcript+=t.results[o][0].transcript,i=r.vuiService.interpretSpeech(r.transcript,e&&e.type),n=new z("invalid",i),i&&(n=new z("date-range",i),"string"==typeof i&&(n=new z(i,i.includes("_")?"":i))),r.vuiService.response.next(n))},this.recognition.onerror=function(){r.setProcess(null)},this.recognition.onend=function(){r.setProcess(null)}},j.prototype.startRecognition=function(){this.transcript="",this.recognition.start()},j.prototype.stopRecognition=function(){this.transcript="",this.recognition.stop(),this.setProcess(null)},j.decorators=[{type:o.Component,args:[{selector:"ng-voice-input",template:'\n <div [class]="style.containerClass">\n <i [class]="style.iconStart" mat-raised-button (click)="startRecognition()" *ngIf="!process"></i>\n <i [ngClass]="process == \'parsing\' ? style.iconParse + \' \' + style.animationParse : style.iconStop + \' \' + style.animationListen" (click)="stopRecognition()" *ngIf="process"></i>\n </div>\n ',encapsulation:o.ViewEncapsulation.None,styles:[".centered{margin:auto;text-align:center}.listening{color:red}.parsing{color:green}.icon{position:relative;width:45px;height:45px;display:inline-block;background-size:26px 26px;background-position:7px 7px;background-repeat:no-repeat;cursor:pointer}.icon.parsing::after{opacity:0;position:absolute;top:-5px;left:-5px;right:0;bottom:0;content:'';border:3px solid #42c0fb;border-radius:100%;-webkit-animation-name:ripple;animation-name:ripple;-webkit-animation-duration:1s;animation-duration:1s;-webkit-animation-delay:0s;animation-delay:0s;-webkit-animation-iteration-count:infinite;animation-iteration-count:infinite;-webkit-animation-timing-function:cubic-bezier(.65,0,.34,1);animation-timing-function:cubic-bezier(.65,0,.34,1);z-index:-1}.icon.parsing::before{opacity:0;position:absolute;top:-5px;left:-5px;right:0;bottom:0;content:'';border:3px solid #42c0fb;border-radius:100%;-webkit-animation-name:ripple;animation-name:ripple;-webkit-animation-duration:1s;animation-duration:1s;-webkit-animation-delay:.3s;animation-delay:.3s;-webkit-animation-iteration-count:infinite;animation-iteration-count:infinite;-webkit-animation-timing-function:cubic-bezier(.65,0,.34,1);animation-timing-function:cubic-bezier(.65,0,.34,1);z-index:-1}.icon.listening::after{opacity:0;position:absolute;top:-5px;left:-5px;right:0;bottom:0;content:'';border:3px solid #42c0fb44;border-radius:100%;-webkit-animation-name:ripple;animation-name:ripple;-webkit-animation-duration:2s;animation-duration:2s;-webkit-animation-delay:0s;animation-delay:0s;-webkit-animation-iteration-count:infinite;animation-iteration-count:infinite;-webkit-animation-timing-function:cubic-bezier(.65,0,.34,1);animation-timing-function:cubic-bezier(.65,0,.34,1);z-index:-1}.icon-small{width:25px;height:25px;background-size:15px 15px;background-position:5px 10px}.icon-mic{background-image:url(\"data:image/svg+xml;utf8,<svg version='1.0' xmlns='http://www.w3.org/2000/svg' width='853.000000pt' height='1280.000000pt' viewBox='0 0 853.000000 1280.000000' preserveAspectRatio='xMidYMid meet'> <metadata> Created by potrace 1.15, written by Peter Selinger 2001-2017 </metadata> <g transform='translate(0.000000,1280.000000) scale(0.100000,-0.100000)' fill='rgb(66,192,251)' > <path d='M4060 12789 c-194 -22 -402 -81 -577 -164 -472 -224 -789 -626 -894 -1135 l-24 -115 0 -2575 c0 -2452 1 -2579 18 -2665 145 -710 755 -1260 1492 -1345 327 -37 671 21 975 166 473 226 789 629 892 1137 l23 112 0 2575 c0 2448 -1 2579 -18 2665 -116 576 -556 1066 -1134 1264 -227 78 -508 108 -753 80z'/><path d='M301 8004 c-158 -42 -262 -156 -292 -319 -7 -38 -9 -312 -6 -839 3 -648 7 -805 21 -921 64 -538 177 -928 391 -1350 184 -361 380 -630 670 -920 529 -528 1216 -906 2020 -1110 116 -29 231 -56 258 -60 l47 -7 0 -854 0 -854 -657 0 c-717 0 -743 -2 -851 -57 -69 -35 -155 -127 -176 -189 -53 -155 36 -357 203 -463 103 -66 -69 -61 2341 -61 2377 0 2235 -3 2349 54 64 33 144 122 173 194 19 49 23 75 23 182 0 120 -1 128 -32 192 -36 76 -100 146 -165 179 -95 48 -100 49 -820 49 l-678 0 0 854 c0 805 1 855 18 860 9 2 33 7 52 11 406 77 934 271 1313 484 1088 609 1778 1567 1971 2736 48 288 51 352 51 1150 l0 760 -23 58 c-63 155 -190 243 -367 254 -147 10 -260 -32 -350 -129 -32 -34 -63 -80 -76 -115 l-24 -58 -6 -785 c-6 -737 -11 -850 -39 -1050 -100 -705 -417 -1330 -913 -1794 -538 -505 -1229 -810 -2052 -908 -146 -17 -674 -17 -820 0 -663 79 -1253 297 -1735 642 -683 489 -1111 1202 -1230 2050 -28 194 -33 327 -39 1060 l-6 785 -24 58 c-44 111 -161 208 -282 236 -62 15 -175 12 -238 -5z'/></g> </svg>\")}.icon-mic-blue{background-image:url(../../assets/icons/mic-blue-solid.png)}.icon-mic-red{background-image:url(../../assets/icons/mic-red-solid.png)}.icon-mic-green{background-image:url(../../assets/icons/mic-green-solid.png)}@-webkit-keyframes ripple{from{opacity:1;transform:scale3d(.75,.75,1)}to{opacity:0;transform:scale3d(1.5,1.5,1)}}@keyframes ripple{from{opacity:1;transform:scale3d(.75,.75,1)}to{opacity:0;transform:scale3d(1.5,1.5,1)}}"]}]}],j.ctorParameters=function(){return[{type:E},{type:o.ChangeDetectorRef}]},j.propDecorators={style:[{type:o.Input}],scrollOffset:[{type:o.Input}],scrollDuration:[{type:o.Input}],onValueChange:[{type:o.Output}]},j);function j(t,e){var s=this;this.vuiService=t,this.ref=e,this.onValueChange=new o.EventEmitter,this.activeDirection=1,this.scrollDirection=V,this.scrollToTop=function(e){var i=setInterval(function(){var t=Math.abs(Math.round(window.pageYOffset/(e/100)));0<t?window.scrollTo(0,t-20):clearInterval(i)},100)},this.scrollBy=function(t,e,i){s.activeDirection=t/t,s.scrollDirection=i||V,1==t&&(t=window.outerHeight);var n=Math.abs(Math.round(t/(e/100))),o=0,r=setInterval(function(){o>=Math.abs(t)?clearInterval(r):(window.scrollBy(a({},"horizontal"==i?{top:0,left:t<0?-1*n:n}:{top:t<0?-1*n:n,left:0},{behavior:"smooth"})),o+=n)},100)},this.scroll={SCROLLDOWN:function(){s.scrollBy(s.scrollOffset,s.scrollDuration)},SCROLLUP:function(){s.scrollBy(-1*s.scrollOffset,s.scrollDuration)},SCROLLRIGHT:function(){s.scrollBy(s.scrollOffset,s.scrollDuration,"horizontal")},SCROLLLEFT:function(){s.scrollBy(-1*s.scrollOffset,s.scrollDuration,"horizontal")},SCROLLBOTTOM:function(){s.scrollBy(1,s.scrollDuration)},SCROLLTOP:function(){s.scrollToTop(s.scrollDuration)},SCROLLCONTINUE:function(){var t=Math.ceil(window.outerHeight/s.scrollDuration);console.log(t*s.scrollDuration,t),s.scrollBy(window.outerHeight,1e3*t)}}}Date.prototype.isValid=function(){return this.getTime()===this.getTime()};_.decorators=[{type:o.NgModule,args:[{declarations:[i,C],imports:[n.CommonModule],exports:[i,C]}]}],n=_;function _(){}t.VuiInputDirective=C,t.NgVoiceInputsComponent=i,t.NgVoiceInputsModule=n,t.ɵa=E,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=ng-voice-inputs.umd.min.js.map