【问题标题】:Removing stopwords from a textblob从 textblob 中删除停用词
【发布时间】:2017-10-26 16:33:58
【问题描述】:

我正在处理一个文本块,其中一个步骤是删除停用词。 Textblob 是不可变的,因此我将其转换为列表来完成这项工作:

blob = tb(tekst)
lista = [word for word in blob.words if word not in stopwords.words('english')]
tekst = ' '.join(lista)
blob = tb(tekst)

对于这个问题有更简单/更优雅的解决方案吗?

【问题讨论】:

  • 查看 nltk...类似的问题,已经回答...stackoverflow.com/questions/5486337/…
  • 你的代码看起来非常好,因为删除停止词。这是一种标准的方式。
  • 是的,与 utengr 所说的不同,实际上没有更有效的方法来做到这一点。无论如何,您将不得不查看每个单词。您可以做的唯一一件事是使其更有效,而不是实际构建列表并使用生成器。只需将您的 []s 更改为 (),如:(word for word in blob.words if word not in stopwords.words('english'))。使用该列表后,您将永远无法再次访问该列表,但无论如何您都会立即加入。
  • 谢谢大家! Christian Will:有道理,但我想避免使用 nltk。尼克查普曼:听起来不错——这确实是一份单份清单。如果它是一种 CPU 效率更高的解决方案,那么它就是我正在寻找的解决方案。

标签: python textblob


【解决方案1】:

你可以试试这个代码:

from textblob import TextBlob
from nltk.corpus import stopwords

b="Do not purchase these earphones. It will automatically disconnect and reconnect. Worst product to buy."
text=TextBlob(b)

# Tokens
tokens=set(text.words)
print("Tokens: ",tokens)
# stopwords
stop=set(stopwords.words("english"))

# Removing stop words using set difference operation
print("Filtered Tokens: ",tokens-stop)

输出: *代币: {'buy'、'disconnect'、'will'、'to'、'purchase'、'reconnect'、'product'、'It'、'Do'、'and' , '最差', '耳机', '不是', '自动', '这些'}

过滤的令牌: {'buy', 'disconnect', 'purchase', 'reconnect', 'product', 'It', 'Do', 'Worst', 'earphones', '自动'}*

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2018-09-28
    • 2018-01-15
    • 2015-01-20
    • 2021-02-02
    • 2020-09-07
    • 2021-03-21
    相关资源
    最近更新 更多