How to parse output of dependency tree into flattened structure

Viewed 577

Okay, I am completely at a loss. I have some output from a dependency tree parsing tool that looks like the following:

(S 
    (NP 
        (PRP It)
    )
    (VP
        (VBD said)
        (CLAUSE
            (S
                (NP
                    (DT the)
                    (NN figure)
                )
                (VP 
                    (VBD was)
                    (VBN rounded)
                )
            )
        )
    )
    (PUNC .)
)

These parse outputs are saved as plain-text. The output is basically a binary tree as far as I can tell. What I would like is to have an output file where each word is on a new line, and each word contains all of the labels associated with the word. An example is:

It  S   NP  PRP
said    S   VP
the S   VP  CLAUSE  S   NP  DT
figure  S   VP  CLAUSE  S   NP  NN
was S   VP  CLAUSE  S   VP  VBD
rounded S   VP  CLAUSE  S   VP  VBN
. PUNC  S

How can I parse this output into the output I'm looking for? I've attempted to use the pyparsing library, and have been able to parse the string into a hierarchical list of lists, but this doesn't quite match my output needs.

I think recursion is probably a good candidate tool here, but I'm not sure how to apply it to this problem. Any help on this would be appreciated - even pseudo-code to get the implementation idea.

1 Answers
Related