def test_dereify(self, mini_amr): # (a :ARG1-of (_ / age-01 :ARG2 b)) -> (a :age b) t1 = ('_', ':instance', 'have-mod-91') t1b = ('_', ':instance', 'chase-01') t2 = ('_', ':ARG1', 'a') t3 = ('_', ':ARG2', 'b') m = Model() with pytest.raises(TypeError): m.dereify(t1) with pytest.raises(TypeError): m.dereify(t1, t2) with pytest.raises(ModelError): m.dereify(t1, t2, t3) m = Model.from_dict(mini_amr) assert m.dereify(t1, t2, t3) == ('a', ':mod', 'b') assert m.dereify(t1, t3, t2) == ('a', ':mod', 'b') with pytest.raises(ModelError): m.dereify(t1b, t2, t3)
def _dereify_agenda(g: Graph, model: Model) -> _Dereification: alns = alignments(g) agenda: _Dereification = {} fixed: Set[Target] = set([g.top]) inst: Dict[Variable, BasicTriple] = {} other: Dict[Variable, List[BasicTriple]] = {} for triple in g.triples: var, role, tgt = triple if role == CONCEPT_ROLE: inst[var] = triple else: fixed.add(tgt) if var not in other: other[var] = [triple] else: other[var].append(triple) for var, instance in inst.items(): if (var not in fixed and len(other.get(var, [])) == 2 and model.is_concept_dereifiable(instance[2])): # passed initial checks # now figure out which other edge is the first one first, second = other[var] if get_pushed_variable(g, second) == var: first, second = second, first try: dereified = model.dereify(instance, first, second) except ModelError: pass else: # migrate epidata epidata: List[Epidatum] = [] if instance in alns: aln = alns[instance] epidata.append( RoleAlignment(aln.indices, prefix=aln.prefix)) epidata.extend(epi for epi in g.epidata[second] if not isinstance(epi, RoleAlignment)) agenda[var] = (first, dereified, epidata) return agenda