AttributeError: module ‘hanlp.utils.rules‘ has no attribute ‘tokenize_english‘

博主分享了在学习《Python人工智能20小时玩转NLP》时遇到的hanlp英文分词问题,通过找到hanlp库中实际的tokenizer模块解决了`tokenize_english`缺失的问题。教程中推荐的import方式需要调整,正确引入了lang.en.english_tokenizer中的tokenizer函数。

最近在看Python人工智能20个小时玩转NLP自然语言处理【黑马程序员】_哔哩哔哩_bilibili​​​​​​

在p37文本处理的基本方法中提到了hanlp的使用

其中有这么一段代码,其目的是使用hanlp进行英文分词

import hanlp
tokenizer = hanlp.utils.rules.tokenize_english 
tokenizer('Mr. Hankcs bought hankcs.com for 1.5 thousand dollars.')

 正确执行结果应该为:

['Mr.', 'Hankcs', 'bought', 'hankcs.com', 'for', '1.5', 'thousand', 'dollars', '.']

但,我的环境报错AttributeError: module 'hanlp.utils.rules' has no attribute 'tokenize_english'

我看视频评论很多也报这个错误,但网上的解决方案不仅少,而且我试了好多个都没用。主要原因是:hanlp.utils.rules中没有tokenize_english。但视频有条评论提到“from hanlp.utils.english_tokenize import tokenize_english
在https://github.com/hankcs/HanLP/blob/doc-zh/README.md 查看文件结构”虽然试过但也没效果,想着是不是时间太久,hanlp版本(不知道叫版本还是什么!!!!)不一样。而且看连接中的文件结构也找不到相关的tokenize_english,但给了我个启发,直接去hanlp文件里面找,想着会不会是换的位置不一样,果然在C:\ProgramData\Anaconda3\Lib\site-packages\hanlp\utils\lang中发现有个en的文件,其中的english_tokenizer.py里面就有一个tokenize_english。

于是:

from hanlp.utils.lang.en.english_tokenizer import tokenize_english
tokenizer = tokenize_english
tokenizer('Mr. Hankcs bought hankcs.com for 1.5 thousand dollars.')

可行(结果一样,不知道和视频中hanlp.utils.rules.tokenize_english是不是一个东西,应该是!!!)

当然后面直接用这串代码也行,不过想方便一点与视频里面代码同步,可以在hanlp.utils.rules里面加上

import hanlp.utils.lang.en.english_tokenizer

def tokenize_english(text):
    return hanlp.utils.lang.en.english_tokenizer.tokenize_english(text)

 本人python小白,import导入总感觉有问题,有一点乱,请各位不要笑,大家可以改的简短一点(其实我试了好多自认为没问题的导入方式,结果出现莫名其妙的错误,完全没有规律可言,算了,虽然这个不顺眼,但是符合预期结果就行)!!!!

 即可用视频中的代码:

import hanlp
tokenizer = hanlp.utils.rules.tokenize_english 
tokenizer('Mr. Hankcs bought hankcs.com for 1.5 thousand dollars.')
Error in sitecustomize; set PYTHONVERBOSE for traceback: AttributeError: partially initialized module 're' has no attribute 'compile' (most likely due to a circular import) Traceback (most recent call last): File "E:\PyCharm 2025.1\plugins\python-ce\helpers\pydev\pydevd.py", line 16, in <module> from _pydevd_bundle.pydevd_collect_try_except_info import collect_return_info File "E:\PyCharm 2025.1\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_collect_try_except_info.py", line 12, in <module> from _pydevd_bundle.pydevd_constants import IS_PY38_OR_GREATER, IS_PY312_OR_GREATER File "E:\PyCharm 2025.1\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_constants.py", line 5, in <module> import platform File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\platform.py", line 117, in <module> import re File "E:\word\PycharmProjects\PythonProject\view\test\test_day1\re.py", line 3, in <module> import requests File "E:\word\PycharmProjects\PythonProject\.venv\Lib\site-packages\requests\__init__.py", line 43, in <module> import urllib3 File "E:\word\PycharmProjects\PythonProject\.venv\Lib\site-packages\urllib3\__init__.py", line 8, in <module> import logging File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\logging\__init__.py", line 26, in <module> import sys, os, time, io, re, traceback, warnings, weakref, collections.abc File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\traceback.py", line 5, in <module> import linecache File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\linecache.py", line 11, in <module> import tokenize File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\tokenize.py", line 38, in <module> cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) ^^^^^^^^^^ AttributeError: partially initialized module 're' has no attribute 'compile' (most lik
06-07
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值