简体   繁体   中英

Set cost for levenshtein distance?

I have a python code that succesfully runs for computing levenshtein distance but now I want to compute costs for the insert, replace and delete.

My code snippet is

def levenshtein_distance(first, second): 

    matrix = np.zeros((len(first)+1,len(second)+1), dtype=np.int)
    for i in range(len(first)+1): 
        for j in range(len(second)+1): 
            if i == 0:  
                matrix[i][j] = j  

            elif j == 0: 
                matrix[i][j] = i
            else: 
                matrix[i][j] = min(matrix[i][j-1] + 2,  
                                   matrix[i-1][j] + 1,        
                                   matrix[i-1][j-1] + 3)     
    return matrix[len(first)][len(second)]

My costs are

Insert: 2 Delete: 1 Replace: 3

On giving these costs inside min() function it somehow miscalculates the cost for substitution. How to calculte the cost ?

Nowhere in your code do you compare the content of first or second . Have a look at this pseudocode implementation: https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM