【发布时间】:2021-09-20 06:40:46
【问题描述】:
我在从标记化中删除停用词时遇到问题。我已经对句子进行了标记,并将结果与 pandas 一起插入名为“tweets_tokenize”的列中。问题是我有双括号( [ ] ),结果只有一个并且重复(详情参见图片),如果使用第一个函数,停用词不起作用。但是,如果使用第二个功能是好的。能解释一下原因吗?
from nltk.corpus import stopwords
stopwords_indonesia = stopwords.words('indonesian')
# First function
def stopwords_remover(words):
words = df['tweets_tokenize']
tweets_stopwords = []
for word in words:
if word not in stopwords_indonesia:
tweets_stopwords.append(word)
return tweets_stopwords
# Second function
def stopwords_remover(words):
tweets_stopwords = []
for word in words:
if word not in stopwords_indonesia:
tweets_stopwords.append(word)
return tweets_stopwords
df['tweets_tokenize'].apply(stopwords_remover)
df.head()
使用第一个函数的结果。
使用第二个函数的结果。
【问题讨论】:
-
这是完整的代码吗?您是否在代码中的某处将变量 tweets_stopwords 声明为全局变量?
-
对不起,我给错了代码。
-
看到更正了,请找我的答案