String similarity metrics

Viewed 24

This code checks the similarity of sentences

normalized1 = s1.lower()
normalized2 = s2.lower()
matcher = difflib.SequenceMatcher(None, normalized1, normalized2)
print(matcher.ratio())

But he has a minus, if you swap words in places, then the similarity will already be much less. How can this be fixed?

s1 = 'Hello wolrd'
s2 = 'Hello wolds'
normalized1 = s1.lower()
normalized2 = s2.lower()
matcher = difflib.SequenceMatcher(None, normalized1, normalized2)
print(matcher.ratio()) # 0.91666666

s1 = 'Hello wolrd'
s2 = 'Wolds hello'
normalized1 = s1.lower()
normalized2 = s2.lower()
matcher = difflib.SequenceMatcher(None, normalized1, normalized2)
print(matcher.ratio()) # 0.45454545
0 Answers
Related