aboutsummaryrefslogtreecommitdiff
path: root/test/glum_test.lua
blob: 4bd5e1022b238d9ea04317954ff59358c71c83a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
--local glum = dofile("../src/test.lua")
local glum = dofile("../src/test2.lua")
local str1 = [[
--This is a test
print("hello")
local abracadabra = "b"
print(abracadabra)
]]
local str2 = [[
--This is another test
local somtbl = {}
somtbl.test = function(self, what, who, where, when)
    print("Thanks, I love " .. what .. "!")
end
somtbl:test(23)
]]
local str3 = [[

--Assign locals to these to the minifier can compress the file better
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, 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 = {...}

    --Length of each string
    local salen, sblen = strlen(stringa), strlen(stringb)

    --Create a 0 matrix the size of len(a) x len(b)
    local dyntbl = {}
    for i = 0,salen do
        dyntbl[i] = {}
        for j = 0,sblen do
            dyntbl[i][j] = 0
        end
    end

    --Initalize the matrix
    for i = 1,salen do
        dyntbl[i][0] = i
    end
    for j = 1,sblen do
        dyntbl[0][j] = j
    end

    --And build up the matrix based on costs-so-far
    for j = 1,sblen do
        for i = 1,salen do
            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
                dyntbl[i-1][j-1] + (ca == cb and 0 or subcost) --substituion
            )
            if arg[1] and i > 1 and j > 1 and ca == chrat(stringb,j-1) and chrat(stringa,i-1) == cb then
                dyntbl[i][j] = min(dyntbl[i][j],
                    dyntbl[i-2][j-2] + (ca == cb and 0 or arg[2])) --transposition
            end
        end
    end

    return dyntbl[salen][sblen]
end

fuzzel[LevenshteinDistance_extended] = function(stringa, stringb, addcost, subcost, delcost)
    return genericDistance(stringa, stringb, addcost, subcost, delcost)
end
fuzzel.ld_e = fuzzel[LevenshteinDistance_extended]

fuzzel[LevenshteinDistance] = function(stringa,stringb)
    return fuzzel.ld_e(stringa,stringb,1,1,1)
end
fuzzel.ld = fuzzel[LevenshteinDistance]

fuzzel[LevenshteinRatio] = function(stringa,stringb)
    return fuzzel.ld(stringa,stringb) / strlen(stringa)
end
fuzzel.lr = fuzzel[LevenshteinRatio]

fuzzel[DamerauLevenshteinDistance_extended] = function(stringa, stringb, addcost, subcost, delcost, trncost)
    return genericDistance(stringa,stringb,addcost,subcost,delcost,tru,trncost)
end
fuzzel.dld_e = fuzzel[DamerauLevenshteinDistance_extended]

fuzzel[DamerauLevenshteinDistance] = function(stringa,stringb)
    return fuzzel.dld_e(stringa,stringb,1,1,1,1)
end
fuzzel.dld = fuzzel[DamerauLevenshteinDistance]

fuzzel[DamerauLevenshteinRatio] = function(stringa,stringb)
    return fuzzel.dld(stringa,stringb) / strlen(stringa)
end
fuzzel.dlr = fuzzel[DamerauLevenshteinRatio]

fuzzel[HammingDistance] = function(stringa,stringb)
    local len,dist = strlen(stringa),0
    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
    return dist
end
fuzzel.hd = fuzzel[HammingDistance]

fuzzel[HammingRatio] = function(stringa,stringb)
    return fuzzel.hd(stringa,stringb) / strlen(stringa)
end
fuzzel.hr = fuzzel[HammingRatio]

local function FuzzySearch(str,func,...)
    local arg = {...}

    --Allow varargs, or a table
    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,sout = func(looparg[1],str),looparg[1]
    for k,v in prs(looparg) do
        local t = func(v,str)
        if t <= tmin then
            tmin,sout = t,k
        end
    end
    return looparg[sout], tmin
end

fuzzel[FuzzyFindDistance] = function(str,...)
    return upack{FuzzySearch(str,fuzzel.dld,...)}
end
fuzzel.ffd = fuzzel[FuzzyFindDistance]

fuzzel[FuzzyFindRatio] = function(str,...)
    return upack{FuzzySearch(str,fuzzel.dlr,...)}
end
fuzzel.ffr = fuzzel[FuzzyFindRatio]

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,slen = {},{},{},strlen(str)
    for k,v in prs(looparg) do
        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)
        end
        tblins(usorted[dist],v)
    end

    --Actually sort them into something can can be iterated with ipairs
    tblsrt(sorted)

    --Then build our output table
    for k,v in iprs(sorted) do
        for i,j in prs(usorted[v]) do
            tblins(otbl,j)
        end
    end
    return otbl
end
fuzzel.ffr = fuzzel[FuzzyFindRatio]

fuzzel[FuzzySortDistance] = function(str,...)
    return FuzzySort(str,fuzzel.dld,fal,...)
end
fuzzel.fsd = fuzzel[FuzzySortDistance]

fuzzel[FuzzySortRatio] = function(str,...)
    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
]]

local str4 = [[
local function safe_str (str)
    str = string.gsub(str,"\\", "\\\\")
    str = string.gsub(str,"\a", "\\a")
    str = string.gsub(str,"\b", "\\b")
    str = string.gsub(str,"\f", "\\f")
    str = string.gsub(str,"\n", "\\n")
    str = string.gsub(str,"\r", "\\r")
    str = string.gsub(str,"\t", "\\t")
    str = string.gsub(str,"\v", "\\v")
    str = string.gsub(str,"\'", "\\'")
    str = string.gsub(str,"\"", "\\\"")
    return str
end
]]

local str6 = [[
local function findsomenewcharacters(str)
    for character in string.gmatch(str,".") do
        if character == "\n\\t\n" then
            print("I found a newline!")
        end
    end
end
]]
--[[
local function a(b)
    b = string.gsub(b, "\\", "\\\\")
    b = string.gsub(b, "\7", "\\\7")
    b = string.gsub(b, "\8", "\\\8")
    b = string.gsub(b, "\12", "\\\12")
    b = string.gsub(b, "\10", "\10")
    b = string.gsub(b, "\13", "\10")
    b = string.gsub(b, "\9", "\\\9")
    b = string.gsub(b, "\11", "\\\11")
    b = string.gsub(b, "\'", "\\\'")
    b = string.gsub(b, "\"", "\\\"")

    return b
end
]]

local f5 = io.open("../src/glum.lua", "r")
local str5 = f5:read("*a")
f5:close()

--print("Glum:")
--print(glum)
--print(glum.minify)
--local f = glum.minify(str5)
--print(string.dump(loadstring(f)))

--print(glum.minify(str5))
local min = glum.minify(str5)
local comp = glum.uglify(min)
print(comp)

--[[
local fuzzel = loadstring(f)()
local options = {
    "test",
    "toste",
    "testing",
    "teste",
}
print(fuzzel.ffd("tennor",options))
]]