有没有一个很好的方法可以使用levenstein距离将一个特定的字符串匹配到另一个较长字符串内的任何区域?在
示例:
str1='aaaaa'
str2='bbbbbbaabaabbbb'
if str1 in str2 with a distance < 2:
return True
因此在上面的示例中,字符串2的部分是aabaa
和{},因此该语句应该返回True
。在
我能想到的唯一方法就是一次从str2取5个字符,与str1比较,然后在str2中重复这个过程。不幸的是,这看起来效率很低,我需要用这种方式处理大量数据。在
Tags:
诀窍是生成所有长度适当的
b
的子串,然后对每个子串进行比较。在您可以看看支持模糊匹配的regex module:
因为你要找的是长度相等的字符串,你也可以做一个Hamming distance这可能比在同两个字符串上的Levenstein距离快得多:
^{pr2}$诀窍通常是使用插入(表示较短)或删除(表示较长)成本。你也可以考虑改用达默劳·列文什坦。 https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
相关问题 更多 >
编程相关推荐