Skip to content

Instantly share code, notes, and snippets.

@creationix
Created November 19, 2014 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/95d2016f42e4cf619e51 to your computer and use it in GitHub Desktop.
Save creationix/95d2016f42e4cf619e51 to your computer and use it in GitHub Desktop.
Make luvit more helpful by suggesting possible corrections.
local uv = require('uv')
local colorize = require('utils').colorize
uv = setmetatable(uv, {
__index = function (table, wanted)
local closest = math.huge
local name = nil
for key in pairs(table) do
local distance = string.levenshtein(key, wanted)
if distance < closest then
closest = distance
name = key
end
end
print("Warning: " .. colorize("failure", "uv." .. wanted) .. " is nil, did you mean " .. colorize("success", "uv." .. name) .. "?")
end
})
function string.levenshtein(str1, str2)
local len1 = string.len(str1)
local len2 = string.len(str2)
local matrix = {}
local cost = 0
-- quick cut-offs to save time
if (len1 == 0) then
return len2
elseif (len2 == 0) then
return len1
elseif (str1 == str2) then
return 0
end
-- initialise the base matrix values
for i = 0, len1, 1 do
matrix[i] = {}
matrix[i][0] = i
end
for j = 0, len2, 1 do
matrix[0][j] = j
end
-- actual Levenshtein algorithm
for i = 1, len1, 1 do
for j = 1, len2, 1 do
if (str1:byte(i) == str2:byte(j)) then
cost = 0
else
cost = 1
end
matrix[i][j] = math.min(matrix[i-1][j] + 1, matrix[i][j-1] + 1, matrix[i-1][j-1] + cost)
end
end
-- return the last value - this is the Levenshtein distance
return matrix[len1][len2]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment