tenstar tenstar - 2 years ago 166
Python Question

Python string similarity with probability

How do i get the probability of a string being similar to another string in python?

I want to get a decimal value like:

0.9 #means 90%


preferably with standard python and library.

e.g. :

similar("Apple","Appel") #would have a high prob.

similar("Apple","Mango") #would have a lower prob.

Answer Source

There is a built in.

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

Using it:

>>> similar("Apple","Appel")
>>> similar("Apple","Mango")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download