dbot/modules/spelling.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2011-10-26 17:19:52 +01: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 17:24:19 +01:00
var winnerDistance = 99999999; //urgh fix later
2011-10-26 17:19:52 +01:00
2011-10-26 17:21:21 +01:00
for(var i=0;i<candidates.length;i++) {
2011-10-26 17:19:52 +01:00
var distance = String.prototype.distance(correction, candidates[i]);
2011-10-26 17:25:41 +01:00
if(distance < winnerDistance) {
2011-10-26 17:19:52 +01:00
winner = candidates[i];
2011-10-26 17:24:19 +01:00
winnerDistance = distance;
2011-10-26 17:19:52 +01:00
}
}
2011-10-26 17:25:41 +01:00
2011-10-26 17:24:19 +01:00
console.log(winner + ' ' + winnerDistance);
2011-10-26 17:25:41 +01:00
2011-10-26 17:24:19 +01:00
if(winnerDistance < 3) {
2011-10-26 17:26:37 +01:00
var fix = last[data.channel][data.user].replace(winner, correction);
2011-10-26 17:27:05 +01:00
dbot.say(data.channel, data.user + ': ' + fix);
2011-10-26 17:19:52 +01:00
}
} 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 17:22:19 +01:00
}
2011-10-26 17:19:52 +01:00
}
exports.fetch = function(dbot) {
return spelling(dbot);
};