Added fetchCommand utility

Implemented fetchCommand method to map command input to
available commands. Includes distance threshhold
develop
Peery 7 years ago
parent 8a38753413
commit dab8f2f47e

@ -31,4 +31,43 @@ function getEditDistance(sourceString, targetString) {
return matrix[targetString.length][sourceString.length];
}
//console.log(getEditDistance("saturday", "sunday")); //sollte 3 ergeben
function fetchCommand(input, commands, threshhold){
let candidates = {};
let bestDistance = null;
for(let key in commands){
let distance = getEditDistance(input, key);
console.log("new it. key: "+key+" dist:"+distance);
if(distance >= threshhold) continue;
if(bestDistance === distance){ //another candidate
candidates[candidates.length] = key;
console.log("other candidate");
}
else if(bestDistance > distance || bestDistance === null){ //new best candidate
candidates = [key];
bestDistance = distance;
console.log("new best candidate");
}
}
return candidates;
}
/* //Test code
const commands = {};
commands['homeserver'] = {};
commands['homeserver']['register'] = {};
commands['homeserver']['register'].regex = /([a-zA-Z0-9])/;
commands['homeserver']['register'].usage = '<user localpart>';
commands['homeserver']['register'].exec = (args) => {
//args is a <user input args string>.match with .regex
};
commands['imninja'] = {};
commands['imninja']['register'] = {};
commands['imninja']['register'].regex = /([a-zA-Z0-9])/;
commands['imninja']['register'].usage = '<user localpart>';
commands['imninja']['register'].exec = (args) => {
//args is a <user input args string>.match with .regex
};
console.log(fetchCommand("inminja", commands)); // -> imninja
console.log(fetchCommand("34C21", commands, 4)); // -> {}*/
//console.log(getEditDistance("saturday", "sunday")); //should be 3