1 line
2.2 KiB
JavaScript
1 line
2.2 KiB
JavaScript
"use strict";var e=function(e){return e&&e.__esModule?e["default"]:e},t=function(){function e(e,t){for(var r in t){var n=t[r];n.configurable=!0,n.value&&(n.writable=!0)}Object.defineProperties(e,t)}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),r=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},n=e(require("http")),s=void 0;try{s=require("jibo-parser")}catch(i){console.warn("Error importing jibo-parser. Simulator will not support robust parsing.")}var a=0,o=function(){function e(){r(this,e),this.port=11231,this.handles={}}return t(e,{init:{value:function(e){var t=this;this.server=n.createServer(),this.server.on("request",function(e,r){"/nlu_interface"===e.url&&"POST"===e.method&&(e.setEncoding("utf8"),e.on("data",function(e){var n=void 0;try{n=JSON.parse(e)}catch(s){return r.writeHead(200,{"Content-Type":"text/html"}),r.write(JSON.stringify({Status:"ERROR",Message:"Inavlid request. Request body is not valid JSON"})),void r.end()}"COMPILE"===n.REQ_TYPE?t.compile(n.REQ_CONTENT.RULE_STRING,r):"PARSE_FROM_URI"===n.REQ_TYPE?t.parseFromUri(n.REQ_CONTENT.URI,n.REQ_CONTENT.TXT_STRING,r):"PARSE_FROM_TEXT"===n.REQ_TYPE&&t.parseFromText(n.REQ_CONTENT.RULE_STRING,n.REQ_CONTENT.TXT_STRING,r)}))}),this.server.on("listening",function(){e()}),this.server.listen(this.port)}},parseFromUri:{value:function(e,t,r){var n={Status:"OK",URI:""},i=this.handles[e];if(i&&s){var a=s.build_sentence_parser(i),o=a.parse_sentence(t);n.Result=JSON.parse(o)}else n.Status="ERROR",n.Message="No handle named '"+e+"' exists";r.writeHead(200,{"Content-Type":"text/html"}),r.write(JSON.stringify(n)),r.end()}},parseFromText:{value:function(e,t,r){var n={Status:"OK"};try{var i=s.compile_fst_from_text(e,"handle:"+a++),o=s.build_sentence_parser(i),u=o.parse_sentence(t);n.Result=JSON.parse(u)}catch(l){n.Status="ERROR",n.Message=l.message}r.writeHead(200,{"Content-Type":"text/html"}),r.write(JSON.stringify(n)),r.end()}},compile:{value:function(e,t){var r={Status:"OK",URI:""};try{var n="handle:"+a++;this.handles[n]=s.compile_fst_from_text(e,n),r.URI=n}catch(i){r.Status="ERROR",r.Message=i.message}t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify(r)),t.end()}}}),e}();module.exports=new o; |