dbot/modules/spelling.js

69 lines
2.4 KiB
JavaScript
Raw Normal View History

2011-10-26 18:19:52 +02:00
var spelling = function(dbot) {
var last = {};
2012-01-01 21:35:43 +01:00
2012-05-25 17:11:07 +02:00
var correct = function (event, correction, candidate, output_callback) {
var rawCandidates = last[event.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++) {
var distance = String.prototype.distance(correction.toLowerCase(), candidates[i].toLowerCase());
if((distance < winnerDistance) && (distance > 0)) {
winner = candidates[i];
2012-01-01 21:35:43 +01:00
winnerDistance = distance;
}
}
if(winnerDistance < Math.ceil(winner.length * 1.33)) {
2012-01-01 21:35:43 +01:00
if(winner !== correction) {
2012-05-25 17:11:07 +02:00
var fix = last[event.channel][candidate].replace(winner, correction);
2012-01-01 21:35:43 +01:00
if (/^.ACTION/.test(fix)) {
fix = fix.replace(/^.ACTION/, '/me');
}
2012-05-25 17:11:07 +02:00
last[event.channel][candidate] = fix;
2012-01-01 21:35:43 +01:00
var output = {
'fix': fix,
2012-05-25 17:11:07 +02:00
'correcter': event.user,
2012-01-01 21:35:43 +01:00
'candidate': candidate
};
output_callback(output);
}
}
}
2011-10-26 18:19:52 +02:00
return {
'name': 'spelling',
'ignorable': true,
2012-05-25 17:11:07 +02:00
'listener': function(event) {
var q = event.message.valMatch(/^(?:\*\*?([\d\w\s']*)|([\d\w\s']*)\*\*?)$/, 3);
var otherQ = event.message.valMatch(/^([\d\w\s]*): (?:\*\*?([\d\w\s']*)|([\d\w\s']*)\*\*?)$/, 4);
if(q) {
correct(event, q[1] || q[2], event.user, function (e) {
event.reply(dbot.t('spelling_self', e));
});
} else if(otherQ) {
correct(event, otherQ[2] || otherQ[3], otherQ[1], function (e) {
event.reply(dbot.t('spelling_other', e));
});
} else {
if(last.hasOwnProperty(event.channel)) {
last[event.channel][event.user] = event.message;
2011-10-26 18:19:52 +02:00
} else {
last[event.channel] = { };
last[event.channel][event.user] = event.message;
2011-10-26 18:19:52 +02:00
}
}
},
'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);
};