私はを使用しています。Aho-Corasick文字列検索アルゴリズムを実装したPythonライブラリは、1回のパスで指定された文字列のパターンを検索します。出力は、私が期待していものではありません。文字列検索ライブラリの結果 - バグ、機能、またはコーディングエラー?
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}
私は期待していた出力は、このた:
{
'the first sentence': 3,
'the first sentence is': 2,
'first sentence': 3,
'first sentence is': 2
}
私は何かが足りないのですか?私は大きな文字列でこれをやっているので、後処理は私の最初の選択ではありません。希望の出力を得る方法はありますか?
+1ありがとうございます。これは私の希望する出力と一致します。万が一大きな文章にこれを使用した経験がありますか?パフォーマンスは賢明です。 – Legend
大規模なコーパスを使った直接の経験はありません。 PyPiのページには、GILが解放され、「高速な」CPythonの実装が含まれていますが、それ以外はわかりません。 – Lemur
ああ、[ここ](http://www.quora.com/What-are-the-best)から抜粋して[esmre](http://code.google.com/p/esmre/) -open-source-high-performance-string-matching-libraries)を使用します。 – Lemur