2016-10-12 14:48:37 +00:00
|
|
|
"""
|
|
|
|
Author : Turfa Auliarachman
|
|
|
|
Date : October 12, 2016
|
|
|
|
|
2020-05-01 21:36:35 +00:00
|
|
|
This is a pure Python implementation of Dynamic Programming solution to the edit
|
|
|
|
distance problem.
|
2016-10-12 14:48:37 +00:00
|
|
|
|
|
|
|
The problem is :
|
2020-05-01 21:36:35 +00:00
|
|
|
Given two strings A and B. Find the minimum number of operations to string B such that
|
|
|
|
A = B. The permitted operations are removal, insertion, and substitution.
|
2016-10-12 14:48:37 +00:00
|
|
|
"""
|
2017-11-25 11:41:55 +00:00
|
|
|
|
2016-10-12 14:48:37 +00:00
|
|
|
|
|
|
|
class EditDistance:
|
|
|
|
"""
|
|
|
|
Use :
|
|
|
|
solver = EditDistance()
|
|
|
|
editDistanceResult = solver.solve(firstString, secondString)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__prepare__()
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def __prepare__(self, n=0, m=0):
|
|
|
|
self.dp = [[-1 for y in range(0, m)] for x in range(0, n)]
|
2016-10-12 14:48:37 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def __solve_dp(self, x, y):
|
2019-10-05 05:14:13 +00:00
|
|
|
if x == -1:
|
|
|
|
return y + 1
|
|
|
|
elif y == -1:
|
|
|
|
return x + 1
|
|
|
|
elif self.dp[x][y] > -1:
|
2016-10-12 14:48:37 +00:00
|
|
|
return self.dp[x][y]
|
|
|
|
else:
|
2022-10-12 22:54:20 +00:00
|
|
|
if self.a[x] == self.b[y]:
|
|
|
|
self.dp[x][y] = self.__solve_dp(x - 1, y - 1)
|
2016-10-12 14:48:37 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
self.dp[x][y] = 1 + min(
|
2022-10-12 22:54:20 +00:00
|
|
|
self.__solve_dp(x, y - 1),
|
|
|
|
self.__solve_dp(x - 1, y),
|
|
|
|
self.__solve_dp(x - 1, y - 1),
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
2016-10-12 14:48:37 +00:00
|
|
|
|
|
|
|
return self.dp[x][y]
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def solve(self, a, b):
|
|
|
|
if isinstance(a, bytes):
|
|
|
|
a = a.decode("ascii")
|
2016-10-12 14:48:37 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
if isinstance(b, bytes):
|
|
|
|
b = b.decode("ascii")
|
2016-10-12 14:48:37 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
self.a = str(a)
|
|
|
|
self.b = str(b)
|
2016-10-12 14:48:37 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
self.__prepare__(len(a), len(b))
|
2016-10-12 14:48:37 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
return self.__solve_dp(len(a) - 1, len(b) - 1)
|
2016-10-12 14:48:37 +00:00
|
|
|
|
2019-07-13 07:10:02 +00:00
|
|
|
|
|
|
|
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)
|
2019-10-05 05:14:13 +00:00
|
|
|
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
|
|
|
for i in range(m + 1):
|
|
|
|
for j in range(n + 1):
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if i == 0: # first string is empty
|
2019-07-13 07:10:02 +00:00
|
|
|
dp[i][j] = j
|
2019-10-05 05:14:13 +00:00
|
|
|
elif j == 0: # second string is empty
|
2019-08-19 13:37:49 +00:00
|
|
|
dp[i][j] = i
|
2019-10-05 05:14:13 +00:00
|
|
|
elif (
|
|
|
|
word1[i - 1] == word2[j - 1]
|
|
|
|
): # last character of both substing is equal
|
|
|
|
dp[i][j] = dp[i - 1][j - 1]
|
2019-08-19 13:37:49 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
insert = dp[i][j - 1]
|
|
|
|
delete = dp[i - 1][j]
|
|
|
|
replace = dp[i - 1][j - 1]
|
2019-07-13 07:10:02 +00:00
|
|
|
dp[i][j] = 1 + min(insert, delete, replace)
|
|
|
|
return dp[m][n]
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
solver = EditDistance()
|
2019-07-13 07:10:02 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
print("****************** Testing Edit Distance DP Algorithm ******************")
|
|
|
|
print()
|
2019-07-13 07:10:02 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
S1 = input("Enter the first string: ").strip()
|
|
|
|
S2 = input("Enter the second string: ").strip()
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
print()
|
2022-10-16 20:50:11 +00:00
|
|
|
print(f"The minimum Edit Distance is: {solver.solve(S1, S2)}")
|
|
|
|
print(f"The minimum Edit Distance is: {min_distance_bottom_up(S1, S2)}")
|
2019-10-05 05:14:13 +00:00
|
|
|
print()
|
|
|
|
print("*************** End of Testing Edit Distance DP Algorithm ***************")
|