Python:在 difflib 中传递 SequenceMatcher“autojunk=False”标志会产生错误

2024-01-17

I am trying https://stackoverflow.com/questions/20267564/python-find-maximum-length-of-all-n-word-length-substrings-shared-by-two-string使用Python的difflib包中的SequenceMatcher方法来识别字符串相似度。不过,我在该方法中遇到了奇怪的行为,我相信我的问题可能与包的“垃圾”过滤器有关,这是一个详细描述的问题here http://bugs.python.org/issue2986。可以这么说,我认为我可以通过按照以下描述的方式将 autojunk 标志传递给我的 SequenceMatcher 来解决我的问题差异库文档 http://docs.python.org/2/library/difflib.html#sequencematcher-objects:

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")

但这会产生以下错误消息:

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?我将不胜感激其他人可以提供的任何建议。


根据SequenceMatcher文档 http://docs.python.org/2/library/difflib.html#sequencematcher-objects:

可选参数autojunk可用于禁用自动垃圾启发式。

新版本中2.7.1:自动垃圾参数。

升级到Python 2.7.1+即可使用autojunk范围。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python:在 difflib 中传递 SequenceMatcher“autojunk=False”标志会产生错误 的相关文章

随机推荐