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