Beispiel #1
0
def test_appears_inverted():
    g = codec.decode('''
        (a / alpha
           :ARG0 (b / beta)
           :ARG1-of (g / gamma))''')
    assert not appears_inverted(g, ('a', ':instance', 'alpha'))
    assert not appears_inverted(g, ('a', ':ARG0', 'b'))
    assert appears_inverted(g, ('g', ':ARG1', 'a'))
Beispiel #2
0
def test_issue_87():
    # https://github.com/goodmami/penman/issues/87

    # The duplicate triple (i, :ARG0, c) below means the graph is bad
    # so the output is not guaranteed. Just check for errors.
    g = codec.decode('(c / company :ARG0-of (i / insure-02 :ARG0 c))')
    appears_inverted(g, ('i', ':ARG0', 'c'))
    codec.encode(g)
    g = codec.decode('(c / company :ARG0-of i :ARG0-of (i / insure-02))')
    appears_inverted(g, ('i', ':ARG0', 'c'))
    codec.encode(g)
Beispiel #3
0
def test_issue_47():
    # https://github.com/goodmami/penman/issues/47
    g = codec.decode('''
        (a / alpha
           :ARG0 (b / beta)
           :ARG1 (g / gamma
                    :ARG0 (d / delta)
                    :ARG1-of (e / epsilon)
                    :ARG1-of b))''')
    assert not appears_inverted(g, ('a', ':ARG0', 'b'))
    assert not appears_inverted(g, ('g', ':ARG0', 'd'))
    assert appears_inverted(g, ('e', ':ARG1', 'g'))
    assert appears_inverted(g, ('b', ':ARG1', 'g'))
Beispiel #4
0
def reify_edges(g: Graph, model: Model) -> Graph:
    """
    Reify all edges in *g* that have reifications in *model*.

    Args:
        g: a :class:`~penman.graph.Graph` object
        model: a model defining reifications
    Returns:
        A new :class:`~penman.graph.Graph` object with reified edges.
    Example:
        >>> from penman.codec import PENMANCodec
        >>> from penman.models.amr import model
        >>> from penman.transform import reify_edges
        >>> codec = PENMANCodec(model=model)
        >>> g = codec.decode('(c / chapter :mod 7)')
        >>> g = reify_edges(g, model)
        >>> print(codec.encode(g))
        (c / chapter
           :ARG1-of (_ / have-mod-91
                       :ARG2 7))
    """
    vars = g.variables()
    if model is None:
        model = Model()
    new_epidata = dict(g.epidata)
    new_triples: List[BasicTriple] = []
    for triple in g.triples:
        if model.is_role_reifiable(triple[1]):
            in_triple, node_triple, out_triple = model.reify(triple, vars)
            if appears_inverted(g, triple):
                in_triple, out_triple = out_triple, in_triple
            new_triples.extend((in_triple, node_triple, out_triple))
            var = node_triple[0]
            vars.add(var)
            # manage epigraphical markers
            new_epidata[in_triple] = [Push(var)]
            old_epis = new_epidata.pop(triple) if triple in new_epidata else []
            node_epis, out_epis = _edge_markers(old_epis)
            new_epidata[node_triple] = node_epis
            new_epidata[out_triple] = out_epis
            # we don't know where to put the final POP without configuring
            # the tree; maybe this should be a tree operation?
        else:
            new_triples.append(triple)
    g = Graph(new_triples,
              epidata=new_epidata,
              metadata=g.metadata)
    logger.info('Reified edges: %s', g)
    return g