如何解释在Python中使用Spacy构建的句子解析树的结果?

2024-10-04 07:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Python中使用Spacy构建和解释一个句子的解析树的结果。 我也使用了以下代码:

from spacy.en import English
nlp=English()
example = "The angry bear chased the frightened little squirrel"
parsedEx = nlp(unicode(example))
for token in parsedEx:
   print("Head:", token.head, " Left:",token.left_edge, " Right:",token.right_edge ," Relationship:",token.dep_)

代码给出了以下内容结果。可以有人告诉我怎么解释?提前谢谢!在

^{pr2}$

Tags: the代码fromimporttokennlpenglishspacy
1条回答
网友
1楼 · 发布于 2024-10-04 07:28:31

可以通过列出依赖树的边来解释依赖树,如下所示:

import spacy
nlp = spacy.load('en')
doc = nlp(u'The world has enough for everyone\'s need, not for everyone\'s greed') 
for tok in doc: 
    print('{}({}-{}, {}-{})'.format(tok.dep_, tok.head.text, tok.head.i, tok.text, tok.i))

上述代码的结果如下所示:

^{pr2}$

相关问题 更多 >