bot-script
Version:
Scripting tool to Write Bot's Workflow
223 lines (216 loc) • 7.27 kB
JavaScript
/**
* Created by agnibha on 27/1/17.
*/
var request = require("request");
module.exports = NLPParser;
/**
* Calls Gupshup's NLP on the fly framework
* @constructor
*/
function NLPParser() {
this.parse = newParser;
this.entityParser = entityParser;
}
/**
*
* @param intents
* @param utterance
* @param onSuccess
* @param onError
*/
function oldParser(intents, utterance, onSuccess, onError) {
var nlpLocation = "http://intent.gupshup.io/intentOnTheFly?data=";
var userStr = utterance;
for (var array_index in intents) {
var intentObj = intents[array_index];
for (var var_index in intentObj.variations) {
userStr = userStr.concat(";", intentObj.variations[var_index].sample);
}
}
request.get(nlpLocation.concat(encodeURI(userStr)), function (error,
response,
body) {
if (error) {
if (typeof onError === "function") {
onError(error);
} else {
console.error(error);
}
}
if (body) {
try {
var nlpResponseArr = JSON.parse(body)[0];
var match = "";
for (var array_index in intents) {
var intentObj = intents[array_index];
for (var var_index in intentObj.variations) {
if (intentObj.variations.indexOf(nlpResponseArr[1]) > -1) {
match = intentObj.intent;
break;
}
}
}
var responseJSON = {
input: nlpResponseArr[0],
match: match,
score: nlpResponseArr[2],
match_score_array: nlpResponseArr[3]
};
onSuccess(responseJSON);
} catch (e) {
if (onError) {
onError(e);
} else {
console.error(e);
}
}
} else if (response) {
console.log("Received Response", response);
} else {
onError(new Error("Something went wrong. NLP Parser Not Working"));
}
});
}
/**
*
* @param intents
* @param utterance
* @param onSuccess
* @param onError
*/
function newParser(intents,
utterance,
onSuccess,
onError,
timeOutDuration,
options) {
var timeOutDu = timeOutDuration ? timeOutDuration : 10000;
// var nlpLocation = 'https://tuiwo5z8gd.execute-api.us-east-1.amazonaws.com/prod/nlponthefly-prod';
var nlpLocation =
(process.env && process.env.FEDERATED_NLP_URL) ||
"https://tuiwo5z8gd.execute-api.us-east-1.amazonaws.com/prod/v2/nlponthefly-prod";
var timeOut = setTimeout(function () {
onError(new Error("TimeOut"));
if (r) {
r.abort();
}
}, timeOutDu);
var requiredOptions = {
queries: [utterance],
intents: intents
};
if (options) {
if (options.method) {
requiredOptions[options.method] = 1;
}
if (options.topn) {
requiredOptions.topn = options.topn;
}
if (options.debug) {
requiredOptions.debug = options.debug;
}
}
var r = request.post(
nlpLocation, {
json: requiredOptions,
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache"
}
},
function (error, response, body) {
if (timeOut) {
clearTimeout(timeOut);
}
if (!error && response.statusCode == 200) {
var resp = body[0];
if (typeof resp !== "undefined") {
var nlpResponse = {
input: resp.query,
match: resp.intentWithMaxScore,
totalWeightTime: resp.totalWeightTime,
matchWords: resp.matchWords,
maxIntentScore: resp.maxIntentScore,
intentVariation: resp.intentVariation,
entities: resp.entities
};
console.log("NLPResponse => " + JSON.stringify(nlpResponse, null, 2));
if (!resp.error) {
var nlpResponse = {
input: resp.query,
match: resp.intentWithMaxScore,
totalWeightTime: resp.totalWeightTime,
matchWords: resp.matchWords,
maxIntentScore: resp.maxIntentScore,
intentVariation: resp.intentVariation,
entities: resp.entities
};
console.log("NLPResponse => " + JSON.stringify(nlpResponse, null, 2));
onSuccess(nlpResponse);
} else {
onError(resp.error);
}
} else {
console.error("NLPResponse => " + JSON.stringify(body, null, 2));
if (typeof body.errorType !== "undefined") {
onError(body.errorType);
}
}
} else {
if (!error) {
error = new Error("Response Status is ".concat(response.statusCode));
}
if (onError) {
onError(error);
} else {
console.error(error);
}
}
}
);
}
function entityParser(userQuery, sampleText, entities, onSuccess, onError) {
var samples = [];
var eofLocation =
"https://tuiwo5z8gd.execute-api.us-east-1.amazonaws.com/prod/nlponthefly-prod";
for (var idx in entities) {
var entity = entities[idx];
var temp = {};
temp.name = entity.entityName;
temp.sampleEntities = [];
temp.sampleEntities.push({
query: sampleText,
entity: entity.entitySample
});
temp.type = "";
samples.push(temp);
}
var requiredOptions = {
entity: 1,
text: userQuery,
samples: samples
};
request.post(
eofLocation, {
json: requiredOptions,
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache"
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
onSuccess(body);
} else {
if (!error) {
error = new Error("Response Status is ".concat(response.statusCode));
}
if (onError) {
onError(error);
} else {
console.error(error);
}
}
}
);
}