extract all pointing chains (e.g. coreference chains) from a document
    graph (or just from the specified layer). return dictionaries describing
    which token signals the beginning of markables, the end of markables,
    as well as a mapping from a markable to the chain it belongs to.

    Parameters
    ----------
    layer : str or None
        The layer from which the pointing chains/relations
        (i.e. coreference relations) should be extracted.
        If no layer is selected, all pointing relations will be considered.
        (This might lead to errors, e.g. when the document contains Tiger
        syntax trees with secondary edges.)

def gen_bracket_mappings(docgraph, layer=None):
    """
    extract all pointing chains (e.g. coreference chains) from a document
    graph (or just from the specified layer). return dictionaries describing
    which token signals the beginning of markables, the end of markables,
    as well as a mapping from a markable to the chain it belongs to.

    Parameters
    ----------
    layer : str or None
        The layer from which the pointing chains/relations
        (i.e. coreference relations) should be extracted.
        If no layer is selected, all pointing relations will be considered.
        (This might lead to errors, e.g. when the document contains Tiger
        syntax trees with secondary edges.)
    """
    # we can't rely on the .ns attribute of a merged graph
    if layer:
        namespace = dg.layer2namespace(layer)
    else:
        namespace = docgraph.ns

    pointing_chains = dg.get_pointing_chains(docgraph, layer=layer)
    markables = sorted(itertools.chain(*pointing_chains),
                       key=dg.util.natural_sort_key)

    markable2chain = {}
    for chain in pointing_chains:
        chain_id = chain[0] # first markable in a chain
        for markable in chain:
            markable2chain[markable] = chain_id

    opening = defaultdict(list)
    closing = defaultdict(list)
    for markable in markables:
        if namespace+':span' in docgraph.node[markable]:
            span_tokens = spanstring2tokens(
                docgraph, docgraph.node[markable][namespace+':span'])
            opening[span_tokens[0]].append(markable)
            closing[span_tokens[-1]].append(markable)
    return opening, closing, markable2chain