I tested my Wagner-Fischer distance calculator by putting in baana - one letter from banana. I got banana with 1 distance, but also cats with 3, which makes no sense, as it is 4. I have looked at the matrix, and it seems that the three 'a's in baana have somehow messed with the expected answer.
Where did I go wrong?
def calculate_distance(word, destination):
word = '_' + word
destination = '_' + destination
print(word, destination)
wordlen, destinationlen = len(word), len(destination)
distance_matrix = []
[distance_matrix.append([None] * destinationlen) for _ in range(wordlen)]
for i in range(len(distance_matrix)):
distance_matrix[i][0] = i
for i in range(len(distance_matrix[0])):
distance_matrix[0][i] = i
pprint(distance_matrix)
for i, line in enumerate(distance_matrix):
for j, d in enumerate(line):
if d is None:
distance_matrix[i][j] = min(distance_matrix[i-1][j-1], distance_matrix[i][j-1], distance_matrix[i-1][j])
if word[i] != destination[j]:
distance_matrix[i][j] += 1
return distance_matrix[-1][-1]
Try with :