MCPcopy Create free account
hub / github.com/antlr/codebuff / levenshtein

Function levenshtein

python/src/lev.py:7–24  ·  view source on GitHub ↗

From Wikipedia article; Iterative with two matrix rows.

(s, t)

Source from the content-addressed store, hash-verified

5import sys
6
7def levenshtein(s, t):
8 ''' From Wikipedia article; Iterative with two matrix rows. '''
9 if s == t: return 0
10 elif len(s) == 0: return len(t)
11 elif len(t) == 0: return len(s)
12 v0 = [None] * (len(t) + 1)
13 v1 = [None] * (len(t) + 1)
14 for i in range(len(v0)):
15 v0[i] = i
16 for i in range(len(s)):
17 v1[0] = i + 1
18 for j in range(len(t)):
19 cost = 0 if s[i] == t[j] else 1
20 v1[j + 1] = min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost)
21 for j in range(len(v0)):
22 v0[j] = v1[j]
23
24 return v1[len(t)]
25
26f1 = sys.argv[1]
27f2 = sys.argv[2]

Callers 1

lev.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected