【问题标题】:Spacy lemma different results from 'English' class and en_core_web_smSpacy lemma 与“英语”类和 en_core_web_sm 的结果不同
【发布时间】:2019-06-07 02:03:05
【问题描述】:

我想知道为什么我从这两种语言模型中得到不同的词形还原结果:spacy.lang.en.English 和 en_core_web_sm。

根据文档,在加载 en_core_web_sm 模型时,这应该告诉 spacy 使用语言“en”并初始化 spacy.lang.en.English,所以我不明白为什么词形还原规则会改变。如何从英文模型中获取引理,但从 en_core_web_sm 中获取标签和 POS?

from spacy.lang.en import English
nlp = English()
doc = nlp('nonlinearities')
print('English nlp: ', [(token.lemma_, token.lemma, token.tag_, token.pos_) for token in doc])

nlp = spacy.load('en_core_web_sm', disable = ['ner'])
doc = nlp('nonlinearities')
print('loaded model nlp: ', [(token.lemma_, token.lemma, token.tag_, token.pos_) for token in doc])

这是输出:

English nlp:  [('nonlinearity', 3011504801575762058, '', '')]
loaded model nlp:  [('nonlinearitie', 2964900603636025371, 'NNS', 'NOUN')]

【问题讨论】:

    标签: python spacy


    【解决方案1】:

    正如您正确指出的那样,spacy.lang.en.Englishen_core_web_sm 是两个不同的模型。这两个模型可能会以不同的方式对单词进行 POS 标记。并且由于一个词的词条也取决于它的词性标签,两个模型可能会返回不同的词条化。

    使用来自English 的引理和来自en_core_web_sm 的POS

    nlp = English()
    sentence = 'nonlinearities is none'
    doc1 = nlp(sentence)
    print('English nlp: ', [(token.lemma_, token.lemma, token.tag_, token.pos_) for token in doc1])
    
    nlp = spacy.load('en_core_web_sm', disable = ['ner'])
    doc2 = nlp(sentence)
    print('loaded model nlp: ', [(token.lemma_, token.lemma, token.tag_, token.pos_) for token in doc2])
    
    print('Mixed nlp: ', [(doc1[i].lemma_, doc1[i].lemma, token.tag_, token.pos_) for i, token in enumerate(doc2)])
    doc2 = nlp('nonlinearities is none')
    print('loaded model nlp: ', [(token.lemma_, token.lemma, token.tag_, token.pos_) for token in doc2])
    
    print('Mixed nlp: ', [(doc1[i].lemma_, doc1[i].lemma, token.tag_, token.pos_) for i, token in enumerate(doc2)])
    

    【讨论】:

    • 补充一点,spacy.lang.en.English 是库附带的基本语言类,仅包含语言数据。没有统计模型(因为您没有加载任何数据),因此也没有词性标签。您加载的 en_core_web_sm 包附带统计组件的权重,包括词性标注器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多