【发布时间】:2013-11-02 20:09:08
【问题描述】:
我正在尝试从文本字符串中删除停用词:
from nltk.corpus import stopwords
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in (stopwords.words('english'))])
我正在处理 600 万个这样的字符串,所以速度很重要。分析我的代码,最慢的部分是上面的行,有没有更好的方法来做到这一点?我正在考虑使用正则表达式re.sub 之类的东西,但我不知道如何为一组单词编写模式。谁能帮帮我,我也很高兴听到其他可能更快的方法。
注意:我尝试过有人建议用 set() 包装 stopwords.words('english'),但这没有任何区别。
谢谢。
【问题讨论】:
-
stopwords.words('english')有多大? -
@SteveBarnes 127 个单词的列表
-
你是把它包在列表理解里面还是外面?尝试添加 stw_set = set(stopwords.words('english')) 并改用这个对象
-
@alko 我以为我把它包在外面没有效果,但我又试了一次,我的代码现在运行速度至少快了 10 倍!!!
-
你是逐行处理文本还是一起处理?
标签: python regex stop-words