Extracts relations from a DGParentedTree.

    Given a DGParentedTree, returns a (relation name, relation type) dict
    of all the RST relations occurring in that tree.

def extract_relations(dgtree, relations=None):
    """Extracts relations from a DGParentedTree.

    Given a DGParentedTree, returns a (relation name, relation type) dict
    of all the RST relations occurring in that tree.
    """
    if hasattr(dgtree, 'reltypes'):
        # dgtree is an RSTTree or a DisTree that contains a DGParentedTree
        return dgtree.reltypes

    if relations is None:
        relations = {}

    if is_leaf(dgtree):
        return relations

    root_label = dgtree.label()
    if root_label == '':
        assert dgtree == DGParentedTree('', []), \
            "The tree has no root label, but isn't empty: {}".format(dgtree)
        return relations
    elif root_label in NUCLEARITY_LABELS:
        for child in dgtree:
            relations.update(extract_relations(child, relations))
    else:  # dgtree is a 'relation' node
        child_labels = [child.label() for child in dgtree]
        assert all(label in NUCLEARITY_LABELS for label in child_labels)
        if 'S' in child_labels:
            relations[root_label] = 'rst'
        else:
            relations[root_label] = 'multinuc'
        for child in dgtree:
            relations.update(extract_relations(child, relations))

    return relations