<commit_before>
"""
Operations on EDS
"""

from itertools import count

from delphin import variable
from delphin import scope
from delphin import eds
from delphin import util


def from_mrs(m, predicate_modifiers=False, unique_ids=True,
             representative_priority=None):
    """
    Create an EDS by converting from MRS *m*.

    In order for MRS to EDS conversion to work, the MRS must satisfy
    the intrinsic variable property (see
    :func:`delphin.mrs.has_intrinsic_variable_property`).

    Args:
        m: the input MRS
        predicate_modifiers: if `True`, include predicate-modifier
            edges; if `False`, only include basic dependencies; if a
            callable, then call on the converted EDS before creating
            unique ids (if `unique_ids=True`)
        unique_ids: if `True`, recompute node identifiers to be unique
            by the LKB's method; note that ids from *m* should already
            be unique by PyDelphin's method
        representative_priority: a function for ranking candidate
            representative nodes; see :func:`scope.representatives`
    Returns:
        EDS
    Raises:
        EDSError: when conversion fails.
    """
    # EP id to node id map; create now to keep ids consistent
    hcmap = {hc.hi: hc for hc in m.hcons}
    reps = scope.representatives(m, priority=representative_priority)
    ivmap = {e.iv : e for e in m.predications}

    top = _mrs_get_top(m.top, hcmap, reps, m.index, ivmap)
    deps = _mrs_args_to_basic_deps(m, hcmap, reps)
    nodes = _mrs_to_nodes(m, deps)

    e = eds.EDS(
        top=top,
        nodes=nodes,
        lnk=m.lnk,
        surface=m.surface,
        identifier=m.identifier)

    if predicate_modifiers is True:
        predicate_modifiers = find_predicate_modifiers
    if predicate_modifiers:
        addl_deps = predicate_modifiers(e, m, representatives=reps)
        for id, node_deps in addl_deps.items():
            e[id].edges.update(node_deps)

    if unique_ids:
        make_ids_unique(e, m)

    return e


def _mrs_get_top(top, hcmap, reps, index, ivmap):
    if top in hcmap:
        lbl = hcmap[top].lo
        if lbl in reps:
            top = reps[lbl][0].id
        else:
            top = reps[ivmap[index].label][0].id
    elif top in reps:
        top = reps[top][0].id
    else:
        top = None
    return top


def _mrs_args_to_basic_deps(m, hcmap, reps):
    ivmap = {p.iv: (p, q)
             for p, q in m.quantification_pairs()
             if p is not None}
    edges = {}

    for src, roleargs in m.arguments().items():
        if src in ivmap:
            p, q = ivmap[src]
            # non-quantifier EPs
            edges[src] = {}
            for role, tgt in roleargs:
                # qeq
                if tgt in hcmap:
                    lbl = hcmap[tgt].lo
                    tgt = reps[lbl][0].id
                # label arg
                elif tgt in reps:
                    tgt = reps[tgt][0].id
                # regular arg<commit_msg>related to #319<commit_after>
"""
Operations on EDS
"""

from itertools import count

from delphin import variable
from delphin import scope
from delphin import eds
from delphin import util


def from_mrs(m, predicate_modifiers=False, unique_ids=True,
             representative_priority=None):
    """
    Create an EDS by converting from MRS *m*.

    In order for MRS to EDS conversion to work, the MRS must satisfy
    the intrinsic variable property (see
    :func:`delphin.mrs.has_intrinsic_variable_property`).

    Args:
        m: the input MRS
        predicate_modifiers: if `True`, include predicate-modifier
            edges; if `False`, only include basic dependencies; if a
            callable, then call on the converted EDS before creating
            unique ids (if `unique_ids=True`)
        unique_ids: if `True`, recompute node identifiers to be unique
            by the LKB's method; note that ids from *m* should already
            be unique by PyDelphin's method
        representative_priority: a function for ranking candidate
            representative nodes; see :func:`scope.representatives`
    Returns:
        EDS
    Raises:
        EDSError: when conversion fails.
    """
    # EP id to node id map; create now to keep ids consistent
    hcmap = {hc.hi: hc for hc in m.hcons}
    reps = scope.representatives(m, priority=representative_priority)
    ivmap = {e.iv : e for e in m.predications}

    top = _mrs_get_top(m.top, hcmap, reps, m.index, ivmap)
    deps = _mrs_args_to_basic_deps(m, hcmap, reps)
    nodes = _mrs_to_nodes(m, deps)

    e = eds.EDS(
        top=top,
        nodes=nodes,
        lnk=m.lnk,
        surface=m.surface,
        identifier=m.identifier)

    if predicate_modifiers is True:
        predicate_modifiers = find_predicate_modifiers
    if predicate_modifiers:
        addl_deps = predicate_modifiers(e, m, representatives=reps)
        for id, node_deps in addl_deps.items():
            e[id].edges.update(node_deps)

    if unique_ids:
        make_ids_unique(e, m)

    return e


def _mrs_get_top(top, hcmap, reps, index, ivmap):
    if top in hcmap:
        lbl = hcmap[top].lo
        if lbl in reps:
            top = reps[lbl][0].id
        else:
            top = reps[ivmap[index].label][0].id
    elif top in reps:
        top = reps[top][0].id
    else:
        top = None
    return top


def _mrs_args_to_basic_deps(m, hcmap, reps):
    ivmap = {p.iv: (p, q)
             for p, q in m.quantification_pairs()
             if p is not None}
    edges = {}

    for src, roleargs in m.arguments().items():
        if src in ivmap:
            p, q = ivmap[src]
            # non-quantifier EPs
            edges[src] = {}
            for role, tgt in roleargs:
                # qeq
                if tgt in hcmap:
                    lbl = hcmap[tgt].lo
                    if lbl in reps:
                        tgt = reps[lbl][0].id
                    else:
                        continue
                # label arg
                elif tgt in reps:
                    tgt = reps[tgt][0].id
                # regular arg
{'fraction_non_alphanumeric': 0.08334692546077312, 'fraction_numerical': 0.0021203718806067527, 'mean_word_length': 1.9810403500243072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2235835', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1908, 'n_words': 593}