我正在使用“模糊匹配”功能Regex https://pypi.python.org/pypi/regex/模块。
如何获得“匹配”的“模糊度值”,它指示模式与字符串的差异程度,就像 Levenshtein 中的“编辑距离”一样?
我以为我可以获取 Match 对象中的值,但它不在那里。官方文档也没有对此说什么。
e.g.:
regex.match('(?:foo){e}','for')
a.captures()
告诉我“for”这个词是匹配的,但我想知道模糊值,应该是1
在这种情况下。
有什么办法可以实现这一点吗?
>>> import difflib
>>> matcher = difflib.SequenceMatcher(None, 'foo', 'for')
>>> sum(size for start, end, size in matcher.get_matching_blocks())
2
>>> max(map(len, ('foo', 'for'))) - _
1
>>>
>>>
>>> matcher = difflib.SequenceMatcher(None, 'foo', 'food')
>>> sum(size for start, end, size in matcher.get_matching_blocks())
3
>>> max(map(len, ('foo', 'food'))) - _
1
http://docs.python.org/2/library/difflib.html#difflib.SequenceMatcher.get_matching_blocks http://docs.python.org/2/library/difflib.html#difflib.SequenceMatcher.get_matching_blocks
http://docs.python.org/2/library/difflib.html#difflib.SequenceMatcher.get_opcodes http://docs.python.org/2/library/difflib.html#difflib.SequenceMatcher.get_opcodes
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)