高性能のトライ&キーワード一致&置換ツール。
これは cython によって実装されており、cpp にコンパイルされます。トライ データ構造は、最適化された二重配列トライである Ceder です。 Python2.7 および 3.4 以降をサポートします。ダンプとロードへのピクルスをサポートします。
これが役に立ったと思われた場合は、星を付けてください。
このモジュールは cython で書かれています。 cython をインストールする必要があります。
pip install cyac
次にトライを作成します
>>> from cyac import Trie
>>> trie = Trie()
キーワードの追加/取得/削除
>>> trie.insert(u"哈哈") # return keyword id in trie, return -1 if doesn't exist
>>> trie.get(u"哈哈") # return keyword id in trie, return -1 if doesn't exist
>>> trie.remove(u"呵呵") # return keyword in trie
>>> trie[id] # return the word corresponding to the id
>>> trie[u"呵呵"] # similar to get but it will raise exeption if doesn't exist
>>> u"呵呵" in trie # test if the keyword is in trie
すべてのキーワードを取得する
>>> for key, id_ in trie.items():
>>> print(key, id_)
プレフィックス/予測
>>> # return the string in the trie which starts with given string
>>> for id_ in trie.predict(u"呵呵"):
>>> print(id_)
>>> # return the prefix of given string which is in the trie.
>>> for id_, len_ in trie.prefix(u"呵呵"):
>>> print(id_, len_)
抽出して置き換えてみる
>>> python_id = trie.insert(u"python")
>>> trie.replace_longest("python", {python_id: u"hahah"}, set([ord(" ")])) # the second parameter is seperator. If you specify seperators. it only matches strings tween seperators. e.g. It won't match 'apython'
>>> for id_, start, end in trie.match_longest(u"python", set([ord(" ")])):
>>> print(id_, start, end)
アホコラシックエキス
>>> ac = AC.build([u"python", u"ruby"])
>>> for id, start, end in ac.match(u"python ruby"):
>>> print(id, start, end)
ファイルにエクスポートし、mmap を使用してファイルをロードし、プロセス間でデータを共有できます。
>>> ac = AC.build([u"python", u"ruby"])
>>> ac.save("filename")
>>> ac.to_buff(buff_object)
Python バッファからの初期化
>>> import mmap
>>> with open("filename", "r+b") as bf:
buff_object = mmap.mmap(bf.fileno(), 0)
>>> AC.from_buff(buff_object, copy=True) # it allocs new memory
>>> AC.from_buff(buff_object, copy=False) # it shares memory
マルチプロセスの例
import mmap
from multiprocessing import Process
from cyac import AC
def get_mmap():
with open("random_data", "r+b") as bf:
buff_object = mmap.mmap(bf.fileno(), 0)
ac_trie = AC.from_buff(buff_object, copy=False)
# Do your aho searches here. "match" function is process safe.
processes_list = list()
for x in range(0, 6):
p = Process(
target=get_mmap,
)
p.start()
processes_list.append(p)
for p in processes_list:
p.join()
cyac でのマルチプロセッシングとメモリ分析の詳細については、この問題を参照してください。
AC オートマトンの関数「match」は、スレッド/プロセスに対して安全です。共有 AC オートマトンと並行して一致を見つけることは可能ですが、それにパターンを書き込んだり追加したりすることはできません。
Ubuntu 14.04.5/Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz 上。
HatTrieと比較すると、水平軸はトークン番号です。縦軸は使用時間(秒)です。
flashTextとの比較。このタスクでは正規表現が遅すぎます (flashText のベンチマークを参照)。水平軸は一致する文字番号です。縦軸は使用時間(秒)です。
ピャホコラシックと比べると、ホライゾン軸がキャラ数で勝負です。縦軸は使用時間(秒)です。
>>> len(char.lower()) == len(char) # this is always true in python2, but not in python3
>>> len(u"İstanbul") != len(u"İstanbul".lower()) # in python3
大文字と小文字を区別しないマッチングの場合、このライブラリはその事実を処理し、正しいオフセットを返します。
python setup.py build
PYTHONPATH= $( pwd ) /build/BUILD_DST python3 tests/test_all.py
PYTHONPATH= $( pwd ) /build/BUILD_DST python3 bench/bench_ * .py