无法实现nltk.stopwords



我试图用nltk删除数据中的停止词,但经过几次尝试后,我无法删除停止词。我的代码的标记化部分工作,但我无法理解为什么停止词不起作用。

def pre_process(text):

# remove special characters and digits
text=re.sub("(\d|\W|_)+"," ",text)
text=re.split("W+",text)

return text
text = dat['text'].apply(lambda x:pre_process(x))
nltk.download('stopwords')
def remove_stopwords(text):
for word in text:
if word in stopwords.words('english'):
text.remove(word)
return text
text_stopword = text.apply(lambda x:remove_stopwords(x))

代码应该删除诸如' The '之类的单词,但是在代码中运行我的csv之后,诸如' The '之类的单词仍然存在。

当前结果:

text返回:

[tv, future, in, the, hands, of, viewers, with...

text_stopword返回:

[tv, future, in, the, hands, of, viewers, with...

您在remove_stopwords函数中的返回语句被错误缩进。由于该函数在第一次迭代后返回文本。

请使用:

def remove_stopwords(text):
for word in text:
if word in stopwords.words('english'):
text.remove(word)
return text

最新更新