对于2.0,请使用以下命令:
for word in nlp.Defaults.stop_words:
lex = nlp.vocab[word]
lex.is_stop = True
0
对于2.0,请使用以下命令:
for word in nlp.Defaults.stop_words:
lex = nlp.vocab[word]
lex.is_stop = True
0
您可以在像这样处理文本之前对其进行编辑(请参阅此文章 ):
>>> 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下有效。对于较新的版本,请参阅其他答案。
0
对于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
也可以首先使用自己的列表。
0
使用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中有进一步描述)。
0
使用spacy添加/删除停用词的最佳方法是什么?我正在使用
token.is_stop
函数,并希望对该集合进行一些自定义更改。我正在查看文档,但找不到有关停用词的任何内容。谢谢!