Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Levenshtein distance implemented in lua
- Made by darraghd493
- ]]
- local module = {}
- module.distance = function(s1, s2)
- s1 = string.lower(s1)
- s2 = string.lower(s2)
- local costs = {}
- for i=1,#s1 do
- local last = i;
- for j=1,#2 do
- if i == 1 then
- costs[j] = j
- elseif j > 1 then
- local newValue = costs[j-1]
- if s1:sub(i,i) ~= s2:sub(j,j) then
- newValue = math.min(math.min(newValue, last), costs[j]) + 1
- end
- costs[j-1] = last
- last = newValue
- end
- end
- end
- return costs[#costs]
- end
- module.similarity = function(s1, s2)
- local longer = if #s1 > #s2 then s1 else s2
- local shorter = if #s1 < #s2 then s1 else s2
- if #longer == 0 then return 1 end
- return (#longer - module.distance(longer, shorter)) / #longer
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment