【问题标题】:Given a word can we get all possible lemmas for it using Spacy?给定一个词,我们可以使用 Spacy 获得所有可能的引理吗?
【发布时间】:2021-06-01 13:13:35
【问题描述】:

输入词是独立的,不是句子的一部分,但我想得到它所有可能的引理,就好像输入词在不同的句子中一样,带有所有可能的词性标签。我还想获得单词引理的查找版本。

我为什么要这样做?

我已经从所有文档中提取了引理,并且我还计算了引理之间的依赖链接的数量。我使用en_core_web_sm 完成了这两项工作。现在,给定一个输入词,我想返回最常链接到输入词的所有可能词条的词条。

因此,简而言之,我想用所有可能的词性标签复制token._lemma 输入词的行为,以保持与我计算的引理链接的一致性。

【问题讨论】:

    标签: python nlp spacy lemmatization spacy-3


    【解决方案1】:

    我发现如果不先构造一个例句来给出上下文,就很难直接从 spaCy 中得到引理和变形。这并不理想,所以我进一步观察发现LemmaInflect 做得很好。

    > from lemminflect import getInflection, getAllInflections, getAllInflectionsOOV
    
    > getAllLemmas('watches')
    {'NOUN': ('watch',), 'VERB': ('watch',)}
    
    > getAllInflections('watch')
    {'NN': ('watch',), 'NNS': ('watches', 'watch'), 'VB': ('watch',), 'VBD': ('watched',), 'VBG': ('watching',), 'VBZ': ('watches',),  'VBP': ('watch',)}
    

    【讨论】:

    • 谢谢,我将使用它作为 spaCy 的扩展来计算我的引理依赖链接。这样我觉得我会保持一致性。
    【解决方案2】:

    spaCy 并不是为此而设计的——它是为分析文本而不是生成文本而设计的。

    链接库看起来不错,但如果你想坚持使用 spaCy 或需要除英语之外的其他语言,你可以查看 spacy-lookups-data,这是用于引理的原始数据。一般来说,每个词性都会有一个字典,可以让你查找词的引理。

    【讨论】:

    【解决方案3】:

    为了获得替代引理,我正在尝试 Spacy rule_lemmatize 和 Spacy 查找数据的组合。 rule_lemmatize 可能会产生多个有效的引理,而查找数据只会为给定的单词提供一个引理(在我检查过的文件中)。然而,在某些情况下,查找数据会产生引理,而 rule_lemmatize 不会。

    我的例子是西班牙语:

    import spacy
    import spacy_lookups_data
    
    import json
    import pathlib
    
    # text = "fui"
    text = "seguid"
    # text = "contenta"
    print("Input text: \t\t" + text)
    
    # Find lemmas using rules:
    nlp = spacy.load("es_core_news_sm")
    lemmatizer = nlp.get_pipe("lemmatizer")
    doc = nlp(text)
    rule_lemmas = lemmatizer.rule_lemmatize(doc[0])
    print("Lemmas using rules: " + ", ".join(rule_lemmas))
    
    # Find lemma using lookup:
    lookups_path = str(pathlib.Path(spacy_lookups_data.__file__).parent.resolve()) + "/data/es_lemma_lookup.json"
    fileObject = open(lookups_path, "r")
    lookup_json = fileObject.read()
    lookup = json.loads(lookup_json)
    print("Lemma from lookup: \t" + lookup[text])
    

    输出:

    Input text:         fui        # I went; I was (two verbs with same form in this tense)
    Lemmas using rules: ir, ser    # to go, to be (both possible lemmas returned)
    Lemma from lookup:  ser        # to be
    
    Input text:         seguid     # Follow! (imperative)
    Lemmas using rules: seguid     # Follow! (lemma not returned) 
    Lemma from lookup:  seguir     # to follow
    
    Input text:         contenta   # (it) satisfies (verb); contented (adjective) 
    Lemmas using rules: contentar  # to satisfy (verb but not adjective lemma returned)
    Lemma from lookup:  contento   # contented (adjective, lemma form)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      相关资源
      最近更新 更多