问题描述
我是在Python的difflib包中使用SequenceMatcher方法来识别字符串相似性。虽然我遇到了奇怪的行为,但我相信我的问题可能与程序包的垃圾过滤器有关,这是一个详细描述的问题。只要说,我以为我可以解决我的问题,通过将一个autojunk标志传递给我的SequenceMatcher以:
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'
有没有人知道我如何将autojunk = False标志传递给SequenceMatcher?我会感谢他人可以提供的任何建议。
Does anyone know how I might pass the autojunk=False flag to SequenceMatcher? I would be grateful for any suggestions others can offer.
推荐答案
根据:
新在版本 2.7.1 中:autojunk参数。
New in version 2.7.1: The autojunk parameter.
升级到Python 2.7.1+以使用 autojunk
参数。
Upgrade to Python 2.7.1+ to use autojunk
parameter.
这篇关于Python:在difflib中传递SequenceMatcher“autojunk = False”标志产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!