我试图在Python中使用space构建和解释句子解析树的结果。我使用了下面的代码:
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')
您可以通过列出它的边来解释依赖树,如下所示:
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)