mirror of
https://github.com/reality/dbot.git
synced 2024-12-25 04:02:39 +01:00
cleaned up the spelling module
This commit is contained in:
parent
32e21067f5
commit
444817f145
@ -1,55 +1,49 @@
|
|||||||
var spelling = function(dbot) {
|
var spelling = function(dbot) {
|
||||||
var dbot = dbot;
|
var dbot = dbot;
|
||||||
var last = {};
|
var last = {};
|
||||||
|
|
||||||
|
var correct = function (data, correction, candidate, output_callback) {
|
||||||
|
var candidates = last[data.channel][candidate].split(' ');
|
||||||
|
var winner = false;
|
||||||
|
var winnerDistance = Infinity; //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][candidate].replace(winner, correction);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'listener': function(data, params) {
|
'listener': function(data, params) {
|
||||||
var q = data.message.valMatch(/^\*\*?([\d\w\s]*)/, 2);
|
var q = data.message.valMatch(/^\*\*?([\d\w\s]*)/, 2);
|
||||||
var otherQ = data.message.valMatch(/^([\d\w\s]*): \*\*?([\d\w\s]*)/, 3);
|
var otherQ = data.message.valMatch(/^([\d\w\s]*): \*\*?([\d\w\s]*)/, 3);
|
||||||
if(q) {
|
if(q) {
|
||||||
var correction = q[1];
|
correct(data, q[1], data.user, function (e) {
|
||||||
var candidates = last[data.channel][data.user].split(' ');
|
dbot.say(data.channel, e.correcter + ' meant: ' + e.fix);
|
||||||
var winner = false;
|
});
|
||||||
var winnerDistance = Infinity; //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][data.user].replace(winner, correction);
|
|
||||||
last[data.channel][data.user] = fix;
|
|
||||||
dbot.say(data.channel, data.user + ' meant: ' + fix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if(otherQ) {
|
} else if(otherQ) {
|
||||||
if(last[data.channel].hasOwnProperty([otherQ[1]])) {
|
correct(data, otherQ[2], otherQ[1], function (e) {
|
||||||
var correction = otherQ[2];
|
dbot.say(data.channel, e.correcter + ' thinks ' + e.candidate + ' meant: ' + e.fix);
|
||||||
var candidates = last[data.channel][otherQ[1]].split(' ');
|
});
|
||||||
var winner = false;
|
|
||||||
var winnerDistance = Infinity; //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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if(last.hasOwnProperty(data.channel)) {
|
if(last.hasOwnProperty(data.channel)) {
|
||||||
last[data.channel][data.user] = data.message;
|
last[data.channel][data.user] = data.message;
|
||||||
|
Loading…
Reference in New Issue
Block a user