aboutsummaryrefslogtreecommitdiff
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
parenta6c3997e2100b52315e8de35b2db1147f96586e4 (diff)
downloadfuzzel-38be30a16aef3231b7d0ce7e3d0e2964e12e8870.tar.gz
fuzzel-38be30a16aef3231b7d0ce7e3d0e2964e12e8870.tar.bz2
fuzzel-38be30a16aef3231b7d0ce7e3d0e2964e12e8870.zip
A few changes to shave off a vew more bytes
-rw-r--r--fuzzel.lua28
-rw-r--r--fuzzel_min.lua3
2 files changed, 13 insertions, 18 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)
diff --git a/fuzzel_min.lua b/fuzzel_min.lua
index 13edd2a..752c5d0 100644
--- a/fuzzel_min.lua
+++ b/fuzzel_min.lua
@@ -1 +1,2 @@
-local a,b,c,d,e,f,g,h,i,j=string.len,string.byte,math.min,assert,pairs,ipairs,type,unpack,table.insert,table.sort;local k={}local l,m,n,o,p,q,r,s,t="Damerau","Levenshtein","Distance","Ratio","Fuzzy","Find","Sort","_extended","Hamming"local u,v,w,x,y,z,A,B,C,D,E,F=m..n..s,m..n,m..o,l..m..n..s,l..m..n,l..m..o,p..q..n,p..q..o,p..r..n,p..r..o,t..n,t..o;local function G(H,I,J,K,L,...)local M,N=a(H),a(I)local O={}for P=0,M do O[P]={}for Q=0,N do O[P][Q]=0 end end;for P=1,M do O[P][0]=P end;for Q=1,N do O[0][Q]=Q end;for Q=1,N do for P=1,M do local R=b(H,P)local S=b(I,Q)O[P][Q]=c(O[P-1][Q]+L,O[P][Q-1]+J,O[P-1][Q-1]+(R==S and 0 or K))if arg[1]and P>1 and Q>1 and R==b(I,Q-1)and b(H,P-1)==S then O[P][Q]=c(O[P][Q],O[P-2][Q-2]+(R==S and 0 or arg[2]))end end end;return O[M][N]end;k[u]=function(H,I,J,K,L)return G(H,I,J,K,L)end;k.ld_e=k[u]k[v]=function(H,I)return k.ld_e(H,I,1,1,1)end;k.ld=k[v]k[w]=function(H,I)return k.ld(H,I)/a(H)end;k.lr=k[w]k[x]=function(H,I,J,K,L,T)return G(H,I,J,K,L,true,T)end;k.dld_e=k[x]k[y]=function(H,I)return k.dld_e(H,I,1,1,1,1)end;k.dld=k[y]k[z]=function(H,I)return k.dld(H,I)/a(H)end;k.dlr=k[z]k[E]=function(H,I)local U=a(H)d(U==a(I),"Hamming Distance cannot be calculated on two strings of different lengths:\""..H.."\" \""..I.."\"")local V=0;for P=1,U do V=V+(b(H,P)~=b(I,P)and 1 or 0)end;return V end;k.hd=k[E]k[F]=function(H,I)return k.hd(H,I)/a(H)end;k.hr=k[F]local function W(X,Y,...)local Z=g(arg[1])=="table"and arg[1]or arg;local _=Y(Z[1],X)local a0=Z[1]for a1,a2 in e(Z)do local a3=Y(a2,X)if a3<_ then _=a3;a0=a2 end end;return a0,_ end;k[A]=function(X,...)return h{W(X,k.dld,...)}end;k.ffd=k[A]k[B]=function(X,...)return h{W(X,k.dlr,...)}end;local function a4(X,Y,...)local Z=g(arg[1])=="table"and arg[1]or arg;local a5={}local a6={}for a1,a2 in e(Z)do local V=Y(X,a2)if a5[V]==nil then a5[V]={}i(a6,V)end;i(a5[V],a2)end;j(a6)local a7={}for a1,a2 in f(a6)do for P,Q in e(a5[a2])do i(a7,Q)end end;return a7 end;k.ffr=k[B]k[C]=function(X,...)return a4(X,k.dld,...)end;k.fsd=k[C]k[D]=function(X,...)return a4(X,k.dlr,...)end;k.fsr=k[D]return k
+--This code has been minified! For the original, see cogarr.net/source/cgit.cgi/fuzzel
+local a,b,c,d,e,f,g,h,i,j=string.len,string.byte,math.min,assert,pairs,ipairs,type,unpack,table.insert,table.sort;local k={}local l,m,n,o,p,q,r,s,t="Damerau","Levenshtein","Distance","Ratio","Fuzzy","Find","Sort","_extended","Hamming"local u,v,w,x,y,z,A,B,C,D,E,F=m..n..s,m..n,m..o,l..m..n..s,l..m..n,l..m..o,p..q..n,p..q..o,p..r..n,p..r..o,t..n,t..o;local function G(H,I,J,K,L,...)local M,N=a(H),a(I)local O={}for P=0,M do O[P]={}for Q=0,N do O[P][Q]=0 end end;for P=1,M do O[P][0]=P end;for Q=1,N do O[0][Q]=Q end;for Q=1,N do for P=1,M do local R,S=b(H,P),b(I,Q)O[P][Q]=c(O[P-1][Q]+L,O[P][Q-1]+J,O[P-1][Q-1]+(R==S and 0 or K))if arg[1]and P>1 and Q>1 and R==b(I,Q-1)and b(H,P-1)==S then O[P][Q]=c(O[P][Q],O[P-2][Q-2]+(R==S and 0 or arg[2]))end end end;return O[M][N]end;k[u]=function(H,I,J,K,L)return G(H,I,J,K,L)end;k.ld_e=k[u]k[v]=function(H,I)return k.ld_e(H,I,1,1,1)end;k.ld=k[v]k[w]=function(H,I)return k.ld(H,I)/a(H)end;k.lr=k[w]k[x]=function(H,I,J,K,L,T)return G(H,I,J,K,L,true,T)end;k.dld_e=k[x]k[y]=function(H,I)return k.dld_e(H,I,1,1,1,1)end;k.dld=k[y]k[z]=function(H,I)return k.dld(H,I)/a(H)end;k.dlr=k[z]k[E]=function(H,I)local U,V=a(H),0;d(U==a(I),"Hamming Distance cannot be calculated on two strings of different lengths:\""..H.."\" \""..I.."\"")for P=1,U do V=V+(b(H,P)~=b(I,P)and 1 or 0)end;return V end;k.hd=k[E]k[F]=function(H,I)return k.hd(H,I)/a(H)end;k.hr=k[F]local function W(X,Y,...)local Z=g(arg[1])=="table"and arg[1]or arg;local _,a0=Y(Z[1],X),Z[1]for a1,a2 in e(Z)do local a3=Y(a2,X)if a3<=_ then _,a0=a3,a1 end end;return Z[a0],_ end;k[A]=function(X,...)return h{W(X,k.dld,...)}end;k.ffd=k[A]k[B]=function(X,...)return h{W(X,k.dlr,...)}end;local function a4(X,Y,...)local Z=g(arg[1])=="table"and arg[1]or arg;local a5,a6,a7={},{},{}for a1,a2 in e(Z)do local V=Y(X,a2)if a5[V]==nil then a5[V]={}i(a6,V)end;i(a5[V],a2)end;j(a6)for a1,a2 in f(a6)do for P,Q in e(a5[a2])do i(a7,Q)end end;return a7 end;k.ffr=k[B]k[C]=function(X,...)return a4(X,k.dld,...)end;k.fsd=k[C]k[D]=function(X,...)return a4(X,k.dlr,...)end;k.fsr=k[D]return k