-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
71 lines (57 loc) · 1.72 KB
/
bot.js
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
var net = require('net');
var natural = require('natural');
var sockets = [];
var reply = function(msg, socket) {
socket.write('>> ' + msg + '\n');
console.log(msg);
}
var process = function(msg) {
var memory = {
'hi': 'Hi There !',
'how are you': 'Great !, How are You doing ?',
'fine': 'Nice :) !',
'bad': 'Oh !, You can tell more if it is OK ..',
'no': 'That\'s OK ..',
};
var leastDistance = null;
var mostlyThis = null;
for(x in memory) {
var measuring = natural.LevenshteinDistance(msg, x, {
insertion_cost: 1,
deletion_cost: 1,
substitution_cost: 1
});
if(leastDistance == null || measuring < leastDistance)
{
leastDistance = measuring;
mostlyThis = x;
}
console.log("For: " + x + " = " + measuring);
}
if(mostlyThis != null)
return memory[mostlyThis];
else
return "Sorry, Couldn't understand You !";
}
var server = net.createServer(function (socket) {
socket.setEncoding('utf8');
socket.write('>> Welcome to the Telnet server!\n>> You can talk to me freely.\n>> Simply, You can say "Bye" at anytime to end this chat !\n\n');
sockets.push(socket);
socket.on('data', function(data) {
data = data.replace(/(\r\n|\n|\r)/gm,"").toLowerCase();
console.log(">> " + data);
switch(data) {
case 'bye':
socket.end(process(data) + "\n");
break;
default:
reply(process(data), socket);
}
})
socket.on('end', function() {
var i = sockets.indexOf(socket);
if (i != -1) {
sockets.splice(i, 1);
}
})
}).listen(8888);