python - 从停用词中清除列表
问题描述
这个变量:
sent=[('include', 'details', 'about', 'your performance'),
('show', 'the', 'results,', 'which', 'you\'ve', 'got')]
需要清除停用词。我试过了
output = [w for w in sent if not w in stop_words]
但它没有奏效。有什么问题?
解决方案
from nltk.corpus import stopwords
stop_words = {w.lower() for w in stopwords.words('english')}
sent = [('include', 'details', 'about', 'your', 'performance'),
('show', 'the', 'results,', 'which', 'you\'ve', 'got')]
如果您想创建一个没有停用词的单词列表;
>>> no_stop_words = [word for sentence in sent for word in sentence if word not in stop_words]
['include', 'details', 'performance', 'show', 'results,', 'got']
如果你想保持句子完整;
>>> sent_no_stop = [[word for word in sentence if word not in stop_words] for sentence in sent]
[['include', 'details', 'performance'], ['show', 'results,', 'got']]
但是,大多数时候您会使用单词列表(不带括号);
sent = ['include', 'details', 'about', 'your performance','show', 'the', 'results,', 'which', 'you\'ve', 'got']
>>> no_stopwords = [word for word in sent if word not in stop_words]
['include', 'details', 'performance', 'show', 'results,', 'got']
推荐阅读
- python - 使用 BeautifulSoup 在 xml 解析中删除包含特定子标签的标签
- php - 嵌套单选按钮
- xamarin.ios - Amazon Cognito 身份错误:“无法存储密钥”Xamarin.iOS
- vagrant - vagrant smb 共享文件中的文件内容混乱
- php - 将 WordPress 个人资料 Gravatar 更改为生成的 robohash
- c# - if 语句有多个条件,而不是使用 AND
- regex - Perl:如何将保留最后一句的段落拆分为另一个数组?
- json - 无法从 WorldCoin Index API 仅获取价格
- android - 从 Firebase 检索数据到谷歌地图
- pandas - 如何计算熊猫数据框中的时间增量?