gpt4 book ai didi

nlp - Spacy - 自定义停用词不起作用

转载 作者:行者123 更新时间:2023-12-05 07:29:31 25 4
gpt4 key购买 nike

我正在尝试将自定义 STOP_WORDS 添加到 spacy。以下代码应将自定义 STOP_WORD“Bestellung”添加到标准的 STOP_WORDS 集中。我遇到的问题是,添加有效,即添加后该集合包含“Bestellung”,但在使用 .is_stop 测试自定义停用词“Bestellung”时,python 返回 FALSE。

另一个带有默认 STOP_WORD(即它是 STOP_WORDS 中的标准)“darunter”的测试返回 TRUE。我不明白,因为“Bestellung”和“darunter”这两个词都在同一组 STOP_WORDS 中。

有谁知道它为什么会这样?

谢谢

import spacy
from spacy.lang.de.stop_words import STOP_WORDS

STOP_WORDS.add("Bestellung")
print(STOP_WORDS) #Printing STOP_WORDS proofs, that "Bestellung" is part of the Set "STOP_WORDS". Both tested words "darunter" and "Bestellung" are part of it.
nlp=spacy.load("de_core_news_sm")
print(nlp.vocab["Bestellung"].is_stop) # return: FALSE
print(nlp.vocab["darunter"].is_stop) # return: TRUE

谢谢

最佳答案

这与之前 spaCy 模型中的一个错误有关。在最新的 spaCy 中运行良好。英文模型示例:

>>> import spacy
>>> nlp = spacy.load('en')
>>> from spacy.lang.en.stop_words import STOP_WORDS
>>> STOP_WORDS.add("Bestellung")
>>> print(nlp.vocab["Bestellung"].is_stop)
True

如果您想在现有的 spaCy 上修复此问题,可以使用此解决方法,它会更改 STOP_WORDS 中出现的单词的 is_stop 属性。

nlp.vocab.add_flag(lambda s: s.lower() in spacy.lang.en.stop_words.STOP_WORDS, spacy.attrs.IS_STOP)

这在spaCy issue中提到在 Github 上

关于nlp - Spacy - 自定义停用词不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709214/

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