Add/remove 带有 spacy 的自定义停用词

Add/remove custom stop words with spacy

add/remove spacy 停用词的最佳方法是什么?我正在使用 token.is_stop 函数并想对集合进行一些自定义更改。我正在查看文档,但找不到有关停用词的任何信息。谢谢!

您可以像这样在处理文本之前编辑它们(参见 this post):

>>> import spacy
>>> nlp = spacy.load("en")
>>> nlp.vocab["the"].is_stop = False
>>> nlp.vocab["definitelynotastopword"].is_stop = True
>>> sentence = nlp("the word is definitelynotastopword")
>>> sentence[0].is_stop
False
>>> sentence[3].is_stop
True

注意:这似乎工作 <=v1.8。对于较新的版本,请参阅其他答案。

对于 2.0 版,我使用了这个:

from spacy.lang.en.stop_words import STOP_WORDS

print(STOP_WORDS) # <- set of Spacy's default stop words

STOP_WORDS.add("your_additional_stop_word_here")

for word in STOP_WORDS:
    lexeme = nlp.vocab[word]
    lexeme.is_stop = True

这会将所有停用词加载到一个集合中。

您可以将停用词修改为 STOP_WORDS 或首先使用您自己的列表。

对于 2.0 使用以下内容:

for word in nlp.Defaults.stop_words:
    lex = nlp.vocab[word]
    lex.is_stop = True

使用 Spacy 2.0.11,您可以使用以下方法之一更新其停用词集:

要添加一个停用词:

import spacy    
nlp = spacy.load("en")
nlp.Defaults.stop_words.add("my_new_stopword")

一次添加多个停用词:

import spacy    
nlp = spacy.load("en")
nlp.Defaults.stop_words |= {"my_new_stopword1","my_new_stopword2",}

删除单个停用词:

import spacy    
nlp = spacy.load("en")
nlp.Defaults.stop_words.remove("whatever")

一次删除多个停用词:

import spacy    
nlp = spacy.load("en")
nlp.Defaults.stop_words -= {"whatever", "whenever"}

注意:要查看当前的停用词集,请使用:

print(nlp.Defaults.stop_words)

更新:评论中指出此修复仅影响当前执行。要更新模型,您可以使用方法 nlp.to_disk("/path")nlp.from_disk("/path")(在 https://spacy.io/usage/saving-loading 中有进一步描述)。

这也收集停用词:)

spacy_stopwords = spacy.lang.en.stop_words.STOP_WORDS

在最新版本中,以下将从列表中删除单词:

spacy_stopwords = spacy.lang.en.stop_words.STOP_WORDS
spacy_stopwords.remove('not')

对于版本 2.3.0 如果您想替换整个列表而不是添加或删除几个停用词,您可以这样做:

custom_stop_words = set(['the','and','a'])

# First override the stop words set for the language
cls = spacy.util.get_lang_class('en')
cls.Defaults.stop_words = custom_stop_words

# Now load your model
nlp = spacy.load('en_core_web_md')

诀窍是在加载模型之前为语言分配停用词集。它还确保停用词的任何 upper/lower 大小写变体都被视为停用词。