Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Focusing only on the example you're giving in this Q, and the Q's title about converting vertical trees to S-expressions, something like...:</p> <pre><code>import re import sys samp='''S =NP ==(DT +def) the == (N +ani) man =VP ==V walks'''.splitlines() relinelev = re.compile(r'(=*)(.*)') reclean = re.compile(r'\s*\((\S+)[^)]*\)') def clean(line): return reclean.sub(r'\1', line) def reparse(tree=samp): stack = [-1] for line in tree: equals, rest = relinelev.match(line).groups() linelev = len(equals) while linelev &lt; stack[-1]: sys.stdout.softspace = False print ')', curlev = stack.pop() if linelev == stack[-1]: sys.stdout.softspace = False print ')', else: stack.append(linelev) print '(%s' % clean(rest), while stack[-1] &gt;= 0: sys.stdout.softspace = False print ')', stack.pop() print reparse() </code></pre> <p>seems to work, and outputs</p> <pre><code>(S (NP (DT the) (N man)) (VP (V walks))) </code></pre> <p>I realize you're trying to do much more "cleaning" than I'm doing here, but that can be concentrated in the <code>clean</code> function, leaving <code>reparse</code> to deal with the Q's title. If you don't want to print as you go, but rather return the result as a string, the changes are of course quite minor:</p> <pre><code>def reparse(tree=samp): stack = [-1] result = [] for line in tree: equals, rest = relinelev.match(line).groups() linelev = len(equals) while linelev &lt; stack[-1]: result[-1] += ')' curlev = stack.pop() if linelev == stack[-1]: result[-1] += ')' else: stack.append(linelev) result.append('(%s' % clean(rest)) while stack[-1] &gt;= 0: result[-1] += ')' stack.pop() return ' '.join(result) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload