44-- Iterative matrix-based method
55-- See: http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix
66
7- -- Return the minimum of three elements
8- local function min (a , b , c )
9- return math.min (math.min (a , b ), c )
10- end
11-
127-- Creates a 2D matrix
138local function matrix (row ,col )
149 local m = {}
@@ -30,7 +25,7 @@ local function lev_iter_based(strA,strB)
3025 if (strA :sub (i - 1 ,i - 1 ) == strB :sub (j - 1 ,j - 1 )) then cost = 0
3126 else cost = 1
3227 end
33- M [i ][j ] = min (M [i - 1 ][j ]+ 1 ,M [i ][j - 1 ]+ 1 ,M [i - 1 ][j - 1 ]+ cost )
28+ M [i ][j ] = math. min (M [i - 1 ][j ]+ 1 ,M [i ][j - 1 ]+ 1 ,M [i - 1 ][j - 1 ]+ cost )
3429 end
3530 end
3631 return M [row ][col ]
@@ -45,7 +40,7 @@ local function lev_recursive_based(strA, strB, s, t)
4540 if s == 0 then return t end
4641 if t == 0 then return s end
4742 local cost = strA :sub (s ,s ) == strB :sub (t ,t ) and 0 or 1
48- return min (
43+ return math. min (
4944 lev_recursive_based (strA , strB , s - 1 , t ) + 1 ,
5045 lev_recursive_based (strA , strB , s , t - 1 ) + 1 ,
5146 lev_recursive_based (strA , strB , s - 1 , t - 1 ) + cost
0 commit comments