python - Change value of leaf in NLTK -


i want change value of leaf in parsed tree object in nltk. use following code.

t = tree(line) chomsky_normal_form(t, horzmarkov=2, vertmarkov=1, childchar = "|", parentchar = "^") print t  leaf in t.leaves():     if leaf==k[0][1]:         leaf = "newvalue"  print t 

as 2 'print t' gives exact same output of tree. thought possible set value leaf in way seems wrong. how should update value of leaf? class of each leaf str. possible change them doesn't seem update update object in tree.

you use treepositions('leaves') (docs) position of leaves in tree , change directly in tree.

t = tree(line) chomsky_normal_form(t, horzmarkov=2, vertmarkov=1, childchar = "|", parentchar = "^")  leafpos in t.treepositions('leaves'):     if t[leafpos] == k[0][1]:         t[leafpos] = "newvalue"  print t 

Comments

Popular posts from this blog

Eclipse formatter for java ending braces -

ruby - Nesting modules inside of a Rails eninge gem -

java - Are there any classes that implement javax.persistence.Parameter<T>? -