Merge pull request #19 from psquid/a83b1c2

Asterisks at either end, and multi-word search [spelling module fixes].
This commit is contained in:
Luke Slater 2012-03-07 07:12:02 -08:00
commit 7ac46f6ed5
2 changed files with 26 additions and 11 deletions

View File

@ -3,23 +3,25 @@ var spelling = function(dbot) {
var last = {}; var last = {};
var correct = function (data, correction, candidate, output_callback) { var correct = function (data, correction, candidate, output_callback) {
var candidates = last[data.channel][candidate].split(' '); var rawCandidates = last[data.channel][candidate].split(' ').allGroupings();
var candidates = [];
for(var i=0;i<rawCandidates.length;i++) {
candidates.push(rawCandidates[i].join(' '));
}
var winner = false; var winner = false;
var winnerDistance = Infinity; var winnerDistance = Infinity;
for(var i=0;i<candidates.length;i++) { for(var i=0;i<candidates.length;i++) {
var distance = String.prototype.distance(correction, candidates[i]); var distance = String.prototype.distance(correction, candidates[i]);
if(distance < winnerDistance) { if((distance < winnerDistance) && (distance > 0)) {
winner = i; winner = candidates[i];
winnerDistance = distance; winnerDistance = distance;
} }
} }
if(winnerDistance < 7) { if(winnerDistance < Math.ceil(winner.length * 1.33)) {
if(winner !== correction) { if(winner !== correction) {
candidates[winner] = correction; var fix = last[data.channel][candidate].replace(winner, correction);
var fix = candidates.join(' ');
last[data.channel][candidate] = fix;
if (/^.ACTION/.test(fix)) { if (/^.ACTION/.test(fix)) {
fix = fix.replace(/^.ACTION/, '/me'); fix = fix.replace(/^.ACTION/, '/me');
} }
@ -36,14 +38,14 @@ var spelling = function(dbot) {
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']*)|([\d\w\s']*)\*\*?)$/, 3);
var otherQ = 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) { if(q) {
correct(data, q[1], data.user, function (e) { correct(data, q[1] || q[2], data.user, function (e) {
dbot.say(data.channel, e.correcter + ' meant: ' + e.fix); dbot.say(data.channel, e.correcter + ' meant: ' + e.fix);
}); });
} else if(otherQ) { } else if(otherQ) {
correct(data, otherQ[2], otherQ[1], function (e) { correct(data, otherQ[2] || otherQ[3], otherQ[1], function (e) {
dbot.say(data.channel, e.correcter + ' thinks ' + e.candidate + ' meant: ' + e.fix); dbot.say(data.channel, e.correcter + ' thinks ' + e.candidate + ' meant: ' + e.fix);
}); });
} else { } else {

View File

@ -27,6 +27,19 @@ Array.prototype.include = function(value) {
return false; return false;
}; };
Array.prototype.allGroupings = function() {
if (this.length == 0) {
return []; /* short-circuit the empty-array case */
}
var groupings = [];
for(var n=1;n<=this.length;n++) {
for(var i=0;i<(this.length-(n-1));i++) {
groupings.push(this.slice(i, i+n));
}
}
return groupings;
}
/*** String ***/ /*** String ***/
String.prototype.valMatch = function(regex, expLength) { String.prototype.valMatch = function(regex, expLength) {