mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Refactor bottom-up edit distance function to be class method (#7347)
* Refactor bottom-up function to be class method * Add type hints * Update convolve function namespace * Remove depreciated np.float * updating DIRECTORY.md * updating DIRECTORY.md * updating DIRECTORY.md * updating DIRECTORY.md * Renamed function for consistency * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com>
This commit is contained in:
@ -19,74 +19,72 @@ class EditDistance:
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.__prepare__()
|
||||
self.word1 = ""
|
||||
self.word2 = ""
|
||||
self.dp = []
|
||||
|
||||
def __prepare__(self, n=0, m=0):
|
||||
self.dp = [[-1 for y in range(0, m)] for x in range(0, n)]
|
||||
|
||||
def __solve_dp(self, x, y):
|
||||
if x == -1:
|
||||
return y + 1
|
||||
elif y == -1:
|
||||
return x + 1
|
||||
elif self.dp[x][y] > -1:
|
||||
return self.dp[x][y]
|
||||
def __min_dist_top_down_dp(self, m: int, n: int) -> int:
|
||||
if m == -1:
|
||||
return n + 1
|
||||
elif n == -1:
|
||||
return m + 1
|
||||
elif self.dp[m][n] > -1:
|
||||
return self.dp[m][n]
|
||||
else:
|
||||
if self.a[x] == self.b[y]:
|
||||
self.dp[x][y] = self.__solve_dp(x - 1, y - 1)
|
||||
if self.word1[m] == self.word2[n]:
|
||||
self.dp[m][n] = self.__min_dist_top_down_dp(m - 1, n - 1)
|
||||
else:
|
||||
self.dp[x][y] = 1 + min(
|
||||
self.__solve_dp(x, y - 1),
|
||||
self.__solve_dp(x - 1, y),
|
||||
self.__solve_dp(x - 1, y - 1),
|
||||
)
|
||||
insert = self.__min_dist_top_down_dp(m, n - 1)
|
||||
delete = self.__min_dist_top_down_dp(m - 1, n)
|
||||
replace = self.__min_dist_top_down_dp(m - 1, n - 1)
|
||||
self.dp[m][n] = 1 + min(insert, delete, replace)
|
||||
|
||||
return self.dp[x][y]
|
||||
return self.dp[m][n]
|
||||
|
||||
def solve(self, a, b):
|
||||
if isinstance(a, bytes):
|
||||
a = a.decode("ascii")
|
||||
def min_dist_top_down(self, word1: str, word2: str) -> int:
|
||||
"""
|
||||
>>> EditDistance().min_dist_top_down("intention", "execution")
|
||||
5
|
||||
>>> EditDistance().min_dist_top_down("intention", "")
|
||||
9
|
||||
>>> EditDistance().min_dist_top_down("", "")
|
||||
0
|
||||
"""
|
||||
self.word1 = word1
|
||||
self.word2 = word2
|
||||
self.dp = [[-1 for _ in range(len(word2))] for _ in range(len(word1))]
|
||||
|
||||
if isinstance(b, bytes):
|
||||
b = b.decode("ascii")
|
||||
return self.__min_dist_top_down_dp(len(word1) - 1, len(word2) - 1)
|
||||
|
||||
self.a = str(a)
|
||||
self.b = str(b)
|
||||
def min_dist_bottom_up(self, word1: str, word2: str) -> int:
|
||||
"""
|
||||
>>> EditDistance().min_dist_bottom_up("intention", "execution")
|
||||
5
|
||||
>>> EditDistance().min_dist_bottom_up("intention", "")
|
||||
9
|
||||
>>> EditDistance().min_dist_bottom_up("", "")
|
||||
0
|
||||
"""
|
||||
self.word1 = word1
|
||||
self.word2 = word2
|
||||
m = len(word1)
|
||||
n = len(word2)
|
||||
self.dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||
|
||||
self.__prepare__(len(a), len(b))
|
||||
|
||||
return self.__solve_dp(len(a) - 1, len(b) - 1)
|
||||
|
||||
|
||||
def min_distance_bottom_up(word1: str, word2: str) -> int:
|
||||
"""
|
||||
>>> min_distance_bottom_up("intention", "execution")
|
||||
5
|
||||
>>> min_distance_bottom_up("intention", "")
|
||||
9
|
||||
>>> min_distance_bottom_up("", "")
|
||||
0
|
||||
"""
|
||||
m = len(word1)
|
||||
n = len(word2)
|
||||
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||
for i in range(m + 1):
|
||||
for j in range(n + 1):
|
||||
|
||||
if i == 0: # first string is empty
|
||||
dp[i][j] = j
|
||||
elif j == 0: # second string is empty
|
||||
dp[i][j] = i
|
||||
elif (
|
||||
word1[i - 1] == word2[j - 1]
|
||||
): # last character of both substing is equal
|
||||
dp[i][j] = dp[i - 1][j - 1]
|
||||
else:
|
||||
insert = dp[i][j - 1]
|
||||
delete = dp[i - 1][j]
|
||||
replace = dp[i - 1][j - 1]
|
||||
dp[i][j] = 1 + min(insert, delete, replace)
|
||||
return dp[m][n]
|
||||
for i in range(m + 1):
|
||||
for j in range(n + 1):
|
||||
if i == 0: # first string is empty
|
||||
self.dp[i][j] = j
|
||||
elif j == 0: # second string is empty
|
||||
self.dp[i][j] = i
|
||||
elif word1[i - 1] == word2[j - 1]: # last characters are equal
|
||||
self.dp[i][j] = self.dp[i - 1][j - 1]
|
||||
else:
|
||||
insert = self.dp[i][j - 1]
|
||||
delete = self.dp[i - 1][j]
|
||||
replace = self.dp[i - 1][j - 1]
|
||||
self.dp[i][j] = 1 + min(insert, delete, replace)
|
||||
return self.dp[m][n]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -99,7 +97,7 @@ if __name__ == "__main__":
|
||||
S2 = input("Enter the second string: ").strip()
|
||||
|
||||
print()
|
||||
print(f"The minimum Edit Distance is: {solver.solve(S1, S2)}")
|
||||
print(f"The minimum Edit Distance is: {min_distance_bottom_up(S1, S2)}")
|
||||
print(f"The minimum edit distance is: {solver.min_dist_top_down(S1, S2)}")
|
||||
print(f"The minimum edit distance is: {solver.min_dist_bottom_up(S1, S2)}")
|
||||
print()
|
||||
print("*************** End of Testing Edit Distance DP Algorithm ***************")
|
||||
|
Reference in New Issue
Block a user