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

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -