forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot-listen.js
More file actions
125 lines (107 loc) · 3.84 KB
/
chatbot-listen.js
File metadata and controls
125 lines (107 loc) · 3.84 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var speak = require("speakeasy-nlp");
var levenshtein = require('fast-levenshtein');
var _ = require('underscore');
var regexps = require('./lib/helpers/regexps.js');
var debug = false;
module.exports = function(RED) {
var fixedWords = ['yes', 'no', 'on', 'off'];
var tokenVariables = ['{{email}}', '{{number}}', '{{url}}'];
function isFixedWord(word) {
return _.contains(fixedWords, word);
}
function isTokenVariable(word) {
return _.contains(tokenVariables, word);
}
/**
* @method getDistance
* Get the Levenshtein distance based on the length of the word, for length = 2 distance must be 0 or "off" and "on"
* will be confused
*/
function getDistance(word) {
if (word.length <= 2) {
return 0
} else if (word.length <= 4) {
return 1;
} else {
return 2;
}
}
/**
* @method matchSentence
* Given the analysis to a sentence, check the all the provided words match with the sentence
* @param {Object} sentence
* @param {Array} words
* @param {ChatContext} chatContext
*/
function matchSentence(sentence, words, chatContext) {
// convert all words lowercase
words = _(words).map(function(word) {
return word.toLowerCase();
});
debug && console.log('tokens - ', sentence.tokens);
debug && console.log('analysis - ', sentence);
// scan the words, all must be present
var result = _(words).all(function(word) {
if (isFixedWord(word)) {
debug && console.log('contain exact', _.contains(sentence.tokens, word));
return _.contains(sentence.tokens, word);
} else if (isTokenVariable(word)) {
// get the right matcher
var variable = word.replace('{{', '').replace('}}', '');
var test = regexps[variable];
var found = null;
// search if in the tokens something is matching
var matched = _(sentence.tokens).some(function(token) {
return (found = test(token)) != null;
});
// store in chat context if any
if (found != null) {
chatContext.set(variable, found);
}
return matched;
} else {
return _(sentence.tokens).some(function(token) {
debug && console.log('* Levenshtein ', token, word, levenshtein.get(token, word) <= getDistance(word));
// distance depends on length of the word to check
return levenshtein.get(token, word) <= getDistance(word);
});
}
});
debug && console.log('- MATCHED ', words, '---> ', result);
return result;
}
function ChatBotListen(config) {
RED.nodes.createNode(this, config);
var node = this;
this.sentences = config.sentences;
this.on('input', function(msg) {
var sentences = node.sentences;
var originalMessage = msg.originalMessage;
var chatId = msg.payload.chatId || (originalMessage && originalMessage.chat.id);
var context = node.context();
var chatContext = msg.chat();
// exit if not string
if (!_.isString(msg.payload.content)) {
return;
}
debug && console.log('Searching match for ', msg.payload.content);
// see if one of the rules matches
var matched = null;
// match
if (chatContext != null) {
matched = _(sentences).any(function (sentence) {
// check if valid json
var words = sentence.split(',');
debug && console.log('---- START analysis ' + msg.payload.content);
// analize sentence
var analysis = speak.classify(msg.payload.content);
// send message if the sentence is matched, otherwise stop here
return matchSentence(analysis, words, chatContext);
});
}
// pass through the message if any of the above matched
node.send(matched ? [msg, null] : [null, msg]);
});
}
RED.nodes.registerType('chatbot-listen', ChatBotListen);
};