aboutsummaryrefslogtreecommitdiff
path: root/fuzzel.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2016-06-06 18:15:42 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2016-06-06 18:15:42 -0400
commit38be30a16aef3231b7d0ce7e3d0e2964e12e8870 (patch)
tree27e68a8175747ac1f871dc9a2b91989c98cb80b4 /fuzzel.lua
parenta6c3997e2100b52315e8de35b2db1147f96586e4 (diff)
downloadfuzzel-38be30a16aef3231b7d0ce7e3d0e2964e12e8870.tar.gz
fuzzel-38be30a16aef3231b7d0ce7e3d0e2964e12e8870.tar.bz2
fuzzel-38be30a16aef3231b7d0ce7e3d0e2964e12e8870.zip
A few changes to shave off a vew more bytes
Diffstat (limited to 'fuzzel.lua')
-rw-r--r--fuzzel.lua28
1 files changed, 11 insertions, 17 deletions
diff --git a/fuzzel.lua b/fuzzel.lua
index 0bb7611..6149f92 100644
--- a/fuzzel.lua
+++ b/fuzzel.lua
@@ -1,5 +1,5 @@
--[[
- Fuzzel v1.1 - Alexander "Apickx" Pickering
+ Fuzzel v1.2 - Alexander "Apickx" Pickering
Entered into the public domain June 2, 2016
You are not required to, but consider putting a link to the source in your file's comments!
@@ -39,7 +39,7 @@
returns number_ratio
fuzzel.FuzzyFindDistance(string_needle, vararg_in)
- in may be either a table, or a list of arguments. fuzzel.FuzzySearchDistance will find the string that most closely resembles needle, based on Damerau-Levenshtein Distance
+ in may be either a table, or a list of arguments. fuzzel.FuzzySearchDistance will find the string that most closely resembles needle, based on Damerau-Levenshtein Distance. If multiple options have the same distance, it will return the first one encountered (This may not be in any sort of order!)
returns string_closest, number_distance
fuzzel.FuzzyFindRatio(string_needle, vararg_in)
@@ -67,11 +67,11 @@
"Brown Fox",
}
- --And use it, to see what option closest matches "Brown Cat"
+ --And use it, to see what option closest matches "Lulzy Cat"
local close,distance = fuzzel.FuzzyFindDistance("Lulzy Cat", options)
print("\"Lulzy Cat\" is close to \"" .. close .. "\", distance:" .. distance)
- --Sort the options to see the order in which they most closely match "Brown Cat"
+ --Sort the options to see the order in which they most closely match "Frag God"
print("\"Frag God\" is closest to:")
for k,v in ipairs(fuzzel.FuzzySortRatio("Frag God",options)) do
print(k .. "\t:\t" .. v)
@@ -133,8 +133,7 @@ local function genericDistance( stringa, stringb, addcost, subcost, delcost, ...
--And build up the matrix based on costs-so-far
for j = 1,sblen do
for i = 1,salen do
- local ca = chrat(stringa,i)
- local cb = chrat(stringb,j)
+ local ca,cb = chrat(stringa,i),chrat(stringb,j)
dyntbl[i][j] = min(
dyntbl[i-1][j] + delcost, --deletion
dyntbl[i][j-1] + addcost, --insertion
@@ -181,9 +180,8 @@ end
fuzzel.dlr = fuzzel[DamerauLevenshteinRatio]
fuzzel[HammingDistance] = function(stringa,stringb)
- local len = strlen(stringa)
+ local len,dist = strlen(stringa),0
asrt(len == strlen(stringb),"Hamming Distance cannot be calculated on two strings of different lengths:\"" .. stringa .. "\" \"" .. stringb .. "\"")
- local dist = 0
for i = 1,len do
dist = dist + ((chrat(stringa,i) ~= chrat(stringb,i)) and 1 or 0)
end
@@ -201,16 +199,14 @@ local function FuzzySearch(str,func,...)
local looparg = typ(arg[1]) == "table" and arg[1] or arg
--Find the string with the shortest distance to the string we were supplied
- local tmin = func(looparg[1],str)
- local sout = looparg[1]
+ local tmin,sout = func(looparg[1],str),looparg[1]
for k,v in prs(looparg) do
local t = func(v,str)
- if t < tmin then
- tmin = t
- sout = v
+ if t <= tmin then
+ tmin,sout = t,k
end
end
- return sout, tmin
+ return looparg[sout], tmin
end
fuzzel[FuzzyFindDistance] = function(str,...)
@@ -227,8 +223,7 @@ local function FuzzySort(str, func, ...)
local looparg = typ(arg[1]) == "table" and arg[1] or arg
--Roughly sort everything by it's distance to the string
- local usorted = {}
- local sorted = {}
+ local usorted,sorted,otbl = {},{},{}
for k,v in prs(looparg) do
local dist = func(str,v)
if usorted[dist] == nil then
@@ -242,7 +237,6 @@ local function FuzzySort(str, func, ...)
tblsrt(sorted)
--Then build our output table
- local otbl = {}
for k,v in iprs(sorted) do
for i,j in prs(usorted[v]) do
tblins(otbl,j)