Beispiel #1
0
    def test_invert(self, mini_amr):
        m = Model()
        assert m.invert(('a', ':ARG0', 'b'))       == ('b', ':ARG0-of', 'a')
        assert m.invert(('a', ':ARG0-of', 'b'))    == ('b', ':ARG0', 'a')
        assert m.invert(('a', ':consist-of', 'b')) == ('b', ':consist', 'a')
        assert m.invert(('a', ':mod', 'b'))        == ('b', ':mod-of', 'a')
        assert m.invert(('a', ':domain', 'b'))     == ('b', ':domain-of', 'a')
        # # without :
        # assert m.invert(('a', 'ARG0', 'b'))        == ('b', 'ARG0-of', 'a')
        # assert m.invert(('a', 'ARG0-of', 'b'))     == ('b', 'ARG0', 'a')

        m = Model.from_dict(mini_amr)
        assert m.invert(('a', ':ARG0', 'b'))       == ('b', ':ARG0-of', 'a')
        assert m.invert(('a', ':ARG0-of', 'b'))    == ('b', ':ARG0', 'a')
        assert m.invert(('a', ':consist-of', 'b')) == ('b', ':consist-of-of', 'a')
        assert m.invert(('a', ':mod', 'b'))        == ('b', ':mod-of', 'a')
        assert m.invert(('a', ':domain', 'b'))     == ('b', ':domain-of', 'a')
Beispiel #2
0
def _interpret_node(t: Node, variables: Set[Variable], model: Model):
    has_concept = False
    triples = []
    epidata = {}
    var, edges = t
    for role, target in edges:
        epis: List[Epidatum] = []

        if role == '/':
            role = CONCEPT_ROLE
            has_concept = True
        elif '~' in role:
            role, _, alignment = role.partition('~')
            epis.append(RoleAlignment.from_string(alignment))

        # atomic targets
        if is_atomic(target):
            # remove any alignments
            if target and '~' in target:
                if target.startswith('"'):
                    # need to handle alignments on strings differently
                    # because strings may contain ~ inside the quotes
                    pivot = target.rindex('"') + 1
                    if pivot < len(target):
                        epis.append(Alignment.from_string(target[pivot:]))
                        target = target[:pivot]
                else:
                    target, _, alignment = target.partition('~')
                    epis.append(Alignment.from_string(alignment))
            triple = (var, role, target)
            if model.is_role_inverted(role):
                if target in variables:
                    triple = model.invert(triple)
                else:
                    logger.warning('cannot deinvert attribute: %r', triple)
            triples.append(triple)
            epidata[triple] = epis
        # nested nodes
        else:
            triple = model.deinvert((var, role, target[0]))
            triples.append(triple)
            epidata[triple] = epis

            # recurse to nested nodes
            epidata[triple].append(Push(target[0]))
            _, _triples, _epis = _interpret_node(target, variables, model)
            triples.extend(_triples)
            epidata.update(_epis)
            epidata[triples[-1]].append(POP)

    if not has_concept:
        instance = (var, CONCEPT_ROLE, None)
        triples.insert(0, instance)
        epidata[instance] = []

    return var, triples, epidata
Beispiel #3
0
def _interpret_node(t: Node, variables: Set[Variable], model: Model):
    has_concept = False
    triples = []
    epidata = {}
    var, edges = t
    for role, target in edges:
        epis: List[Epidatum] = []

        if role == '/':
            role = CONCEPT_ROLE
            has_concept = True
        elif '~' in role:
            role, _, alignment = role.partition('~')
            epis.append(RoleAlignment.from_string(alignment))

        # atomic targets
        if is_atomic(target):
            if target and '~' in target:
                target, _, alignment = target.partition('~')
                epis.append(Alignment.from_string(alignment))
            triple = (var, role, target)
            if model.is_role_inverted(role):
                if target in variables:
                    triple = model.invert(triple)
                else:
                    logger.warning('cannot deinvert attribute: %r', triple)
            triples.append(triple)
            epidata[triple] = epis
        # nested nodes
        else:
            triple = model.deinvert((var, role, target[0]))
            triples.append(triple)
            epidata[triple] = epis

            # recurse to nested nodes
            epidata[triple].append(Push(target[0]))
            _, _triples, _epis = _interpret_node(target, variables, model)
            triples.extend(_triples)
            epidata.update(_epis)
            epidata[triples[-1]].append(POP)

    if not has_concept:
        instance = (var, CONCEPT_ROLE, None)
        triples.insert(0, instance)
        epidata[instance] = []

    return var, triples, epidata
Beispiel #4
0
def _interpret_node(t: Node, variables: Set[Variable], model: Model):
    has_concept = False
    triples = []
    epidata = []
    var, edges = t
    for role, target in edges:
        epis: List[Epidatum] = []

        role, role_epis = _process_role(role)
        epis.extend(role_epis)
        has_concept |= role == CONCEPT_ROLE

        # atomic targets
        if is_atomic(target):
            target, target_epis = _process_atomic(target)
            epis.extend(target_epis)
            triple = (var, role, target)
            if model.is_role_inverted(role):
                if target in variables:
                    triple = model.invert(triple)
                else:
                    logger.warning('cannot deinvert attribute: %r', triple)
            triples.append(triple)
            epidata.append((triple, epis))
        # nested nodes
        else:
            triple = model.deinvert((var, role, target[0]))
            triples.append(triple)

            epis.append(Push(target[0]))
            epidata.append((triple, epis))

            # recurse to nested nodes
            _, _triples, _epis = _interpret_node(target, variables, model)
            triples.extend(_triples)
            _epis[-1][1].append(POP)  # POP from last triple of nested node
            epidata.extend(_epis)

    if not has_concept:
        instance = (var, CONCEPT_ROLE, None)
        triples.insert(0, instance)
        epidata.append((instance, []))

    return var, triples, epidata