def test_decode_alignments(self): g = decode('(a / alpha~1)') assert g.triples == [ ('a', ':instance', 'alpha'), ] assert surface.alignments(g) == { ('a', ':instance', 'alpha'): surface.Alignment((1,)), } assert surface.role_alignments(g) == {} assert decode('(a / alpha~1)') == decode('(a / alpha ~1)') g = decode('(a :ARG~e.1,2 b)') assert g.triples == [ ('a', ':instance', None), ('a', ':ARG', 'b'), ] assert surface.alignments(g) == {} assert surface.role_alignments(g) == { ('a', ':ARG', 'b'): surface.RoleAlignment((1, 2), prefix='e.'), } # https://github.com/goodmami/penman/issues/50 g = decode('(a :ARG1 "str~ing" :ARG2 "str~ing"~1)') assert g.triples == [ ('a', ':instance', None), ('a', ':ARG1', '"str~ing"'), ('a', ':ARG2', '"str~ing"'), ] assert surface.alignments(g) == { ('a', ':ARG2', '"str~ing"'): surface.Alignment((1,)), } assert surface.role_alignments(g) == {}
def test_alignments(isi_aligned): g = codec.decode('(a :ARG~1 (b / beta~2))') assert alignments(g) == { ('b', ':instance', 'beta'): Alignment((2, )), } assert role_alignments(g) == { ('a', ':ARG', 'b'): RoleAlignment((1, )), } assert codec.encode(g, indent=None) == '(a :ARG~1 (b / beta~2))' g = codec.decode(isi_aligned[0]) assert alignments(g) == { ('d', ':instance', 'drive-01'): Alignment((2, ), prefix='e.'), ('h', ':instance', 'he'): Alignment((1, ), prefix='e.'), ('c', ':instance', 'care-04'): Alignment((3, ), prefix='e.'), } assert role_alignments(g) == {} assert codec.encode(g) == ('(d / drive-01~e.2\n' ' :ARG0 (h / he~e.1)\n' ' :manner (c / care-04~e.3\n' ' :polarity -))')
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