我有一个树结构文件,其中括号用于表示树.这是将代码转换为
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()
当字符长度为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’] ……….. …]
怎么修改?
最佳答案 我以为你想用_!来表示带空格的字符串.然后我使用正则表达式拆分表达式:
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', [...], [...], [...]]]]]