【问题标题】:Remove stopwords with nltk.corpus from list with lists从带有列表的列表中删除带有 nltk.corpus 的停用词
【发布时间】:2020-05-28 01:35:57
【问题描述】:

我有一个列表,其中包含评论的所有分隔词,如下所示:

texts = [['fine','for','a','night'],['it','was','good']]

我想使用 nltk.corpus 包删除所有停用词,并将所有没有停用词的单词放回列表中。最终结果应该是一个列表,由不带停用词的单词列表组成。这是我试过的:

import nltk
nltk.download() # to download stopwords corpus
from nltk.corpus import stopwords
stopwords=stopwords.words('english')
words_reviews=[]

for review in texts:
    wr=[]
    for word in review:
        if word not in stopwords:
            wr.append(word)
        words_reviews.append(wr)

这段代码确实有效,但现在我收到错误:AttributeError: 'list' object has no attribute 'words',指的是停用词。我确保我安装了所有软件包。可能是什么问题?

【问题讨论】:

标签: nltk stop-words


【解决方案1】:

问题是您在代码中重新定义了stopwords

from nltk.corpus import stopwords
stopwords=stopwords.words('english')

在第一行之后,stopwords 是一个带有words() 方法的语料库阅读器。第二行之后是一个列表。相应地进行。

实际上在列表中查找内容确实很慢,因此如果使用此功能,您将获得更好的性能:

stopwords = set(stopwords.words('english'))

【讨论】:

    【解决方案2】:

    而不是

    [word for word in text_tokens if not word in stopwords.words()]

    使用

    [word for word in text_tokens if not word in all_stopwords]
    

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 2023-03-29
      • 2023-03-24
      • 2021-02-02
      • 2022-01-20
      • 2021-12-22
      相关资源
      最近更新 更多