gpt4 book ai didi

python - 如何使用 Spacy 解析动词

转载 作者:行者123 更新时间:2023-11-30 22:18:46 25 4
gpt4 key购买 nike

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

最佳答案

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

首先,导入spacy

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

假设您有一个 token 示例,

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', ...]

关于python - 如何使用 Spacy 解析动词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49271730/

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