Created
March 26, 2017 12:34
-
-
Save ArjunHariharan/eabb5b2f816e841e63ba83f6324af4d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let request = require('request'); | |
module.exports = function(config) { | |
if (!config) { | |
config = {}; | |
} | |
if (!config.rasa_uri) { | |
config.rasa_uri = 'http://localhost:5000'; | |
} | |
let middleware = { | |
receive: function (bot, message, next) { | |
/*eslint-disable */ | |
let postData = { | |
'q': message.text.toLowerCase(), | |
}; | |
/*eslint-enable */ | |
let requestOptions = { | |
url: config.rasa_uri + '/parse', | |
body: postData, | |
json: true | |
}; | |
request.post(requestOptions, function (error, response, body) { | |
// TODO | |
// error handling | |
if (body) { | |
/* There will be only one intent for sentences of any length. | |
* Ex query - "Hi. i am arjun. i have 102 fever" | |
* | |
* Output - {"text": "hi. this is arjun. i have 102 fever", | |
* "confidence": 0.49330375056610876, "intent": "symptom_analyze", | |
* "entities": [{"start": 12, "end": 18, "value": "arjun.", | |
* "entity": "patient_name"}, {"start": 21, "end": 25, "value": | |
* "have", "entity": "symptom"}, {"start": 26, "end": 29, | |
* "value": "102", "entity": "fever_temp"}, {"start": 30, | |
* "end": 35, "value": "fever", "entity": "symptom"}]} | |
*/ | |
let intent = body.intent; | |
// Group all similar entities in an array. | |
let bodyEntities = body.entities; | |
let messageEntities = {}; | |
for (let i = 0; i < bodyEntities.length; i++) { | |
if (bodyEntities[i].entity in messageEntities && | |
messageEntities[bodyEntities[i].entity].indexOf( | |
bodyEntities[i].value) === -1) { | |
// Entity exists already. Push the new entity value into the same | |
// entity. | |
messageEntities[bodyEntities[i].entity.toLowerCase()].push( | |
bodyEntities[i].value); | |
} else { | |
// This is a new entity. Add it to message entities. | |
messageEntities[bodyEntities[i].entity.toLowerCase()] = | |
[bodyEntities[i].value]; | |
} | |
} | |
// intent.entities = messageEntities; | |
message.intent = intent; | |
message.entities = messageEntities; | |
} | |
next(); | |
}); | |
}, | |
hears: function (patterns, message) { | |
if (patterns.indexOf('*') >= 0) { | |
return true; | |
} | |
for (let t = 0; t < patterns.length; t++) { | |
if (message.intent.name === patterns[t]) { | |
return true; | |
} | |
} | |
} | |
}; | |
return middleware; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
same here, how and where to use it?