from anytree import Node, RenderTree
class WordTree:
def __init__(self, array, parent = None):
#print("son :",array[0][i])
self.parent = array[0].label()[]
#selfself.children = [] # if parenthesis then it has son after "," analyse : include all elements until the next parenthesi
self.data = array
#print(array[0])
for elementson in arrayarray[0]:
print(type(elementson),elementson)
if type(elementson) is Tree:
print("sub tree creation")
t = WordTree(elementson) # should I verify if parent is empty ?
elif elementtype(son) is stringstr:
print("son creation")
self.parent = parent
self.data = array[0]children.append(son)
else:
print("issue?")
break # prolbem ?
Funciona bien al principio, pero cuando encontramos un subárbol tenemos problemas.
>>> t = WordTree(treeQuestion, treeQuestion[0].label())
<class 'str'> When_WRB_advmod
son creation
<class 'str'> did_VBD_aux
son creation
<class 'str'> Beyonce_NNP_nsubj
son creation
<class 'nltk.tree.Tree'> (becoming_VBG_xcomp popular_JJ_acomp)
sub tree creation
<class 'str'> p
son creation
<class 'str'> o
son creation
<class 'str'> p
Solo toma las letras del subárbol, no las palabras.