dbot/modules/spelling.js

70 lines
2.7 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);
var otherQ = data.message.valMatch(/^([\d\w\s]*): \*\*?([\d\w\s]*)$/, 3);
2011-10-26 18:19:52 +02:00
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-12-14 21:57:15 +01:00
if(winnerDistance < 7) {
2011-10-26 21:52:53 +02:00
if(winner !== correction) {
var fix = last[data.channel][data.user].replace(winner, correction);
2011-11-01 18:48:13 +01:00
last[data.channel][data.user] = fix;
2011-10-26 21:56:52 +02:00
dbot.say(data.channel, data.user + ' meant: ' + fix);
2011-10-26 21:52:53 +02:00
}
2011-10-26 18:19:52 +02:00
}
2011-11-04 17:34:36 +01:00
} else if(otherQ) {
if(last[data.channel].hasOwnProperty([otherQ[1]])) {
var correction = otherQ[2];
var candidates = last[data.channel][otherQ[1]].split(' ');
var winner = false;
var winnerDistance = 99999999; //urgh fix later
for(var i=0;i<candidates.length;i++) {
var distance = String.prototype.distance(correction, candidates[i]);
if(distance < winnerDistance) {
winner = candidates[i];
winnerDistance = distance;
}
}
if(winnerDistance < 3) {
if(winner !== correction) {
var fix = last[data.channel][otherQ[1]].replace(winner, correction);
last[data.channel][otherQ[1]] = fix;
dbot.say(data.channel, data.user + ' thinks ' + otherQ[1] + ' meant: ' + fix);
}
}
}
2011-10-26 18:19:52 +02: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 18:22:19 +02:00
}
2011-10-26 18:19:52 +02:00
}
exports.fetch = function(dbot) {
return spelling(dbot);
};