I have an array ( list
) of words that represents a tree created by SpaCy and I would like to get the first element start_VB_ROOT
but I don't get it.
>>>questionSpacy = spacy_nlp(question)
>>>treeQuestion = nltk_spacy_tree(questionSpacy)
>>>print(treeQuestion)
[Tree('start_VB_ROOT', ['When_WRB_advmod', 'did_VBD_aux', 'Beyonce_NNP_nsubj', Tree('becoming_VBG_xcomp', ['popular_JJ_acomp']), '?_._punct'])]
Indeed when it did treeQuestion[0]
I get :
(start_VB_ROOT
When_WRB_advmod
did_VBD_aux
Beyonce_NNP_nsubj
(becoming_VBG_xcomp popular_JJ_acomp)
We want start_VB_ROOT
but when did treeQuestion[0][0]
I getWhen_WRB_advmod
And when it did print(treeQuestion[0].label)
I get:
<bound method Tree.label of Tree('start_VB_ROOT', ['When_WRB_advmod', 'did_VBD_aux', 'Beyonce_NNP_nsubj', Tree('becoming_VBG_xcomp', ['popular_JJ_acomp']), '?_._punct'])>
So if I want to access element 1, I'm on IndexError
I assume that the elements are instances of
nltk.Tree
, so if you want to access the node label you must use the methodTree.label()
: