python-3.x - 删除更改的停用词
问题描述
背景:
1)我有以下代码要stopwords
使用 nltk 包删除:
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
your_string = "The dog does not bark at the tree when it sees a squirrel"
tokens = word_tokenize(your_string)
lower_tokens = [t.lower() for t in tokens]
filtered_words = [word for word in lower_tokens if word not in stopwords.words('english')]
2)此代码可用于删除此处stopwords
所示the
的内容:
['dog', 'barks', 'tree', 'sees', 'squirrel']
3)我用下面的代码改变了stopwords
这个词:not
to_remove = ['not']
new_stopwords = set(stopwords.words('english')).difference(to_remove)
问题:
4)但是当我使用new_stopwords
以下代码时:
your_string = "The dog does not bark at the tree when it sees a squirrel"
tokens = word_tokenize(your_string)
lower_tokens = [t.lower() for t in tokens]
filtered_words = [word for word in lower_tokens if word not in new_stopwords.words('english')]
5)我收到以下错误,因为new_stopwords
是set
:
AttributeError: 'set' object has no attribute 'words'
问题:
6)如何使用新定义new_stopwords
的来获得所需的输出:
['dog', 'not','barks', 'tree', 'sees', 'squirrel']
解决方案
您非常接近,但是您对错误消息的阅读是错误的:问题不在于“new_stopwords
是set
”,正如您所说,而是“set
没有属性words
”
哪个,它没有。new_stopwords
是一个集合,这意味着您可以直接在列表推导中使用它:
filtered_words = [word for word in lower_tokens if word not in new_stopwords]
您还可以省去修改停用词列表的麻烦,只需使用两个条件:
keep_list = ['not']
filtered_words = [word for word in lower_tokens if (word not in stopwords.words("english")) or (word in keep_list)]
推荐阅读
- firebase - 如何显示所有用户的倒计时时间
- javascript - NodeJS knex Select Innerjoin 多个字段
- python - /admin/ 的 Wagtail KeyError 4
- javascript - 调用 ngrenderer.selectRootElement 时,角度选择元素会丢失数据
- ios - XCode 10.1 中的解析问题 - “预期类型”出现在“BraintreeDropIn”窗格中
- python - 上下文中的多个答案跨度,BERT 问答
- flutter - 飞镖中的多个随机数
- r - R circlize chordDiagram 如何提高图像质量
- ios - 标题视图模型
- javascript - typeorm find with and & or where block