gpt4 book ai didi

python - 使用 spacy 添加/删除自定义停用词

转载 作者:IT老高 更新时间:2023-10-28 21:57:15 30 4
gpt4 key购买 nike

使用 spacy 添加/删除停用词的最佳方法是什么?我正在使用 token.is_stop功能,并希望对集合进行一些自定义更改。我正在查看文档,但找不到任何关于停用词的信息。谢谢!

最佳答案

使用 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 中有详细说明) )。

关于python - 使用 spacy 添加/删除自定义停用词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41170726/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com