Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.71 KB | None | 0 0
  1. function string.levenshteinDistance( s1, s2 )
  2.     if string.utf8len(s1) == 0 then return string.utf8len(s2) end
  3.     if string.utf8len(s2) == 0 then return string.utf8len(s1) end
  4.  
  5.     if string.utf8sub(s1, -1, -1) == string.utf8sub(s2, -1, -1) then
  6.         return string.levenshteinDistance( string.utf8sub(s1, 1, -2), string.utf8sub(s2, 1, -2) )
  7.     end
  8.  
  9.     local a = string.levenshteinDistance( string.utf8sub(s1, 1, -2), string.utf8sub(s2, 1, -2) )
  10.     local b = string.levenshteinDistance( string.utf8sub(s1, 1, -1), string.utf8sub(s2, 1, -2) )
  11.     local c = string.levenshteinDistance( string.utf8sub(s1, 1, -2), string.utf8sub(s2, 1, -1) )
  12.  
  13.     if a > b then return b + 1 end
  14.     if a > c then return c + 1 end
  15.     return a + 1
  16. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement