2

I am looking for tools that can format parse trees. I want to be able to collapse and expand them. I understand that collapsing them is rather simple with regex or even simple search and replace, but I am doing some testing which requires me to work with a large amount of trees for testing.

When I say expand, I mean pretty print them in a way that represents the tree

For example, the following parse tree:

(S (VP (DT He) (VBP ran)))

Expands out to:

(S
    (VP
        (DT He) 
        (VBP ran)
    )
)

Again, I understand collapsing them back is rather simple, but I have a lot of trees to test.. AND these trees can be quite large. I tried googling variations on "parse syntax tree tools formatter" and wasn't finding anything. This would be something similar to a programming language beautifier.

Any links for online syntax tools would be greatly appreciated!

  • 2
    You could just look for a Lisp or Scheme pretty printer, which would do the job, though perhaps not exactly with the same output like you specify here. – Oliver Mason Jul 26 '19 at 9:32
3

Here is a script I quickly assembled from pieces of code I had lying around:

https://trinket.io/python3/a33a025467?toggleCode=true&showInstructions=true

Beware that this script parses your tree very naively; it just processes the input symbol by symbol and does some formatting when it encounters opening or closing brackets; it will not warn you if you have any syntax errors in your tree structure.

I included the option to set a mode (plain or fancy), indentation depth and permitted opening/closing brackets - to change the settings, click the pen button, change the code and hit the play button to re-run the script; the usage of the settings is hopefully self-explanatory enough even if you don't know Python. This could of course be made more user-friendly if one had time.

Example (plain mode, indentation depth 4):

Place your flat tree here:

(S (VP (DT He) (VBP ran)))

Here is your inflated tree:

(S 
    (VP 
        (DT He)
        (VBP ran)
    )
)

Example (fancy mode, indentation depth 3):

Place your flat tree here:

(S (VP (DT He) (VBP ran)))

Here is your inflated tree:

|-- S 
|   |-- VP 
|   |   |-- DT He
|   |   |-- VBP ran

Probably if someone messes up the script in the edit mode with this online tool, it will be lost for good, so here is the source code for restoration:

"""
This is a simple script for formatting tree structures.
Author: https://linguistics.stackexchange.com/users/13238/lemontree

You can customize
- whether to use plain and fancy printout (default: plain),
- the depth of indentation (default: 4),
- the permitted opening and closing brackets (default: "[", "("),
by editing the settings in the source code.

Exmple input:
[S [NP [PRN This]] [VP [V is] [NP [D a] [N tree]]]]

"""

# print the documentation
print(__doc__)

# settings -- make your changes here
mode = "plain"        # set the printout mode (one out of "plain" or "fancy")
ind_depth = 4         # set the amount of spaces by which you want each nesting level to be indented
opening = ["(", "["]  # set the symbol sequences which signal the start of a subtree
closing = [")", "]"]  # set the symbol sequences which signal the end   of a subtree

# ask for tree input
tree_in = input("Place your flat tree here:\n")

# generate formatted tree
tree_out = ""
ind_level = -1
for char in tree_in:  # parse each character

    # opening bracket found - new nesting level; increase ind. level, add newline and indentation
    if char in opening:
        ind_level += 1
        if mode == "plain":
            tree_out += "\n" + \
                        (ind_level * ind_depth) * " "
        elif mode == "fancy":
            tree_out += "\n" + \
                        (ind_level * ("|" + ind_depth * " ") if ind_level > 0 else "") + \
                        "|" + (ind_depth-1) * "-" + " "

    # append current symbol
    if mode == "plain":
        tree_out += char
    elif mode == "fancy":
        if char not in opening and char not in closing:
            tree_out += char

    # closing bracket found - end of nesting level; decrease ind. level and add newline
    if char in closing:
        ind_level -= 1
        if mode == "plain":
            tree_out += "\n" + \
                        (ind_level * ind_depth) * " "
        elif mode == "fancy":
            tree_out += "\n" + \
                        (ind_level * ind_depth + 1) * " " + \
                        "\n"

tree_out = "\n".join([line for line in tree_out.splitlines() if line.split()])  # remove blank lines

# output formatted tree
print("\nHere is your inflated tree:\n")
print(tree_out)

If anyone has recommendations on possibly better online Python interpreters which permit running code with user input, I'm happy to learn about them.

| improve this answer | |
  • Ah, WOW!!! Why thank you lemontree! Is there a way I can give you more points? – Steve3p0 Jul 26 '19 at 22:22
  • If you want, you can sacrifice some of your own reputation to award a bounty. – lemontree Jul 27 '19 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.