Test if two strings are probably equal

If you are implementing anti-flood control then it's much better to test if two strings are probably equal, than doing an exact match.

Here is how I implement this in Python:

import difflib

def probably_equal(str1, str2):
    s = difflib.SequenceMatcher(None, str1, str2)
    ratio = s.ratio()
    if ratio >= 0.8:
        return True, ratio
    return False, ratio

def test(str1, str2):
    are_equal, ratio = probably_equal(str1, str2)
    print 'Equal test: "%s..." == "%s..."' % (str1[0:10], str2[0:10])
    print 'are_equal: %s [ratio: %s]' % (are_equal, ratio)
    print '---'

test("I am NOT spamming, don't know what is going on."
     "I think I'll have to talk to Amix. plurk.com/p/i9e... plurk.com/p/i9jz7 #plurk",
     "I am NOT spamming, don't know what is going on."
     "I think I'll have to talk to Amix. plurk.com/p/i9e... plurk.com/p/i9jmk #plurk")

test("i am amir ok?", "i am amix ok?")

test("maybe we are", "maybe we aren't equal")

The above code will print following:

Equal test: "I am NOT s..." == "I am NOT s..."
are_equal: True [ratio: 0.984]
---
Equal test: "i am amir ..." == "i am amix ..."
are_equal: True [ratio: 0.923076923077]
---
Equal test: "maybe we a..." == "maybe we a..."
are_equal: False [ratio: 0.727272727273]
Code · Python · Tips 5. Mar 2009
© Amir Salihefendic. Powered by Skeletonz.