aboutsummaryrefslogtreecommitdiff
path: root/fuzzel.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2016-06-07 12:39:08 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2016-06-07 12:39:08 -0400
commit0e5b26e378aee8e99c00468c57f7479d3fa1653d (patch)
treefd32bd563e7386b8a349e0af7dbeb4b488880989 /fuzzel.lua
parent79d80ecf0be3e19d94a539f8b8488c2700ef22f0 (diff)
downloadfuzzel-0e5b26e378aee8e99c00468c57f7479d3fa1653d.tar.gz
fuzzel-0e5b26e378aee8e99c00468c57f7479d3fa1653d.tar.bz2
fuzzel-0e5b26e378aee8e99c00468c57f7479d3fa1653d.zip
Added autocomplete functions
Diffstat (limited to 'fuzzel.lua')
-rw-r--r--fuzzel.lua39
1 files changed, 29 insertions, 10 deletions
diff --git a/fuzzel.lua b/fuzzel.lua
index e647711..bd9308a 100644
--- a/fuzzel.lua
+++ b/fuzzel.lua
@@ -54,6 +54,14 @@
Same as above, but uses Damerau-Levenshtein Ratio instead
returns table_sorted
+ fuzzel.FuzzyAutocompleteDistance(string_needle, vararg in)
+ vararg_in can be either a table, or a list of entries, this will fuzzy sort based on the length of the input, which makes it better at autocompletion than fuzzy sorting. Uses Damerau-Levenshtein Distance.
+ returns table_sorted
+
+ fuzzel.FuzzyAutocompleteRatio(string_needle, vararg_in)
+ Same as above, but uses DamerauLevenshteinRatio
+ returns table_sorted
+
Example:
Returns a function that will return the closest string to the string it was passed
-----------------FuzzelExample.lua------------------
@@ -101,13 +109,13 @@
]]--You probably don't want to touch anything past this point
--Assign locals to these to the minifier can compress the file better
-local strlen,chrat,min,asrt,prs,iprs,typ,upack,tblins,tblsrt = string.len,string.byte,math.min,assert,pairs,ipairs,type,unpack,table.insert,table.sort
+local strlen,chrat,min,asrt,prs,iprs,typ,upack,tblins,tblsrt,strsub,tru,fal = string.len,string.byte,math.min,assert,pairs,ipairs,type,unpack,table.insert,table.sort,string.sub,true,false
local fuzzel = {}
--A clever way to allow the minifier to minify function names, this basically just assigns variables with their string equivalent.
-local da, le, di, ra, fu, fi, so, ex, ha = "Damerau", "Levenshtein", "Distance", "Ratio", "Fuzzy", "Find", "Sort", "_extended", "Hamming"
-local LevenshteinDistance_extended,LevenshteinDistance,LevenshteinRatio,DamerauLevenshteinDistance_extended,DamerauLevenshteinDistance,DamerauLevenshteinRatio,FuzzyFindDistance,FuzzyFindRatio,FuzzySortDistance,FuzzySortRatio,HammingDistance,HammingRatio = le..di..ex,le..di,le..ra,da..le..di..ex,da..le..di,da..le..ra,fu..fi..di,fu..fi..ra,fu..so..di,fu..so..ra,ha..di,ha..ra
+local da, le, di, ra, fu, fi, so, ex, ha, au = "Damerau", "Levenshtein", "Distance", "Ratio", "Fuzzy", "Find", "Sort", "_extended", "Hamming", "Autocomplete"
+local LevenshteinDistance_extended,LevenshteinDistance,LevenshteinRatio,DamerauLevenshteinDistance_extended,DamerauLevenshteinDistance,DamerauLevenshteinRatio,FuzzyFindDistance,FuzzyFindRatio,FuzzySortDistance,FuzzySortRatio,HammingDistance,HammingRatio,FuzzyAutocompleteDistance,FuzzyAutocompleteRatio = le..di..ex,le..di,le..ra,da..le..di..ex,da..le..di,da..le..ra,fu..fi..di,fu..fi..ra,fu..so..di,fu..so..ra,ha..di,ha..ra,fu..au..di,fu..au..ra
local function genericDistance( stringa, stringb, addcost, subcost, delcost, ...)
local arg = {...}
@@ -167,7 +175,7 @@ end
fuzzel.lr = fuzzel[LevenshteinRatio]
fuzzel[DamerauLevenshteinDistance_extended] = function(stringa, stringb, addcost, subcost, delcost, trncost)
- return genericDistance(stringa,stringb,addcost,subcost,delcost,true,trncost)
+ return genericDistance(stringa,stringb,addcost,subcost,delcost,tru,trncost)
end
fuzzel.dld_e = fuzzel[DamerauLevenshteinDistance_extended]
@@ -183,7 +191,7 @@ fuzzel.dlr = fuzzel[DamerauLevenshteinRatio]
fuzzel[HammingDistance] = function(stringa,stringb)
local len,dist = strlen(stringa),0
- asrt(len == strlen(stringb),"Hamming Distance cannot be calculated on two strings of different lengths:\"" .. stringa .. "\" \"" .. stringb .. "\"")
+ asrt(len == strlen(stringb), ha.." "..di.." cannot be calculated on two strings of different lengths:\"" .. stringa .. "\" \"" .. stringb .. "\"")
for i = 1,len do
dist = dist + ((chrat(stringa,i) ~= chrat(stringb,i)) and 1 or 0)
end
@@ -222,16 +230,17 @@ fuzzel[FuzzyFindRatio] = function(str,...)
return upack{FuzzySearch(str,fuzzel.dlr,...)}
end
-local function FuzzySort(str, func, ...)
+local function FuzzySort(str, func, short, ...)
local arg = {...}
--allow varargs, or a table
local looparg = typ(arg[1]) == "table" and arg[1] or arg
--Roughly sort everything by it's distance to the string
- local usorted,sorted,otbl = {},{},{}
+ local usorted,sorted,otbl,slen = {},{},{},strlen(str)
for k,v in prs(looparg) do
- local dist = func(str,v)
+ local sstr = short and strsub(v,0,slen) or v
+ local dist = func(str,sstr)
if usorted[dist] == nil then
usorted[dist] = {}
tblins(sorted,dist)
@@ -253,13 +262,23 @@ end
fuzzel.ffr = fuzzel[FuzzyFindRatio]
fuzzel[FuzzySortDistance] = function(str,...)
- return FuzzySort(str,fuzzel.dld,...)
+ return FuzzySort(str,fuzzel.dld,fal,...)
end
fuzzel.fsd = fuzzel[FuzzySortDistance]
fuzzel[FuzzySortRatio] = function(str,...)
- return FuzzySort(str,fuzzel.dlr,...)
+ return FuzzySort(str,fuzzel.dlr,fal,...)
end
fuzzel.fsr = fuzzel[FuzzySortRatio]
+fuzzel[FuzzyAutocompleteDistance] = function(str, ...)
+ return FuzzySort(str,fuzzel.dld,tru,...)
+end
+fuzzel.fad = fuzzel[FuzzyAutocompleteDistance]
+
+fuzzel[FuzzyAutocompleteRatio] = function(str,...)
+ return FuzzySort(str,fuzzel.dlr,tru,...)
+end
+fuzzel.far = fuzzel[FuzzyAutocompleteRatio]
+
return fuzzel