Monday, 22 March 2021

How to find string similar to 2 other strings (in terms of Levenshtein distance)?

Let's say I have 2 strings which is pretty similar. I want to find other string which is close to s1 and s2 in terms of Levenshtein distance.

import Levenshtein
s1 = 'aaabbbccc'
s2 = 'abacbbbccde'
dist = Levenshtein.distance(s1, s2)
print(dist)
mid_str = get_avg_string(s1, s2)

What can be effective implementation of function:

def get_avg_string(s1, s2):
    return ''

I need that this variables:

sum_lev = Levenshtein.distance(s1, mid_str) + Levenshtein.distance(s2, mid_str)
diff_lev = abs(Levenshtein.distance(s1, mid_str) - Levenshtein.distance(s2, mid_str)

to be minimal (I think sum_lev will be equal to dist and diff_lev <= 1).



from How to find string similar to 2 other strings (in terms of Levenshtein distance)?

No comments:

Post a Comment