当前位置: 首页 > news >正文

20250324-使用 `nltk` 的 `sent_tokenize`, `word_tokenize、WordNetLemmatizer` 方法时报错

解决使用 nltksent_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.html

  Attempted to load corpora/wordnet

  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) 运行命令(不是每个人都能成功

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', '.']

相关文章:

  • 前端 Babel 入门简介
  • 系统思考与心智模式
  • 计算机网络--第四章 网络层(1)
  • 基于SpringBoot的“社区居民诊疗健康管理系统”的设计与实现(源码+数据库+文档+PPT)
  • 聊聊langchain4j的Code Execution Engine
  • 基于Java的科研成果统计系统的设计及实现(源码+lw+部署文档+讲解),源码可白嫖!
  • CANopen基本理论
  • 2025年- G27-Lc101-542. 01 黑客帝国--java版
  • 2024年认证杯SPSSPRO杯数学建模B题(第二阶段)神经外科手术的定位与导航全过程文档及程序
  • 欧拉角与法向量之间的相互转换(附代码)
  • Cursor+Claude-3.5生成Android app
  • HuggingFace Transformers
  • 【算法笔记】图论基础(二):最短路、判环、二分图
  • Python-金融相关代码讲解
  • 详解Redis 核心特性与基础
  • 【C】高效的 GPIO 读取编码方式
  • 深入理解智能家居领域中RS485、Modbus、KNX 和 Zigbee协议概念
  • AI编程工具
  • 从切图仔到鸿蒙开发01-文本样式
  • 如何查看Unity打包生成的ab文件
  • 11家券商一季度净利翻番:9家利润超20亿,国泰海通居首
  • 金砖国家外长会晤主席声明(摘要)
  • 山西太原一居民小区发生爆炸,应急管理部派工作组赴现场
  • 城市更新·简报│中央财政支持城市更新,倾斜超大特大城市
  • 原国家有色金属工业局副局长黄春萼逝世,享年86岁
  • 蔡澜回应“入ICU观察”称未至于病危,助理:只是老毛病