解决使用 nltk
的 sent_tokenize
, word_tokenize、WordNetLemmatizer
方法时报错问题
第 2 节的手动方法的法1可解决大部分问题,可首先尝试章节 2 的方法
1. nltk.download(‘punkt_tab’)
LookupError:
**********************************************************************Resource punkt_tab not found.Please use the NLTK Downloader to obtain the resource:>>> import nltk>>> nltk.download('punkt_tab')For more information see: https://www.nltk.org/data.html Attempted to load tokenizers/punkt_tab/english/Searched in:- 'C:\\Users\\chenw/nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\share\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\lib\\nltk_data'- 'C:\\Users\\chenw\\AppData\\Roaming\\nltk_data'- 'C:\\nltk_data'- 'D:\\nltk_data'- 'E:\\nltk_data'
**********************************************************************
根据提示可手动或自动处理这一报错
1) 运行命令(不是每个人都能成功)
将 your_path
换为你想安装的文件地址(必须是上面所显示的任一路径)
import nltk
nltk.download('punkt_tab', download_dir=your_path)
可以使用一下方法检查 NLTK 数据路径
import nltk
print(nltk.data.find('tokenizers/punkt_tab'))
2) 手动
首先,从官网下载文件
NLTK Corpora: https://www.nltk.org/nltk_data/
在官网页面搜索 punkt_tab
,在第 77 条:Punkt Tokenizer Models [ download | source ]
点击 download
下载。
之后,(我使用的是提示的第四条路径)在上面提示路径中的任意一个路径中,在 ..../lib
文件夹下新建 nltk_data/tokenizers
文件夹。
然后,将下载的文件解压到 tokenizers
文件夹下:
最后,试运行一下是不会报错的(只要没有哪一步出错)
2. nltk.download(‘wordnet’)
WordNetLemmatizer().lemmatize()
LookupError:
**********************************************************************Resource wordnet not found.Please use the NLTK Downloader to obtain the resource:>>> import nltk>>> nltk.download('wordnet')For more information see: https://www.nltk.org/data.htmlAttempted to load corpora/wordnetSearched in:- 'C:\\Users\\chenw/nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\share\\nltk_data'- 'D:\\RuanJianAPP\\anaconda3\\envs\\NLP-30820\\lib\\nltk_data'- 'C:\\Users\\chenw\\AppData\\Roaming\\nltk_data'- 'C:\\nltk_data'- 'D:\\nltk_data'- 'E:\\nltk_data'
**********************************************************************
同样也是分手动和自动,按照上述的方法
1) 运行命令(不是每个人都能成功)
与 punkt_tab
的处理方法类似
import nltk
nltk.download('wordnet', download_dir=your_path)
2) 手动
法1:直接从下面的链接中下载语料库(推荐)
nltk_data: https://github.com/nltk/nltk_data
将下载好的包解压,解压后包中 packages 中的所以文件复制到 nltk_data
文件夹中
tokenizers\punkt_tab.zip
文件需要解压,之后无关文件可删除
可参考文章:nltk.download(‘wordnet‘)错误;Resource wordnet not found. Please use the NLTK Downloader to obtain th
法2:与 1. nltk.download('punkt_tab')
的手动操作方法相同
NLTK Corpora 的 114 个压缩包(我没试过,文件太大,下载太慢)
3. 验证数据包是否下载成功
from nltk.tokenize import word_tokenize
text = "This are some sample sentences to test the tokenizer."
tokens = word_tokenize(text)
print(tokens)
['This', 'are', 'some', 'sample', 'sentences', 'to', 'test', 'the', 'tokenizer', '.']
from nltk import WordNetLemmatizer
lemmatizer = WordNetLemmatizer() # 词形还原器
wordlist = [] # 存储词形还原后单词的列表# 对每个单词进行词形还原
for word in tokens:# 这里默认词性为名词("n"),可以根据需要扩展到其他词性# 例如:使用词性标注工具(如 nltk.pos_tag)来确定单词的词性lemma_word = lemmatizer.lemmatize(word, pos="n")wordlist.append(lemma_word)print(wordlist)
['This', 'are', 'some', 'sample', 'sentence', 'to', 'test', 'the', 'tokenizer', '.']