git @ Cat's Eye Technologies Dissociated-Parse / master

Tree @master (Download .tar.gz) @masterraw · history · blame

#!/usr/bin/env python3

# Copyright (c) 2021-2024 Chris Pressey, Cat's Eye Technologies
# This file is distributed under the MIT license.  For more information, see
# the file LicenseRef-MIT-X-Dissociated-Parse.txt in the LICENSES directory.
# SPDX-License-Identifier: LicenseRef-MIT-X-Dissociated-Parse

import json
import os
import re
import sys

def render(f, tree):
    render_r(f, tree)

def render_r(f, tree):
    if isinstance(tree, str):
        f.write(' ')
    elif isinstance(tree, list):
        tag = tree[0]
        assert isinstance(tag, str)
        for child in tree[1:]:
            render_r(f, child)

def enter(map, tree):
    if isinstance(tree, str):
    elif isinstance(tree, list):

        # key each tree by its part of speech plus the first word (only) of its content.
        words = []
        for child in tree:
            if isinstance(child, str):
        key = '-'.join(words[:2])

        map.setdefault(key, []).append(tree)
        for child in tree[1:]:
            enter(map, child)

def main():
    map = {}

    with open('data/trees.json', 'r') as f:
        data = json.loads(

    # for tree in data['trees']:
    #     render(sys.stdout, tree)

    for tree in data['trees']:
        enter(map, tree)

    with open('data/productions.json', 'w') as f:
        f.write(json.dumps(map, indent=4))