如何打印树的所有叶子

时间:2013-12-12 17:55:09

标签: python tree text-parsing string-parsing

这是我的代码:

from stat_parser import Parser, display_tree


parser = Parser()

# http://www.thrivenotes.com/the-last-question/
evaluate =  parser.parse("""


In it's first elections, Aam Aadmi Party beats Sheila Dixit winning the Delhi Elections.


""")


for i in range(0,len(evaluate)):
    if evaluate[i].node == 'S':
        for j in evaluate[i]:
            for l in j:
                print l.leaves
        print

但是这会返回:

<bound method Tree.leaves of Tree(u'PRP', ['it'])>
<bound method Tree.leaves of Tree(u'VBZ', ["'s"])>
<bound method Tree.leaves of Tree(u'NP', [Tree(u'JJ', ['first']), Tree(u'NNS', ['elections'])])>

<bound method Tree.leaves of Tree(u'NNP', ['Aam'])>
<bound method Tree.leaves of Tree(u'NNP', ['Aadmi'])>
<bound method Tree.leaves of Tree(u'NNP', ['Party'])>
<bound method Tree.leaves of Tree(u'NNP', ['beats'])>
<bound method Tree.leaves of Tree(u'NNP', ['Sheila'])>
<bound method Tree.leaves of Tree(u'NNP', ['Dixit'])>
<bound method Tree.leaves of Tree(u'VBG', ['winning'])>
<bound method Tree.leaves of Tree(u'NP', [Tree(u'DT', ['the']), Tree(u'NNP', ['Delhi']), Tree(u'NNP', ['Elections'])])>

但是我想以正确的顺序打印“S”分支下的所有内容?

我该怎么做?

0 个答案:

没有答案