diff --git a/kiss/src/kiss/FuzzyMap.hx b/kiss/src/kiss/FuzzyMap.hx index 5119d30f..c6952ed4 100644 --- a/kiss/src/kiss/FuzzyMap.hx +++ b/kiss/src/kiss/FuzzyMap.hx @@ -1,6 +1,7 @@ package kiss; import haxe.ds.StringMap; +import kiss.FuzzyMapTools; using hx.strings.Strings; @@ -20,37 +21,8 @@ abstract FuzzyMap(StringMap) from StringMap to StringMap { return this; } - static var threshold = 0.4; - public static function fuzzyMatchScore(key:String, fuzzySearchKey:String) { - return 1 - (key.toLowerCase().getLevenshteinDistance(fuzzySearchKey.toLowerCase()) / Math.max(key.length, fuzzySearchKey.length)); - } - function bestMatch(fuzzySearchKey:String, ?throwIfNone=true):String { - if (this.exists(fuzzySearchKey)) return fuzzySearchKey; - - var bestScore = 0.0; - var bestKey = null; - - for (key in this.keys()) { - var score = fuzzyMatchScore(key, fuzzySearchKey); - if (score > bestScore) { - bestScore = score; - bestKey = key; - } - } - - if (bestScore < threshold) { - if (throwIfNone) - throw 'No good match for $fuzzySearchKey in $this -- best was $bestKey with $bestScore'; - else - return null; - } - - #if (test || debug) - trace('Fuzzy match $bestKey for $fuzzySearchKey score: $bestScore'); - #end - - return bestKey; + return FuzzyMapTools.bestMatch(this, fuzzySearchKey, throwIfNone); } @:arrayAccess diff --git a/kiss/src/kiss/FuzzyMapTools.hx b/kiss/src/kiss/FuzzyMapTools.hx index bba9438b..1c833810 100644 --- a/kiss/src/kiss/FuzzyMapTools.hx +++ b/kiss/src/kiss/FuzzyMapTools.hx @@ -2,23 +2,58 @@ package kiss; import haxe.Json; import haxe.ds.StringMap; +using hx.strings.Strings; typedef MapInfo = { file:String, matches:Map }; -/** - * FuzzyMap is highly inefficient, so you may wish to memoize the matches that it makes before - * releasing your project. FuzzyMapTools helps with this - */ class FuzzyMapTools { static var serializingMaps = new Map, MapInfo>(); + /** + * FuzzyMap is highly inefficient, so you may wish to memoize the matches that it makes before + * releasing your project. FuzzyMapTools.serializeMatches() helps with this + */ public static function serializeMatches(m:StringMap, file:String) { serializingMaps[m] = { file: file, matches: new Map() }; } + public static function fuzzyMatchScore(key:String, fuzzySearchKey:String) { + return 1 - (key.toLowerCase().getLevenshteinDistance(fuzzySearchKey.toLowerCase()) / Math.max(key.length, fuzzySearchKey.length)); + } + + static var threshold = 0.4; + + public static function bestMatch(map:FuzzyMap, fuzzySearchKey:String, ?throwIfNone=true):String { + if (map.existsExactly(fuzzySearchKey)) return fuzzySearchKey; + + var bestScore = 0.0; + var bestKey = null; + + for (key in map.keys()) { + var score = fuzzyMatchScore(key, fuzzySearchKey); + if (score > bestScore) { + bestScore = score; + bestKey = key; + } + } + + if (bestScore < threshold) { + if (throwIfNone) + throw 'No good match for $fuzzySearchKey in $map -- best was $bestKey with $bestScore'; + else + return null; + } + + #if (test || debug) + trace('Fuzzy match $bestKey for $fuzzySearchKey score: $bestScore'); + #end + + return bestKey; + } + @:allow(kiss.FuzzyMap) static function onMatchMade(m:StringMap, key:String, value:Dynamic) { #if (sys || hxnodejs) diff --git a/kiss/src/kiss/KissInterp.hx b/kiss/src/kiss/KissInterp.hx index f3505d60..7c9ae596 100644 --- a/kiss/src/kiss/KissInterp.hx +++ b/kiss/src/kiss/KissInterp.hx @@ -36,6 +36,7 @@ class KissInterp extends Interp { variables.set("Math", Math); variables.set("Json", haxe.Json); variables.set("StringMap", InterpMap); + variables.set("FuzzyMapTools", FuzzyMapTools); variables.set("StringTools", StringTools); variables.set("Path", haxe.io.Path); #if (sys || hxnodejs)