On Apr 16, 10:16 pm, John Machin <sjmac...@lexicon.net> wrote: > On Apr 17, 8:55 am, Clarendon <jine...@hotmail.com> wrote: > > > > > Hello! > > > I need a program that accesses a parse tree based on the designated > > words (terminals) within the tree. For instance, in: > > > I came a long way in changing my habit. > > > (ROOT > > (S > > (NP (PRP I)) > > (VP (VBD came) > > (NP (DT a) (JJ long) (NN way)) > > (PP (IN in) > > (S > > (VP (VBG changing) > > (NP (PRP$ my) (NN habit)))))) > > > the designated words are "a long way". I need the program to recognize > > how many parentheses there are after them. Currently two: NN way)). > > Then I need it to see how many parentheses there are before it. > > Currently there are two as well: (NP (DT. > > Why is the answer not (S (VP (NP (DT ? You may need to explain what > you mean by "before" and "after" ... also the parentheses are an > artifact of this particular method of representing a parse tree. What > in general terms are you trying to do?
It sounds like the OP wants a 'flatten' function: a tuple of tuple A, where tuple A is ( category, word, ref to original, parent index, depth ). Unproduced: >>> x= flatten( sentence_representation ) >>> x[ :9 ] ( 'ROOT', None, <object>, None, 0 ) ( 'S', None, <object>, 0, 1 ) ( 'NP', None, <object>, 1, 2 ) ( 'PRP', 'I', <object>, 2, 3 ) ( 'VP', None, <object>, 1, 2 ) ( 'VBD', 'came', <object>, 4, 2 ) ( 'NP', None, <object>, 4, 3 ) ( 'DT', 'a', <object>, 6, 4 ) ( 'JJ', 'long', <object>, 6, 4 ) ( 'NN', 'way', <object>, 6, 4 ) I'm not certain it's accurate. The depth field is redundant but may be useful to have. -- http://mail.python.org/mailman/listinfo/python-list