Skip to content

Commit

Permalink
Merge pull request #120 from Jesin/feature/constructor-operatives
Browse files Browse the repository at this point in the history
Operatives for working with enums
  • Loading branch information
ErikMcClure authored Jan 6, 2025
2 parents b8476fb + 738a737 commit 91e7a93
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 161 deletions.
43 changes: 43 additions & 0 deletions alicorn-utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,47 @@ function M.get_cursor_position(input_file, output_file)
return cursor_line, cursor_column
end

-- https://gist.github.com/Badgerati/3261142
-- Returns the Levenshtein distance between the two given strings
function M.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

return M
Loading

0 comments on commit 91e7a93

Please sign in to comment.