我使用的是thispython库,它实现了Aho-Corasick字符串搜索算法,可以在一个过程中在给定字符串中找到一组模式。输出不是我期望的:
In [4]: import ahocorasick
In [5]: import collections
In [6]: tree = ahocorasick.KeywordTree()
In [7]: ss = "this is the first sentence in this book the first sentence is really the most interesting the first sentence is always first"
In [8]: words = ["first sentence is", "first sentence", "the first sentence", "the first sentence is"]
In [9]: for w in words:
...: tree.add(w)
...:
In [10]: tree.make()
In [13]: final = collections.defaultdict(int)
In [15]: for match in tree.findall(ss, allow_overlaps=True):
....: final[ss[match[0]:match[1]]] += 1
....:
In [16]: final
{ 'the first sentence': 3, 'the first sentence is': 2}
我期望的输出是:
^{pr2}$我错过什么了吗?我是在大字符串上这样做的,所以后期处理不是我的第一选择。有没有办法得到想要的输出?在
我理解Aho-Corasick算法的方式和我实现它的方式会让我同意您的预期输出。看起来您正在使用的Python库出错了,或者可能有一个标志,您可以告诉它从某个位置开始提供所有匹配项,而不仅仅是从特定位置开始的最长匹配项。在
原始论文http://www.win.tue.nl/~watson/2R080/opdracht/p333-aho-corasick.pdf中的示例支持您的理解。在
我不知道
ahocorasick
模块,但这些结果似乎令人怀疑。acora模块显示如下:结果:
^{2}$相关问题 更多 >
编程相关推荐