Reputation: 11
I'm trying to build and interpret the results of a parse tree of a sentence using Spacy in Python. I've used the below code for the same :
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_)
The code gave the below result.Can someone tell me how to interpret it? Thanks in advance!
('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')
Upvotes: 1
Views: 1026
Reputation: 21
You could interpret the dependency tree by listing out it's edges as shown below:
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))
The result of the above code will look like this:
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)
Upvotes: 2