def _process(t): """Encode tree *t* and return the string.""" # tree transformations if normalize_options['make_variables']: t.reset_variables(normalize_options['make_variables']) if normalize_options['canonicalize_roles']: t = transform.canonicalize_roles(t, model) if normalize_options['rearrange'] == 'canonical': layout.rearrange(t, key=model.canonical_order) elif normalize_options['rearrange'] == 'random': layout.rearrange(t, key=model.random_order) g = layout.interpret(t, model) # graph transformations if normalize_options['reify_edges']: g = transform.reify_edges(g, model) if normalize_options['dereify_edges']: g = transform.dereify_edges(g, model) if normalize_options['reify_attributes']: g = transform.reify_attributes(g) if normalize_options['indicate_branches']: g = transform.indicate_branches(g, model) if triples: return codec.format_triples(g.triples, indent=bool( format_options.get('indent', True))) else: return codec.encode(g, **format_options)
def test_reify_attributes(): decode = def_codec.decode norm = lambda g: reify_attributes(g) encode = lambda g: def_codec.encode(g, indent=None) g = norm(decode('(a / alpha :mod 5)')) assert encode(g) == '(a / alpha :mod (_ / 5))' g = norm(decode('(a / alpha :mod~1 5~2)')) assert encode(g) == '(a / alpha :mod~1 (_ / 5~2))'
def _process_in(t, model, normalize_options): """Encode tree *t* and return the string.""" # tree transformations if normalize_options['canonicalize_roles']: t = transform.canonicalize_roles(t, model) g = layout.interpret(t, model) # graph transformations if normalize_options['reify_edges']: g = transform.reify_edges(g, model) if normalize_options['dereify_edges']: g = transform.dereify_edges(g, model) if normalize_options['reify_attributes']: g = transform.reify_attributes(g) if normalize_options['indicate_branches']: g = transform.indicate_branches(g, model) return g
def test_issue_35(): # https://github.com/goodmami/penman/issues/35 # don't re-encode; these (presumably) bad graphs probably won't # round-trip without changes. Changes may be predictable, but I # don't want to test and guarantee some particular output g = amr_codec.decode('(a / alpha :mod b :mod (b / beta))') g = reify_edges(g, amr_model) assert g.triples == [('a', ':instance', 'alpha'), ('_', ':ARG1', 'a'), ('_', ':instance', 'have-mod-91'), ('_', ':ARG2', 'b'), ('_2', ':ARG1', 'a'), ('_2', ':instance', 'have-mod-91'), ('_2', ':ARG2', 'b'), ('b', ':instance', 'beta')] g = amr_codec.decode('(a / alpha :mod 7 :mod 7))') g = reify_attributes(g) assert g.triples == [('a', ':instance', 'alpha'), ('a', ':mod', '_'), ('_', ':instance', '7'), ('a', ':mod', '_2'), ('_2', ':instance', '7')]