3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 04:02:39 +01:00

Check all groupings of words for corrections; makes multi-word corrections behave properly.

This commit is contained in:
Psychedelic Squid 2012-03-06 22:02:52 +00:00
parent ad6b354daf
commit 4e2d4017d0
2 changed files with 20 additions and 5 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) {
winner = i; winner = candidates[i];
winnerDistance = distance; winnerDistance = distance;
} }
} }
if(winnerDistance < 7) { if(winnerDistance < 7) {
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');
} }

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) {