Extract parent and child node from python tree -


i using nltk's tree data structure.below sample nltk.tree.

(s   (s     (advp (rb recently))     (np (nn someone))     (vp       (vbd mentioned)       (np (dt the) (nn word) (nn malaria))       (pp (to to) (np (prp me)))))   (, ,)   (cc and)   (in so)   (s     (np       (np (cd one) (jj whole) (nn flood))       (pp (in of) (np (nns memories))))     (vp (vbd came) (s (vp (vbg pouring) (advp (rb back))))))   (. .)) 

i not aware of nltk.tree datastructure. want extract parent , super parent node every leaf node e.g. 'recently' want (advp, rb), , 'someone' (np, nn)this final outcome want.earlier answer used eval() function want avoid.

[('advp', 'rb'), ('np', 'nn'), ('vp', 'vbd'), ('np', 'dt'), ('np', 'nn'), ('np', 'nn'), ('pp', 'to'), ('np', 'prp'), ('s', 'cc'), ('s', 'in'), ('np', 'cd'), ('np', 'jj'), ('np', 'nn'), ('pp', 'in'), ('np', 'nns'), ('vp', 'vbd'), ('vp', 'vbg'), ('advp', 'rb')] 

python code same without using eval function , using nltk tree datastructure

sentences = " (s   (s (advp (rb recently)) (np (nn someone)) (vp   (vbd mentioned)   (np (dt the) (nn word) (nn malaria))   (pp (to to) (np (prp me)))))   (, ,)   (cc and)   (in so)   (s     (np       (np (cd one) (jj whole) (nn flood))       (pp (in of) (np (nns memories))))     (vp (vbd came) (s (vp (vbg pouring) (advp (rb back))))))   (. .))"  print list(tails(sentences))   def tails(items, path=()): child in items:     if type(child) nltk.tree:         if child.label() in {".", ","}:  # ignore punctuation             continue         result in tails(child, path + (child.label(),)):             yield result     else:         yield path[-2:] 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -