AC自动机(Aho-Corasick Automaton Algorithm)


//
AC自动机(Aho-Corasick Automaton Algorithm)

(1)建trie树



(2)广度优先搜索构建所有结点的fail
构造next指针,概括起来是:假如当前的节点的字符为e,沿着它的父亲的next指针走,直到找到一个结点,它的孩子也有e这个字符,然后将当前结点的next指针指向那个结点
有e这个孩子的孩子e。
从root开始广度优先搜索每个点,如果当前搜的点的孩子,与该点的next指针指向的点有相同的孩子,则当前点的孩子指向next指针的孩子,
否则next指针循环搜回去,如果搜不到就指向root。


ac_自动机

(3)查找
当时的文本字符串为text,长度为len
则有:
ptr=root;
for i:=1 to len-1 do
    while(ptr->text[i]==NULL&&ptr!=root)
         ptr=ptr->next;
    ptr=ptr->child[i];
   if(ptr==NULL) then ptr=root;//如果搜不到回到根结点
HDOJ2002
Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8434    Accepted Submission(s): 2828

Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 

 

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 

 

Output
Print how many keywords are contained in the description.
 

 

Sample Input
1 5 she he say shr her yasherhs
 

 

Sample Output
3





你遇到的错误: ``` ⛔ 启动失败:⚠️ 初始化问答系统失败:❌ 构建 Aho-Corasick 自动机失败:module 'ahocorasick' has no attribute 'Automaton' ``` --- ## 🔍 错误原因分析 这个错误的核心是: > `module 'ahocorasick' has no attribute 'Automaton'` 说明你在代码中使用了: ```python import ahocorasick ahocorasick.Automaton() ``` 但你的环境中安装的 `ahocorasick` 模块 **不是 PyPI 上官方的那个 `pyahocorasick`**,或者版本不兼容! ### ✅ 根本原因: 你很可能安装的是名字叫 `ahocorasick` 的错误包(非主流),而正确的包应该是 **`pyahocorasick`** —— 这是一个常见的命名混淆问题。 | 包名 | 是否正确 | 说明 | |------|----------|------| | ❌ `ahocorasick` | 否 | 非官方、功能不全、API 不同 | | ✅ `pyahocorasick` | 是 | 官方推荐,支持 `Automaton` 类 | --- ## ✅ 解决方案(4 步彻底修复) ### ✅ 第一步:卸载错误的包 打开终端(PowerShell / CMD / VSCode Terminal),运行: ```bash pip uninstall ahocorasick -y ``` > ⚠️ 注意:即使你觉得之前装的是“正确的”,也请先卸干净。 --- ### ✅ 第二步:安装正确的包 ```bash pip install pyahocorasick ``` ✅ 正确的项目地址是:[https://pypi.org/project/pyahocorasick/](https://pypi.org/project/pyahocorasick/) 它支持: - `ahocorasick.Automaton()` - `add_word(word, (index, value))` - `make_automaton()` - `iter(text)` 等高效多模式匹配方法 --- ### ✅ 第三步:验证是否修复成功 创建一个测试文件 `test_aho.py`: ```python import ahocorasick try: automaton = ahocorasick.Automaton() automaton.add_word("test", "matched") automaton.make_automaton() for item in automaton.iter("this is a test"): print(item) print("✅ 测试通过!pyahocorasick 安装成功。") except Exception as e: print(f"❌ 仍然出错:{e}") ``` 运行它: ```bash python test_aho.py ``` 如果输出包含 `matched` 和坐标信息,则表示安装成功。 --- ### ✅ 第四步:确认 `question_classifier.py` 中的构造方式正确 确保你的 `build_actree` 方法如下: ```python def build_actree(self, wordlist): actree = ahocorasick.Automaton() for index, word in enumerate(wordlist): actree.add_word(word, (index, word)) actree.make_automaton() return actree ``` 📌 这才是 `pyahocorasick` 的标准用法。 --- ## 🛠️ 补充建议(防止再错) ### 1. 查看当前安装的包版本: ```bash pip show ahocorasick # 或 pip show pyahocorasick ``` ✅ 正常输出应类似: ``` Name: pyahocorasick Version: 2.0.9 Summary: A fast implementation of the Aho-Corasick string matching algorithm Home-page: https://github.com/WojciechMula/pyahocorasick ``` 如果是 `ahocorasick` 且没有主页或版本很低 → 就是错的! --- ### 2. 在 requirements.txt 中固定依赖(可选) 新建 `requirements.txt`: ```txt pyahocorasick==2.0.9 py2neo==2021.2.3 pymongo==4.7 lxml==4.9.3 ``` 然后一键安装: ```bash pip install -r requirements.txt ``` --- ## ✅ 总结 你要做的就是: ```bash # 1. 卸载错误包 pip uninstall ahocorasick -y # 2. 安装正确包 pip install pyahocorasick # 3. 确认 question_classifier.py 使用 Automaton API # 4. 再次运行 chatbot_graph.py python chatbot_graph.py ``` 🔁 完成后,`AttributeError: module 'ahocorasick' has no attribute 'Automaton'` 将彻底消失! 如有其他报错,请贴出完整日志,我可以继续帮你诊断。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值