Exemplo n.º 1
0
def select_args(xmrs, nodeid=None, rargname=None, value=None):
    """
    Return the list of matching (nodeid, role, value) triples in *xmrs*.

    Predication arguments in *xmrs* match if the `nodeid` of the
    :class:`~delphin.mrs.components.ElementaryPredication` they are
    arguments of match *nodeid*, their role matches *rargname*, and
    their value matches *value*. The *nodeid*, *rargname*, and *value*
    filters are ignored if they are `None`.

    Note:
        The *value* filter matches the variable, handle, or constant
        that is the overt value for the argument. If you want to find
        arguments that target a particular nodeid, look into
        :meth:`Xmrs.incoming_args() <delphin.mrs.xmrs.Xmrs.incoming_args>`.
        If you want to match a target value to its resolved object, see
        :func:`find_argument_target`.
    Args:
        xmrs (:class:`~delphin.mrs.xmrs.Xmrs`): semantic structure to
            query
        nodeid (optional): nodeid to match
        rargname (str, optional): role name to match
        value (str, optional): argument value to match
    Returns:
        list: matching arguments as (nodeid, role, value) triples
    """
    argmatch = lambda a: (
        (nodeid is None or a[0] == nodeid) and
        (rargname is None or a[1].upper() == rargname.upper()) and
        (value is None or a[2] == value))
    all_args = ((nid, role, val) for nid in xmrs.nodeids()
                for role, val in sorted(xmrs.args(nid).items(),
                                        key=lambda i: rargname_sortkey(i[0])))
    return list(filter(argmatch, all_args))
Exemplo n.º 2
0
 def to_triples(self, short_pred=True, properties=True):
     """
     Encode the Eds as triples suitable for PENMAN serialization.
     """
     node_triples, edge_triples = [], []
     # sort nodeids just so top var is first
     nodes = sorted(self.nodes(), key=lambda n: n.nodeid != self.top)
     for node in nodes:
         nid = node.nodeid
         pred = node.pred.short_form() if short_pred else node.pred.string
         node_triples.append((nid, 'predicate', pred))
         if node.lnk:
             node_triples.append((nid, 'lnk', '"{}"'.format(str(node.lnk))))
         if node.carg:
             node_triples.append((nid, 'carg', '"{}"'.format(node.carg)))
         if properties:
             if node.cvarsort is not None:
                 node_triples.append((nid, 'type', node.cvarsort))
             props = node.properties
             node_triples.extend((nid, p, v) for p, v in props.items())
         edge_triples.extend(
             (nid, rargname, tgt)
             for rargname, tgt in sorted(
                 self.edges(nid).items(),
                 key=lambda x: rargname_sortkey(x[0])
             )
         )
     return node_triples + edge_triples
Exemplo n.º 3
0
def get_outbound_args(xmrs, nodeid, allow_unbound=True):
    tgts = set(intrinsic_variables(xmrs))
    tgts.update(xmrs.labels())
    tgts.update(hc.hi for hc in xmrs.hcons())
    return [(nodeid, role, val)
            for role, val in sorted(xmrs.outgoing_args(nodeid).items(),
                                    key=lambda r_v: rargname_sortkey(r_v[0]))
            if allow_unbound or val in tgts]
Exemplo n.º 4
0
def _serialize_eds(e, properties, pretty_print, show_status,
                   predicate_modifiers):
    if not isinstance(e, Eds):
        e = Eds.from_xmrs(e, predicate_modifiers=predicate_modifiers)
    # do something predictable for empty EDS
    if len(e.nodeids()) == 0:
        return '{:\n}' if pretty_print else '{:}'

    # determine if graph is connected
    g = {n: set() for n in e.nodeids()}
    for n in e.nodeids():
        for tgt in e.edges(n).values():
            g[n].add(tgt)
            g[tgt].add(n)
    nidgrp = _bfs(g, start=e.top)

    status = ''
    if show_status and nidgrp != set(e.nodeids()):
        status = ' (fragmented)'
    delim = '\n' if pretty_print else ' '
    connected = ' ' if pretty_print else ''
    disconnected = '|' if show_status else ' '

    return eds.format(
        top=e.top + ':' if e.top is not None else ':',
        flag=status,
        delim=delim,
        ed_list=delim.join(
            ed.format(
                membership=connected if n.nodeid in nidgrp else disconnected,
                id=n.nodeid,
                pred=n.pred.short_form(),
                lnk=str(n.lnk) if n.lnk else '',
                carg=carg.format(constant=n.carg) if n.carg else '',
                props=proplist.format(
                    varsort=n.cvarsort + ' ' if n.cvarsort else '',
                    proplist=', '.join(
                        '{} {}'.format(k, v)
                        for k, v in sorted(n.properties.items())
                    )
                ) if properties and n.sortinfo else '',
                dep_list=(', '.join(
                    '{} {}'.format(rargname, tgt)
                    for rargname, tgt in sorted(
                        e.edges(n.nodeid).items(),
                        key=lambda x: rargname_sortkey(x[0])
                    )
                )),
            )
            for n in e.nodes()
        ),
        enddelim='\n' if pretty_print else ''
    )
