2011-10-26 18:19:52 +02:00
|
|
|
var spelling = function(dbot) {
|
2012-04-15 23:04:58 +02:00
|
|
|
var name = 'spelling';
|
2011-10-26 18:19:52 +02:00
|
|
|
var dbot = dbot;
|
|
|
|
var last = {};
|
2012-01-01 21:35:43 +01:00
|
|
|
|
|
|
|
var correct = function (data, correction, candidate, output_callback) {
|
2012-03-06 23:02:52 +01:00
|
|
|
var rawCandidates = last[data.channel][candidate].split(' ').allGroupings();
|
|
|
|
var candidates = [];
|
|
|
|
for(var i=0;i<rawCandidates.length;i++) {
|
|
|
|
candidates.push(rawCandidates[i].join(' '));
|
|
|
|
}
|
2012-01-01 21:35:43 +01:00
|
|
|
var winner = false;
|
2012-01-02 14:13:31 +01:00
|
|
|
var winnerDistance = Infinity;
|
2012-01-01 21:35:43 +01:00
|
|
|
|
|
|
|
for(var i=0;i<candidates.length;i++) {
|
2012-03-20 19:53:54 +01:00
|
|
|
var distance = String.prototype.distance(correction.toLowerCase(), candidates[i].toLowerCase());
|
2012-03-06 23:19:11 +01:00
|
|
|
if((distance < winnerDistance) && (distance > 0)) {
|
2012-03-06 23:02:52 +01:00
|
|
|
winner = candidates[i];
|
2012-01-01 21:35:43 +01:00
|
|
|
winnerDistance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 00:04:37 +01:00
|
|
|
if(winnerDistance < Math.ceil(winner.length * 1.33)) {
|
2012-01-01 21:35:43 +01:00
|
|
|
if(winner !== correction) {
|
2012-03-06 23:02:52 +01:00
|
|
|
var fix = last[data.channel][candidate].replace(winner, correction);
|
2012-01-01 21:35:43 +01:00
|
|
|
if (/^.ACTION/.test(fix)) {
|
|
|
|
fix = fix.replace(/^.ACTION/, '/me');
|
|
|
|
}
|
|
|
|
last[data.channel][candidate] = fix;
|
|
|
|
var output = {
|
|
|
|
'fix': fix,
|
|
|
|
'correcter': data.user,
|
|
|
|
'candidate': candidate
|
|
|
|
};
|
|
|
|
output_callback(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 18:19:52 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
'listener': function(data, params) {
|
2012-04-15 23:04:58 +02:00
|
|
|
if((dbot.db.ignores.hasOwnProperty(data.user) &&
|
|
|
|
dbot.db.ignores[data.user].include(name)) == false) {
|
|
|
|
var q = data.message.valMatch(/^(?:\*\*?([\d\w\s']*)|([\d\w\s']*)\*\*?)$/, 3);
|
|
|
|
var otherQ = data.message.valMatch(/^([\d\w\s]*): (?:\*\*?([\d\w\s']*)|([\d\w\s']*)\*\*?)$/, 4);
|
|
|
|
if(q) {
|
|
|
|
correct(data, q[1] || q[2], data.user, function (e) {
|
|
|
|
dbot.say(data.channel, dbot.strings[dbot.language].spelling_self.format(e));
|
|
|
|
});
|
|
|
|
} else if(otherQ) {
|
|
|
|
correct(data, otherQ[2] || otherQ[3], otherQ[1], function (e) {
|
|
|
|
dbot.say(data.channel, dbot.strings[dbot.language].spelling_other.format(e));
|
|
|
|
});
|
2011-10-26 18:19:52 +02:00
|
|
|
} else {
|
2012-04-15 23:04:58 +02:00
|
|
|
if(last.hasOwnProperty(data.channel)) {
|
|
|
|
last[data.channel][data.user] = data.message;
|
|
|
|
} else {
|
|
|
|
last[data.channel] = { };
|
|
|
|
last[data.channel][data.user] = data.message;
|
|
|
|
}
|
2011-10-26 18:19:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-04-15 22:43:02 +02:00
|
|
|
'on': 'PRIVMSG',
|
|
|
|
|
2012-04-15 23:04:58 +02:00
|
|
|
'name': name,
|
2012-04-15 22:43:02 +02:00
|
|
|
|
|
|
|
'ignorable': true
|
2011-10-26 18:22:19 +02:00
|
|
|
}
|
2011-10-26 18:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return spelling(dbot);
|
|
|
|
};
|