Levenshtein distance with bound/limit

后端 未结 1 698
天涯浪人
天涯浪人 2021-01-13 17:25

I have found some Python implementations of the Levenshtein distance.

I am wondering though how these algorithms can be efficiently modified so that the

1条回答
  •  抹茶落季
    2021-01-13 17:41

    As described in Levenstein distance limit, you can add a test over the row that is computed to return early:

    def levenshtein(s1, s2, maximum):
        if len(s1) > len(s2):
            s1, s2 = s2, s1
    
        distances = range(len(s1) + 1)
        for i2, c2 in enumerate(s2):
            distances_ = [i2+1]
            for i1, c1 in enumerate(s1):
                if c1 == c2:
                    distances_.append(distances[i1])
                else:
                    distances_.append(1 + min((distances[i1], distances[i1 + 1], distances_[-1])))
            if all((x >= maximum for x in distances_)):
                return False
            distances = distances_
        return distances[-1]
    

    0 讨论(0)
提交回复
热议问题