AC自动机
1.如何安装
pip install ac_auto
2.该包包含的类
AhoCorasickAutomation
AC自动机,用于多模匹配,在使用前需要先根据词典创建Trie树,构建对象稍微耗时,搜索时间平均性能O(N+M),N为待识别文本的长度,M为所有模式字符串加总长度,但也不建议过长的输入
请注意,AC自动机不能避免分词错误,如“佳保安全”,若“保安”是关键词,也会将其识别出,使用前请确认实际的需求场景
ac_auto_entity = AhoCorasickAutomation(["关键词1", "关键词2"])
ac_auto_entity.search("需要搜索的文本,其中可能包含关键词1")
输出结果:
{'关键词1': [(14, 17)], '关键词2': [(28, 31)]}
也可指定输出格式
ac_auto_entity.search("需要搜索的文本,其中可能包含关键词1", output_mode=AhoCorasickAutomation.OUTPUT_LIST_ONLY_KEY)
输出结果:
['关键词1', '关键词2']
AhoCorasickAutomationConditionalFilter
对AC自动机的匹配结果进行条件过滤,可设置前后一定距离内的文本需要包含或不包含某些关键词的条件
ac_auto_entity = AhoCorasickAutomation(["关键词1", "关键词2"])
text_to_scan = "需要搜索的文本,其中可能包含关键词1,要求附近有条件1"
hits = ac_auto_entity.search(text_to_scan)
ac_filter_entity = AhoCorasickAutomationConditionalFilter({
"关键词1": ["条件1"]
}, distance=10, mode=AhoCorasickAutomationConditionalFilter.FILTER_MODE_WHITE)
hits = ac_filter_entity.filter(text_to_scan, hits)
输出结果:
{'关键词1': [(14, 17)]} # 关键词2由于不符合条件,将被过滤
Metadata
Release files for ac-auto 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ac_auto-0.1.2.tar.gz | 5.3 kB | Without provenance | Details for ac_auto-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ac_auto-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for ac_auto-0.1.2-py3-none-any.whl |
Total release size: 11.1 kB
Release files / ac_auto-0.1.2.tar.gz
| Download URL | ac_auto-0.1.2.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
34151ea1053929f193df343f7458e2d8e8eabaee1104e85cfde604ce326a0be8
|
|
BLAKE2b-256 checksum How to use checksums |
98fbc6ad7305da5c493b0ede5caf9166e7b07847cb87bcaf30b790a0f9332336
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.8.5
|
Release files / ac_auto-0.1.2-py3-none-any.whl
| Download URL | ac_auto-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a4b486b7e3a63670c34cdd84238cd4b74efc2fb1fd9ddfdc2e45ba9765581d25
|
|
BLAKE2b-256 checksum How to use checksums |
0f8b9ffe9e8cc671fa46365d316619e2c7ab2943118f755b790927bf0cfef2cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.8.5
|
Security for ac-auto 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.