【问题标题】:How do I interpret the results of a sentence's parse tree built using Spacy in Python?如何解释在 Python 中使用 Spacy 构建的句子解析树的结果?
【发布时间】:2016-10-06 12:23:05
【问题描述】:

我正在尝试使用 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_)

代码给出了以下结果。有人可以告诉我如何解释吗?提前致谢!

 ('Head:', bear, ' Left:', The, ' Right:', The, ' Relationship:', u'det')
   ('Head:', bear, ' Left:', angry, ' Right:', angry, ' Relationship:', u'amod')
   ('Head:', chased, ' Left:', The, ' Right:', bear, ' Relationship:', u'nsubj')
   ('Head:', chased, ' Left:', The, ' Right:', squirrel, ' Relationship:', u'ROOT')
   ('Head:', squirrel, ' Left:', the, ' Right:', the, ' Relationship:', u'det')
   ('Head:', squirrel, ' Left:', frightened, ' Right:', frightened, ' Relationship:', u'amod')
   ('Head:', squirrel, ' Left:', little, ' Right:', little, ' Relationship:', u'amod')
   ('Head:', chased, ' Left:', the, ' Right:', squirrel, ' Relationship:', u'dobj')

【问题讨论】:

标签: python nlp parse-tree spacy


【解决方案1】:

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

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))

以上代码的结果将如下所示:

det(world-1, The-0)
nsubj(has-2, world-1)
ROOT(has-2, has-2)
dobj(has-2, enough-3)
prep(enough-3, for-4)
poss(need-7, everyone-5)
case(everyone-5, 's-6)
pobj(for-4, need-7)
punct(need-7, ,-8)
neg(for-10, not-9)
prep(need-7, for-10)
poss(greed-13, everyone-11)
case(everyone-11, 's-12)
pobj(for-10, greed-13)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多