3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-28 06:49:25 +01:00
dbot/modules/spelling.js

44 lines
1.5 KiB
JavaScript
Raw Normal View History

2011-10-26 18:19:52 +02:00
var spelling = function(dbot) {
var dbot = dbot;
var last = {};
return {
'listener': function(data, params) {
var q = data.message.valMatch(/^\*([\d\w\s]*)/, 2);
if(q) {
2011-10-26 18:24:19 +02:00
dbot.say(data.channel, 'got q');
2011-10-26 18:19:52 +02:00
var correction = q[1];
var candidates = last[data.channel][data.user].split(' ');
var winner = false;
2011-10-26 18:24:19 +02:00
var winnerDistance = 99999999; //urgh fix later
2011-10-26 18:19:52 +02:00
2011-10-26 18:21:21 +02:00
for(var i=0;i<candidates.length;i++) {
2011-10-26 18:19:52 +02:00
var distance = String.prototype.distance(correction, candidates[i]);
2011-10-26 18:24:19 +02:00
if(distance < winner) {
2011-10-26 18:19:52 +02:00
winner = candidates[i];
2011-10-26 18:24:19 +02:00
winnerDistance = distance;
2011-10-26 18:19:52 +02:00
}
}
2011-10-26 18:24:19 +02:00
console.log(winner + ' ' + winnerDistance);
if(winnerDistance < 3) {
2011-10-26 18:19:52 +02:00
var fix = data.message.replace(winner, correction);
dbot.say(data.channel, data.user + ':' + fix);
}
} else {
if(last.hasOwnProperty(data.channel)) {
last[data.channel][data.user] = data.message;
} else {
last[data.channel] = { };
last[data.channel][data.user] = data.message;
}
}
},
'on': 'PRIVMSG'
2011-10-26 18:22:19 +02:00
}
2011-10-26 18:19:52 +02:00
}
exports.fetch = function(dbot) {
return spelling(dbot);
};