3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-28 06:49:25 +01:00
dbot/modules/spelling.js

66 lines
2.2 KiB
JavaScript
Raw Normal View History

2011-10-26 18:19:52 +02:00
var spelling = function(dbot) {
var dbot = dbot;
var last = {};
2012-01-01 21:35:43 +01:00
var correct = function (data, correction, candidate, output_callback) {
var candidates = last[data.channel][candidate].split(' ');
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, candidates[i]);
if(distance < winnerDistance) {
2012-01-09 15:53:48 +01:00
winner = i;
2012-01-01 21:35:43 +01:00
winnerDistance = distance;
}
}
2012-01-02 14:20:20 +01:00
if(winnerDistance < 7) {
2012-01-01 21:35:43 +01:00
if(winner !== correction) {
2012-01-09 15:53:48 +01:00
candidates[winner] = correction;
2012-01-09 15:51:34 +01:00
var fix = candidates.join(' ');
last[data.channel][candidate] = fix;
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) {
var q = data.message.valMatch(/^(?:\*\*?([\d\w\s']*)|([\d\w\s']*)\*\*?)$/, 2);
2012-01-20 17:26:47 +01:00
var otherQ = data.message.valMatch(/^([\d\w\s]*): \*\*?([\d\w\s']*)$/, 3);
2011-10-26 18:19:52 +02:00
if(q) {
2012-01-01 21:35:43 +01:00
correct(data, q[1], data.user, function (e) {
dbot.say(data.channel, e.correcter + ' meant: ' + e.fix);
});
2011-11-04 17:34:36 +01:00
} else if(otherQ) {
2012-01-01 21:35:43 +01:00
correct(data, otherQ[2], otherQ[1], function (e) {
dbot.say(data.channel, e.correcter + ' thinks ' + e.candidate + ' meant: ' + e.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);
};