dbot/modules/spelling.js

45 lines
1.4 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) {
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:25:41 +02:00
if(distance < winnerDistance) {
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:25:41 +02:00
2011-10-26 18:24:19 +02:00
console.log(winner + ' ' + winnerDistance);
2011-10-26 18:25:41 +02:00
2011-10-26 18:24:19 +02:00
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);
};