-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
33 lines (27 loc) · 856 Bytes
/
handler.js
File metadata and controls
33 lines (27 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const natural = require('natural');
const tokenizer = new natural.WordTokenizer();
const actionMap = {
greet: {
context: ['HI', 'hello'],
returns: function(tokenizedText) {
return 'Hello! I\'m Jarvis, what can I do for you?'
}
}
};
function resolve(tokenizedText=[]) {
const actions = Object.keys(actionMap);
const returns = [];
actions.forEach((key) => {
const action = actionMap[key];
if (natural.JaroWinklerDistance(action.context.join(''), tokenizedText.join('')) > 0.5 ) {
returns.push(action.returns(tokenizedText));
}
});
return returns.join('\r\n');
}
const handler = (session) => {
const tokenizedText = tokenizer.tokenize(session.message.text);
const response = resolve(tokenizedText) || 'Sorry!.. I couldn\'t understand you. :(';
session.send(response);
};
module.exports = handler;