Exemplo n.º 5
0
def select_args(xmrs, nodeid=None, rargname=None, value=None):
    """
    Return a list of triples (nodeid, rargname, value) for all arguments
    matching *nodeid*, *rargname*, and/or *value* values. If none match,
    return an empty list.
    """
    argmatch = lambda a: (
        (nodeid is None or a[0] == nodeid) and
        (rargname is None or a[1].upper() == rargname.upper()) and
        (value is None or a[2] == value))
    all_args = ((nid, role, val) for nid in xmrs.nodeids()
                for role, val in sorted(xmrs.args(nid).items(),
                                        key=lambda i: rargname_sortkey(i[0])))
    return list(filter(argmatch, all_args))
Exemplo n.º 6
0
def _serialize_eds(e, properties=False, pretty_print=True, **kwargs):
    if not isinstance(e, Eds):
        e = Eds.from_xmrs(e)
    # do something predictable for empty EDS
    if len(e.nodeids()) == 0:
        return '{:\n}' if pretty_print else '{:}'

    # determine if graph is connected
    g = {n: set() for n in e.nodeids()}
    for n in e.nodeids():
        for rargname, tgt in e.edges(n).items():
            g[n].add(tgt)
            g[tgt].add(n)
    nidgrp = _bfs(g, start=e.top)

    delim = '\n' if pretty_print else ' '
    connected = ' ' if pretty_print else ''

    return eds.format(
        top=e.top + ':' if e.top is not None else ':',
        flag='' if nidgrp == set(e.nodeids()) else ' (fragmented)',
        delim=delim,
        ed_list=delim.join(
            ed.format(
                membership=connected if n.nodeid in nidgrp else '|',
                id=n.nodeid,
                pred=n.pred.short_form(),
                lnk=str(n.lnk) if n.lnk else '',
                carg=carg.format(constant=n.carg) if n.carg else '',
                props=proplist.format(
                    varsort=n.cvarsort + ' ' if n.cvarsort else '',
                    proplist=', '.join(
                        '{} {}'.format(k, v)
                        for k, v in sorted(n.properties.items())
                    )
                ) if properties and n.sortinfo else '',
                dep_list=(', '.join(
                    '{} {}'.format(rargname, tgt)
                    for rargname, tgt in sorted(
                        e.edges(n.nodeid).items(),
                        key=lambda x: rargname_sortkey(x[0])
                    )
                )),
            )
            for n in e.nodes()
        ),
        enddelim='\n' if pretty_print else ''
    )
Exemplo n.º 7
0
def get_outbound_args(xmrs, nodeid, allow_unbound=True):
    warnings.warn(
        'get_outbound_args() is deprecated; '
        'try xmrs.outgoing_args(nodeid)',
        DeprecationWarning
    )
    tgts = set(intrinsic_variables(xmrs))
    tgts.update(xmrs.labels())
    tgts.update(hc.hi for hc in xmrs.hcons())
    return [
        (nodeid, role, val)
        for role, val in sorted(
            xmrs.outgoing_args(nodeid).items(),
            key=lambda r_v: rargname_sortkey(r_v[0])
        )
        if allow_unbound or val in tgts
    ]
Exemplo n.º 8
0
def select_args(xmrs, nodeid=None, rargname=None, value=None):
    """
    Return a list of triples (nodeid, rargname, value) for all arguments
    matching *nodeid*, *rargname*, and/or *value* values. If none match,
    return an empty list.
    """
    argmatch = lambda a: ((nodeid is None or a[0] == nodeid) and
                          (rargname is None or
                           a[1].upper() == rargname.upper()) and
                          (value is None or a[2] == value))
    all_args = (
        (nid, role, val)
        for nid in xmrs.nodeids()
        for role, val in sorted(
            xmrs.args(nid).items(),
            key=lambda i: rargname_sortkey(i[0])
        )
    )
    return list(filter(argmatch, all_args))