forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
damerau_levenshtein_distance.py
71 lines (56 loc) · 2.23 KB
/
damerau_levenshtein_distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
This script is a implementation of the Damerau-Levenshtein distance algorithm.
It's an algorithm that measures the edit distance between two string sequences
More information about this algorithm can be found in this wikipedia article:
https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
"""
def damerau_levenshtein_distance(first_string: str, second_string: str) -> int:
"""
Implements the Damerau-Levenshtein distance algorithm that measures
the edit distance between two strings.
Parameters:
first_string: The first string to compare
second_string: The second string to compare
Returns:
distance: The edit distance between the first and second strings
>>> damerau_levenshtein_distance("cat", "cut")
1
>>> damerau_levenshtein_distance("kitten", "sitting")
3
>>> damerau_levenshtein_distance("hello", "world")
4
>>> damerau_levenshtein_distance("book", "back")
2
>>> damerau_levenshtein_distance("container", "containment")
3
>>> damerau_levenshtein_distance("container", "containment")
3
"""
# Create a dynamic programming matrix to store the distances
dp_matrix = [[0] * (len(second_string) + 1) for _ in range(len(first_string) + 1)]
# Initialize the matrix
for i in range(len(first_string) + 1):
dp_matrix[i][0] = i
for j in range(len(second_string) + 1):
dp_matrix[0][j] = j
# Fill the matrix
for i, first_char in enumerate(first_string, start=1):
for j, second_char in enumerate(second_string, start=1):
cost = int(first_char != second_char)
dp_matrix[i][j] = min(
dp_matrix[i - 1][j] + 1, # Deletion
dp_matrix[i][j - 1] + 1, # Insertion
dp_matrix[i - 1][j - 1] + cost, # Substitution
)
if (
i > 1
and j > 1
and first_string[i - 1] == second_string[j - 2]
and first_string[i - 2] == second_string[j - 1]
):
# Transposition
dp_matrix[i][j] = min(dp_matrix[i][j], dp_matrix[i - 2][j - 2] + cost)
return dp_matrix[-1][-1]
if __name__ == "__main__":
import doctest
doctest.testmod()