将嵌套的括号树转换为嵌套列表

时间:2014-04-12 02:10:56

标签: python

我有一个树结构文件,其中括号用于表示树。这是将代码转换为python嵌套列表

的代码
def foo(s):
    def foo_helper(level=0):
        try:
            token = next(tokens)
        except StopIteration:
            if level != 0:
                raise Exception('missing closing paren')
            else:
                return []
        if token == ')':
            if level == 0:
                raise Exception('missing opening paren')
            else:
                return []
        elif token == '(':
            return [foo_helper(level+1)] + foo_helper(level)
        else:
            return [token] + foo_helper(level)
    tokens = iter(s)
    return foo_helper()    

由...给出 how to parse a strign and return nested array

当字符长度为1时,它可以正常工作。对于单词或句子,同样不适当的工作。 我的树样本是:

( Satellite (span 69 74) (rel2par Elaboration)
        ( Nucleus (span 69 72) (rel2par span)
          ( Nucleus (span 69 70) (rel2par span)
            ( Nucleus (leaf 69) (rel2par span) (text _!MERRILL LYNCH READY ASSETS TRUST :_!) )
            ( Satellite (leaf 70) (rel2par Elaboration) (text _!8.65 % ._!) )
          )
          ( Satellite (span 71 72) (rel2par Elaboration)
            ( Nucleus (leaf 71) (rel2par span) (text _!Annualized average rate of return_!) )
            ( Satellite (leaf 72) (rel2par Temporal) (text _!after expenses for the past 30 days ;_!) )
          )
        )
        ( Satellite (span 73 74) (rel2par Elaboration)
          ( Nucleus (leaf 73) (rel2par span) (text _!not a forecast_!) )
          ( Satellite (leaf 74) (rel2par Elaboration) (text _!of future returns ._!) )
        )
      )

这里,输出需要 ['satellite',['span','69','74'].........]但是使用给定的函数我得到的是['s','a','t'...............['s','p','a','n','7','3']..............]

如何修改?

2 个答案:

答案 0 :(得分:1)

您打算不在字符串本身上调用此函数,而是在令牌列表上调用此函数,即字符串split

def parse(s):
    def parse_helper(level=0):
        try:
            token = next(tokens)
        except StopIteration:
            if level:
                raise Exception('Missing close paren')
            else:
                return []
        if token == ')':
            if not level:
                raise Exception('Missing open paren')
            else:
                return []
        elif token == '(':
            return [parse_helper(level+1)] + parse_helper(level)
        else:
            return [token] + parse_helper(level)
    tokens = iter(s)
    return parse_helper()

if __name__ == '__main__':
    with open('tree.thing', 'r') as treefile:
        tree = treefile.read()

    print(parse(tree.split()))

如果treefile包含您发布的数据结构,我会得到以下结果:

[['Satellite', '(span', '69', '74)', '(rel2par', 'Elaboration)', ['Nucleus', '(span', '69', '72)', '(rel2par', 'span)', ['Nucleus', '(span', '69', '70)', '(rel2par', 'span)', ['Nucleus', '(leaf', '69)', '(rel2par', 'span)', '(text', '_!MERRILL', 'LYNCH', 'READY', 'ASSETS', 'TRUST', ':_!)'], ['Satellite', '(leaf', '70)', '(rel2par', 'Elaboration)', '(text', '_!8.65', '%', '._!)']], ['Satellite', '(span', '71', '72)', '(rel2par', 'Elaboration)', ['Nucleus', '(leaf', '71)', '(rel2par', 'span)', '(text', '_!Annualized', 'average', 'rate', 'of', 'return_!)'], ['Satellite', '(leaf', '72)', '(rel2par', 'Temporal)', '(text', '_!after', 'expenses', 'for', 'the', 'past', '30', 'days', ';_!)']]], ['Satellite', '(span', '73', '74)', '(rel2par', 'Elaboration)', ['Nucleus', '(leaf', '73)', '(rel2par', 'span)', '(text', '_!not', 'a', 'forecast_!)'], ['Satellite', '(leaf', '74)', '(rel2par', 'Elaboration)', '(text', '_!of', 'future', 'returns', '._!)']]]]

答案 1 :(得分:1)

我假设您想使用_!表示带空格的字符串。然后我使用正则表达式分割表达式:

from re import compile
resexp = compile(r'([()]|_!)')
…
  tokens = iter(resexp.split(s))
…

我的结果是(使用深度= 4的pprint)

$ python lispparse.py  | head
['\n',
 [' Satellite ',
  ['span 69 74'],
  ' ',
  ['rel2par Elaboration'],
  '\n        ',
  [' Nucleus ',
   ['span 69 72'],
   ' ',
   ['rel2par span'],

我进一步改进了它:

tokens = iter(filter(None, (i.strip() for i in resexp.split(s))))

得到了:

$ python lispparse.py  
[['Satellite',
  ['span 69 74'],
  ['rel2par Elaboration'],
  ['Nucleus',
   ['span 69 72'],
   ['rel2par span'],
   ['Nucleus', [...], [...], [...], [...]],
   ['Satellite', [...], [...], [...], [...]]],
  ['Satellite',
   ['span 73 74'],
   ['rel2par Elaboration'],
   ['Nucleus', [...], [...], [...]],
   ['Satellite', [...], [...], [...]]]]]