Python: Passing SequenceMatcher in difflib an "autojunk=False" flag yields error

1.1k views Asked by At

I am trying to use the SequenceMatcher method in Python's difflib package to identify string similarity. I have experienced strange behavior with the method, though, and I believe my problem may be related to the package's "junk" filter, a problem described in detail here. Suffice it to say that I thought I could fix my problem by passing an autojunk flag to my SequenceMatcher in the way described by the difflib documentation:

import difflib

def matches(s1, s2):
    s = difflib.SequenceMatcher(None, s1, s2, autojunk=False)
    match = [s1[i:i+n] for i, j, n in s.get_matching_blocks() if n > 0]
    return match

print matches("they all are white a sheet of spotless paper when they first are born but they are to be scrawled upon and blotted by every goose quill", "you are all white a sheet of lovely spotless paper when you first are born but you are to be scrawled and blotted by every gooses quill")

But this yields the following error message:

Traceback (most recent call last):
  File "test3.py", line 8, in <module>
    print matches("they all are white a sheet of spotless paper when they first are born but they are to be scrawled upon and blotted by every goose quill", "you are all white a sheet of lovely spotless paper when you first are born but you are to be scrawled and blotted by every gooses quill")
  File "test3.py", line 4, in matches
    s = difflib.SequenceMatcher(None, s1, s2, autojunk=False)
TypeError: __init__() got an unexpected keyword argument 'autojunk'

Does anyone know how I might pass the autojunk=False flag to SequenceMatcher? I would be grateful for any suggestions others can offer.

1

There are 1 answers

1
falsetru On BEST ANSWER

According to SequenceMatcher documentation:

The optional argument autojunk can be used to disable the automatic junk heuristic.

New in version 2.7.1: The autojunk parameter.

Upgrade to Python 2.7.1+ to use autojunk parameter.