Getting incorrect score from fuzzy wuzzy partial_ratio
<p>I am fairly new to Python and I am trying to use fuzzy wuzzy for fuzzy matching. I believe I am getting incorrect scores for matches using the partial_ratio function. Here is my exploratory code:</p>

<pre><code>&gt;&gt;&gt;from fuzzywuzzy import fuzz
&gt;&gt;&gt;fuzz.partial_ratio('Subject: Dalki Manganese Ore Mine of M/S Bharat Process and Mechanical Engineers Ltd., Villages Dalki, Soyabahal, Sading and Thakurani R.F., Tehsil Barbil, Distt, Keonjhar, Orissa environmental clearance','Barbil')
50
</code></pre>

<p>I believe this should return a score of 100, since the second string, 'Barbil', is contained in the first string. When I try taking off a few characters at the end or beginning of the first string, I get a matching score of 100. </p>

<pre><code>&gt;&gt;&gt;fuzz.partial_ratio('Subject: Dalki Manganese Ore Mine of M/S Bharat Process and Mechanical Engineers Ltd., Villages Dalki, Soyabahal, Sading and Thakurani R.F., Tehsil Barbil, Distt, Keonjhar, Orissa environmental clear','Barbil')
100
&gt;&gt;&gt; fuzz.partial_ratio('ect: Dalki Manganese Ore Mine of M/S Bharat Process and Mechanical Engineers Ltd., Villages Dalki, Soyabahal, Sading and Thakurani R.F., Tehsil Barbil, Distt, Keonjhar, Orissa environmental clearance','Orissa')
100
</code></pre>

<p>It seems to switch from a score of 50 to a score of 100 when the length of the first string goes to 199. Does anyone have insight as to what might be happening?</p>

<p>It's because when one of the strings is <a href="https://docs.python.org/3.4/library/difflib.html#difflib.SequenceMatcher" rel="nofollow">200 characters or longer, an automatic junk heuristic gets turned on in python's SequenceMatcher</a>. 
This code should work for you:</p>

<pre><code>from difflib import SequenceMatcher

def partial_ratio(s1, s2):
    """"Return the ratio of the most similar substring
    as a number between 0 and 100."""

    if len(s1) &lt;= len(s2):
        shorter = s1
        longer = s2
    else:
        shorter = s2
        longer = s1

    m = SequenceMatcher(None, shorter, longer, autojunk=False)
    blocks = m.get_matching_blocks()

    # each block represents a sequence of matching characters in a string
    # of the form (idx_1, idx_2, len)
    # the best partial match will block align with at least one of those blocks
    #   e.g. shorter = "abcd", longer = XXXbcdeEEE
    #   block = (1,3,3)
    #   best score === ratio("abcd", "Xbcd")
    scores = []
    for (short_start, long_start, _) in blocks:
        long_end = long_start + len(shorter)
        long_substr = longer[long_start:long_end]

        m2 = SequenceMatcher(None, shorter, long_substr, autojunk=False)
        r = m2.ratio()
        if r &gt; .995:
            return 100
        else:
            scores.append(r)

    return max(scores) * 100.0
</code></pre>

python fuzzy-comparison fuzzywuzzy