diff --git a/modules/spelling.js b/modules/spelling.js index 37b906d..5ce2605 100644 --- a/modules/spelling.js +++ b/modules/spelling.js @@ -3,23 +3,25 @@ var spelling = function(dbot) { var last = {}; 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 0)) { + winner = candidates[i]; winnerDistance = distance; } } - if(winnerDistance < 7) { + if(winnerDistance < Math.ceil(winner.length * 1.33)) { if(winner !== correction) { - candidates[winner] = correction; - var fix = candidates.join(' '); - last[data.channel][candidate] = fix; + var fix = last[data.channel][candidate].replace(winner, correction); if (/^.ACTION/.test(fix)) { fix = fix.replace(/^.ACTION/, '/me'); } @@ -36,14 +38,14 @@ var spelling = function(dbot) { return { 'listener': function(data, params) { - var q = data.message.valMatch(/^\*\*?([\d\w\s']*)$/, 2); - var otherQ = data.message.valMatch(/^([\d\w\s]*): \*\*?([\d\w\s']*)$/, 3); + 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], data.user, function (e) { + correct(data, q[1] || q[2], data.user, function (e) { dbot.say(data.channel, e.correcter + ' meant: ' + e.fix); }); } 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); }); } else { diff --git a/snippets.js b/snippets.js index 54c235b..b769658 100644 --- a/snippets.js +++ b/snippets.js @@ -27,6 +27,19 @@ Array.prototype.include = function(value) { 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.prototype.valMatch = function(regex, expLength) {