Question: I am trying to implement the edit distance algorithm with an extra cost for substitution(as opposed to insertion or deletion), and I would also like
I am trying to implement the edit distance algorithm with an extra cost for substitution(as opposed to insertion or deletion), and I would also like to have special costs for certain substitutions.(e.g 'a' to 'b' and vice versa could have a cost of 3 and it would be (a,b):3 in the dictionary called subCostList and any combination not in the subcost list would have the cost of defaultSubCost. I would like this algorithm implemented in python.

def editDistance(stri, str2, deleteCost = 1, defaultSubCost = 2, subCostlist = {}): str1 = stri.lower() str2 = str2.lower() sizex = len(str1) + 1 sizey = len(str2) + 1 matrix = np.zeros((sizex, sizey)) for x in range(sizex): matrix[x,0) = x for y in range(sizey): matrix[0,y) = y for x in range(1, sizex): for y in range(1, sizey): if str1[x-1] == str2[y-1]: matrix[x,y] = mind matrix[x-1,y] + 1, matrix[x-1, y-1] , matrix[x, y -1] + 1 ) if matrix(x,y] == matrix[x-1,7-1]: matrix[x,y] += defaultSubCost - 1 else: matrix (x,y) = mind matrix[x-1,y] + 1, matrix[x-1,y-1] + 1, matrix[x,y-1] + 1 ) EE if matrix(x,y] | matrix[x-1, y-1]: #print(matrix) return(matrix[sizex 1, sizey 1])
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
