Skip to main content

this project is a aho-corasick automaton implementation by python

Project description

ahocorasick-python

ac自动机python的实现,可用于python2 python3等主流python发行版,对标准的ac自动机算法进行了完善 优化(主要是改进了结果的准确性)。
注意:为了保证结果的准确性,请安装使用最新版(0.0.7)。

1.如何安装

pip 安装(推荐)

pip install  ahocorasick-python

源码安装

git clone  https://github.com/xizhicode/ahocorasick-python.git
cd ahocorasick-python && python setup.py install

2.如何使用

注: 此处python3为例,python2也是类似的结果

简单检索

import  ahocorasick     # 导入包
tree = ahocorasick.AhoCorasick("test","book","oo","ok", "k") # 构建ac自动机
print(tree.search("test book")) # 检索

输出结果:

{'test', 'k', 'oo', 'book', 'ok'}

检索并返回结果字符所在的位置(可以用于字符替换等场景)

import  ahocorasick     # 导入包
tree = ahocorasick.AhoCorasick("test","book","oo","ok", "k") # 构建ac自动机
print(tree.search("test book",True)) # 检索

输出结果:

{('k', (8, 9)), ('book', (5, 9)), ('oo', (6, 8)), ('ok', (7, 9)), ('test', (0, 4))}

3.参考资料

4.联系我

QQ: 943489924
邮箱:zhoukunpeng504@163.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ahocorasick-python, version 0.0.8
Filename, size File type Python version Upload date Hashes
Filename, size ahocorasick-python-0.0.8.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page