gpt4 book ai didi

Python "regex"模块 : Fuzziness value

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:56 24 4
gpt4 key购买 nike

我正在使用 Regex 的“模糊匹配”功能模块。

如何获得“匹配”的“模糊值”,它表示模式与字符串的不同程度,就像 Levenshtein 中的“编辑距离”一样?

我以为我可以获取 Match 对象中的值,但它不存在。官方文档对此什么也没说。

例如:

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_opcodes

关于Python "regex"模块 : Fuzziness value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17023862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com