From dab8f2f47e671f7a5c98ea56aa086cd6e26cf457 Mon Sep 17 00:00:00 2001 From: Peery Date: Sun, 15 Oct 2017 17:05:14 +0200 Subject: [PATCH] Added fetchCommand utility Implemented fetchCommand method to map command input to available commands. Includes distance threshhold --- commands.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/commands.js b/commands.js index 0832314..e5148f6 100644 --- a/commands.js +++ b/commands.js @@ -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 = ''; +commands['homeserver']['register'].exec = (args) => { + //args is a .match with .regex +}; + +commands['imninja'] = {}; +commands['imninja']['register'] = {}; +commands['imninja']['register'].regex = /([a-zA-Z0-9])/; +commands['imninja']['register'].usage = ''; +commands['imninja']['register'].exec = (args) => { + //args is a .match with .regex +}; +console.log(fetchCommand("inminja", commands)); // -> imninja +console.log(fetchCommand("34C21", commands, 4)); // -> {}*/ +//console.log(getEditDistance("saturday", "sunday")); //should be 3