【问题标题】:How To Parse Verbs Using Spacy如何使用 Spacy 解析动词
【发布时间】:2018-03-14 07:17:41
【问题描述】:

我正在尝试解析语料库中的动词并将它们列在字典中,并计算每个动词作为及物、不及物和双及物出现的次数。我想知道如何使用 spacy 来解析动词并将它们标记为及物、不及物和双及物。

【问题讨论】:

标签: python dictionary spacy linguistics


【解决方案1】:

这里,我总结一下Mirith/Verb-categorizer的代码。基本上,您可以遍历VERB 令牌并查看它们的子代,将它们分类为及物、不及物或双及物。一个例子如下。

首先,导入spacy

import spacy
nlp = spacy.load('en')

假设您有一个令牌示例,

tokens = nlp('I like this dog. It is pretty good. I saw a bird. We arrived at the classroom door with only seven seconds to spare.')

您可以创建以下函数将VERB 转换为您想要的新类型:

def check_verb(token):
    """Check verb type given spacy token"""
    if token.pos_ == 'VERB':
        indirect_object = False
        direct_object = False
        for item in token.children:
            if(item.dep_ == "iobj" or item.dep_ == "pobj"):
                indirect_object = True
            if (item.dep_ == "dobj" or item.dep_ == "dative"):
                direct_object = True
        if indirect_object and direct_object:
            return 'DITRANVERB'
        elif direct_object and not indirect_object:
            return 'TRANVERB'
        elif not direct_object and not indirect_object:
            return 'INTRANVERB'
        else:
            return 'VERB'
    else:
        return token.pos_

示例

[check_verb(t) for t in tokens] # ['PRON', 'TRAN', 'DET', 'NOUN', 'PUNCT', ...]

【讨论】:

  • 在问题的上下文中,这个答案是完全正确的。但是对那些只看这段代码的人的警告:你需要一个语料库来回答“动词 V 是及物、不及物还是双及物?”这个问题。因为只是观察了一些使用 V 的次数,例如不及物并不意味着它也不能被使用
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2019-01-12
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多