December 2018
Intermediate to advanced
318 pages
8h 28m
English
The following code is a Python-based implementation of the iterative Levenshtein distance:
def iterative_levenshtein(a, b): rows = len(a)+1 cols = len(b)+1 dist = [[0 for x in range(cols)] for x in range(rows)]
The preceding dist[i,j] function contains the Levenshtein distance between the i and j characters of the sequences a and b:
#edit distance by deleting character for i in range(1, rows): dist[i][0] = i # edit distance by inserting the characters for i in range(1, cols): dist[0][i] = i
The edit distances are computed either by deleting or by inserting characters from/into the string sequences:
for col in range(1, cols): for row in range(1, rows): if s[row-1] == t[col-1]: cost = 0 ...
Read now
Unlock full access