python - 名称错误 : name 'stopwords' is not defined

标签 python nlp stop-words

出于某种原因,我收到错误 NameError: name 'stopwords' is not defined,即使我已经安装了软件包。我正在尝试对一些反馈评论进行自然语言处理。 dataset 对象是一个包含两列的表,Reviews(反馈的一句话) 和目标变量 Liked(1 或 0)。帮助赞赏,谢谢!


block 1

import re
import nltk
nltk.download('stopwords')

输出 1

   > [nltk_data] Downloading package stopwords to

   > [nltk_data]     /Users/user/nltk_data...

   > [nltk_data]   Package stopwords is already up-to-date!

   > Out[14]: True

区 block 2

dataset['Review'][0]
review = re.sub('[^a-zA-Z]',' ' ,dataset['Review'][0])
review = review.lower()
review = review.split()
review = [word for word in review if not word in stopwords.words('english')] **ERROR ON THIS LINE**

输出 2

>NameError                                 Traceback (most recent call last)
<ipython-input-16-8d0ee1fd7c7f> in <module>()
      3 review = review.lower()
      4 review = review.split()
----> 5 review = [word for word in review if not word in stopwords.words('english')]

><ipython-input-16-8d0ee1fd7c7f> in <listcomp>(.0)
      3 review = review.lower()
      4 review = review.split()
----> 5 review = [word for word in review if not word in stopwords.words('english')]

>NameError: name 'stopwords' is not defined

最佳答案

您只需在代码中使用stopwords 之前添加以下行:

from nltk.corpus import stopwords

关于python - 名称错误 : name 'stopwords' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47485687/

相关文章:

python - 在 python 中清除 sys.argv

python - 使用 train_test_split 拆分数据时与之后加载 csv 文件时的精度不同

pandas - 如何在某个描述上提取多个关键字

python - 将未标记语料库转换为标记语料库 (NLTK)

scikit-learn - CountVectorizer 上的词形还原不会删除停用词

python - sdl2 - 导入错误 : DLL load failed: The specified module could not be found and [CRITICAL] [App] Unable to get a Window, 中止

python - 未找到 Discord debian 模块 python3.6 和 python2.7 安装

python - 在 Anaconda/NLTK 中找不到 Genia Tagger 文件错误

java - 获得最少数量的子词

Java Arraylist 按索引删除多个元素