繁体   English   中英

如何删除英语和西班牙语停用词

[英]How to remove English and Spanish stop words

我正在尝试删除英语和西班牙语的停用词。 我的代码适用于英语,但不适用于西班牙语:

stopword = nltk.corpus.stopwords.words('english', 'spanish')

def remove_stopwords(text):
    text = [word for word in text if word not in stopword]
    return text
    
df['Tweet_nonstop'] = df['Tweet_tokenized'].apply(lambda x: remove_stopwords(x))

有人可以帮助解决这个问题吗? 谢谢

要获取英语和西班牙语停用词,您可以使用以下命令:

stopword_en = nltk.corpus.stopwords.words('english')
stopword_es = nltk.corpus.stopwords.words('spanish')
stopword = stopword_en + stopword_es

nltk.corpus.stopwords.words的第二个参数,来自帮助,不是另一种语言:

>>> help(nltk.corpus.stopwords.words)
Help on method words in module nltk.corpus.reader.wordlist:

words(fileids=None, ignore_lines_startswith='\n') method of nltk.corpus.reader.wordlist.WordListCorpusReader instance

第一个参数fileids可以采用多个值,因此,诸如nltk.corpus.stopwords.words(fileids=('english', 'spanish'))类的调用也可以按预期工作。

除了上面的答案,尝试

stopwords.words(['english','spanish'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM