mirror of
https://github.com/reality/dbot.git
synced 2024-11-27 14:29:29 +01:00
some command correction shiz
This commit is contained in:
parent
2ed6488d77
commit
0a212bab1d
@ -13,7 +13,27 @@ var command = function(dbot) {
|
|||||||
this.listener = function(event) {
|
this.listener = function(event) {
|
||||||
var commandName = event.params[0];
|
var commandName = event.params[0];
|
||||||
if(!_.has(dbot.commands, commandName)) {
|
if(!_.has(dbot.commands, commandName)) {
|
||||||
if(_.has(dbot.modules, 'quotes')) {
|
if(_.has(dbot.modules, 'spelling')) {
|
||||||
|
var commands = _.keys(dbot.commands)
|
||||||
|
winner = false,
|
||||||
|
closestMatch = Infinity;
|
||||||
|
|
||||||
|
_.each(commands, function(command) {
|
||||||
|
var distance = dbot.api.spelling.distance(commandName, command);
|
||||||
|
if(distance < closestMatch) {
|
||||||
|
closestMatch = distance;
|
||||||
|
winner = command;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if(closestMatch < 3) {
|
||||||
|
event.reply(commandName + ' not found. Did you mean ' + winner + '?');
|
||||||
|
} else if(_.has(dbot.modules, 'quotes')) {
|
||||||
|
commandName = '~';
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if(_.has(dbot.modules, 'quotes')) {
|
||||||
commandName = '~';
|
commandName = '~';
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
var _ = require('underscore')._;
|
var _ = require('underscore')._;
|
||||||
|
|
||||||
var allGroupings = function(arr) {
|
var spelling = function(dbot) {
|
||||||
|
this.last = {};
|
||||||
|
|
||||||
|
this.api = {
|
||||||
|
'allGroupings': function(arr) {
|
||||||
if (arr.length == 0) {
|
if (arr.length == 0) {
|
||||||
return []; /* short-circuit the empty-array case */
|
return []; /* short-circuit the empty-array case */
|
||||||
}
|
}
|
||||||
@ -11,8 +15,9 @@ var allGroupings = function(arr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return groupings;
|
return groupings;
|
||||||
}
|
},
|
||||||
var distance = function(s1, s2) {
|
|
||||||
|
'distance': function(s1, s2) {
|
||||||
// Calculate Levenshtein distance between two strings
|
// Calculate Levenshtein distance between two strings
|
||||||
//
|
//
|
||||||
// version: 1109.2015
|
// version: 1109.2015
|
||||||
@ -71,13 +76,12 @@ var distance = function(s1, s2) {
|
|||||||
v0 = v1;
|
v0 = v1;
|
||||||
v1 = v_tmp; }
|
v1 = v_tmp; }
|
||||||
return v0[s1_len];
|
return v0[s1_len];
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var spelling = function(dbot) {
|
|
||||||
this.last = {};
|
|
||||||
this.internalAPI = {};
|
this.internalAPI = {};
|
||||||
this.internalAPI.correct = function (event, correction, candidate, output_callback) {
|
this.internalAPI.correct = function (event, correction, candidate, output_callback) {
|
||||||
var rawCandidates = allGroupings(this.last[event.channel.name][candidate].split(' '));
|
var rawCandidates = this.api.allGroupings(this.last[event.channel.name][candidate].split(' '));
|
||||||
|
|
||||||
var candidates = [];
|
var candidates = [];
|
||||||
for(var i=0;i<rawCandidates.length;i++) {
|
for(var i=0;i<rawCandidates.length;i++) {
|
||||||
@ -87,7 +91,7 @@ var spelling = function(dbot) {
|
|||||||
var winnerDistance = Infinity;
|
var winnerDistance = Infinity;
|
||||||
|
|
||||||
for(var i=0;i<candidates.length;i++) {
|
for(var i=0;i<candidates.length;i++) {
|
||||||
var d = distance(correction.toLowerCase(), candidates[i].toLowerCase());
|
var d = this.api.distance(correction.toLowerCase(), candidates[i].toLowerCase());
|
||||||
if((d < winnerDistance) && (d > 0)) {
|
if((d < winnerDistance) && (d > 0)) {
|
||||||
winner = candidates[i];
|
winner = candidates[i];
|
||||||
winnerDistance = d;
|
winnerDistance = d;
|
||||||
|
Loading…
Reference in New Issue
Block a user