Created
November 12, 2017 05:26
-
-
Save wtsnjp/2b193a5b8096126050055f12ea90ee43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env texlua | |
-- | |
-- usage: texlua fuzzy.lua KEYWORD | |
-- | |
kpse.set_program_name(arg[-1]) | |
function main() | |
if not arg[1] then | |
os.exit(1) | |
end | |
tlp_doclist = load_tlpdb_cache() | |
min, result = 100, '' | |
start = os.clock() | |
-- fuzzy search: | |
-- select a tlp whose Levenshtein distance to keyword is the smallest | |
for p in pairs(tlp_doclist) do | |
tmp = levenshtein(arg[1], p) | |
if tmp < min then | |
min, result = tmp, p | |
end | |
end | |
stop = os.clock() | |
if min < 100 then | |
print('Result: ' .. result) | |
print('Levenshtein distance: ' .. min) | |
end | |
print('Time: ' .. ((stop - start)*1) .. '[s]') | |
end | |
function load_tlpdb_cache() | |
local TEXMFVAR = kpse.var_value('TEXMFVAR') | |
local filename = TEXMFVAR .. '/texdoc/cache-tlpdb.lua' | |
local s_meta, tlp_from_runfile, tlp_doclist = dofile(filename) | |
return tlp_doclist | |
end | |
-- definition from <http://lua-users.org/lists/lua-l/2008-01/msg00095.html> | |
function levenshtein(s, t) | |
local s, t = tostring(s), tostring(t) | |
if type(s) == 'string' and type(t) == 'string' then | |
local m, n, d = #s, #t, {} | |
for i = 0, m do d[i] = { [0] = i } end | |
for j = 1, n do d[0][j] = j end | |
for i = 1, m do | |
for j = 1, n do | |
local cost = s:sub(i,i) == t:sub(j,j) and 0 or 1 | |
d[i][j] = math.min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost) | |
end | |
end | |
return d[m][n] | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment