【问题标题】:Customize spacy stop words and save the model自定义 spacy 停用词并保存模型
【发布时间】:2021-06-02 20:00:50
【问题描述】:

我正在使用它来将停用词添加到 spacy 的停用词列表中

nlp.Defaults.stop_words |= {"my_new_stopword1","my_new_stopword2",}

但是,当我使用 nlp.to_disk() 保存 nlp 对象并使用 nlp.from_disk() 再次加载它时, 我丢失了自定义停用词列表。 有没有办法使用 nlp 模型保存自定义停用词?

提前致谢

【问题讨论】:

    标签: python-3.x nlp spacy stop-words spacy-3


    【解决方案1】:

    大多数语言默认值(停用词、词法属性和语法迭代器)不与模型一起保存。

    如果要自定义它们,可以创建自定义语言类,请参阅:https://spacy.io/usage/linguistic-features#language-subclass。从此链接复制的示例:

    from spacy.lang.en import English
    
    class CustomEnglishDefaults(English.Defaults):
        stop_words = set(["custom", "stop"])
    
    class CustomEnglish(English):
        lang = "custom_en"
        Defaults = CustomEnglishDefaults
    
    nlp1 = English()
    nlp2 = CustomEnglish()
    
    print(nlp1.lang, [token.is_stop for token in nlp1("custom stop")])
    print(nlp2.lang, [token.is_stop for token in nlp2("custom stop")])
    

    【讨论】:

    • 此解决方案有效。但是,这个 nlp 对象不包含词向量。
    猜你喜欢
    • 2019-03-13
    • 2017-05-01
    • 2018-09-14
    • 2018-12-26
    • 2019-01-11
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多