我正在尝试从一段文本中移除停用词:
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')
有多大? - Steve Barnes