用空格添加/删除停用词的最佳方法是什么?我正在使用token.is_stop
函数,并希望对该集合进行一些自定义更改。我正在查看文档,但找不到有关停用词的任何内容。谢谢!
使用Spacy 2.0.11,您可以使用以下任一方法更新其停用词集:
添加单个停用词:
>>> 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
一次添加多个停用词:
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")
处有更详细的描述)。
对于2.0版,我使用了此:
https://spacy.io/usage/saving-loading
这会将所有停用词加载到集合中。
您可以将停用词修改为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
或首先使用您自己的列表。
对于2.0,请使用以下代码:
STOP_WORDS
以下最新版本会将单词从列表中删除:
for word in nlp.Defaults.stop_words:
lex = nlp.vocab[word]
lex.is_stop = True
这也收集停用词:)
spacy_stopwords = spacy.lang.en.stop_words.STOP_WORDS
spacy_stopwords.remove('not')