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_edges_default_codec(): decode = def_codec.decode norm = lambda g: reify_edges(g, def_model) 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-of (b / beta))')) assert encode(g) == '(a / alpha :mod-of (b / beta))'
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')]
def test_reify_edges_amr_codec(): decode = amr_codec.decode norm = lambda g: reify_edges(g, amr_model) encode = lambda g: amr_codec.encode(g, indent=None) g = norm(decode('(a / alpha :mod 5)')) assert encode(g) == '(a / alpha :ARG1-of (_ / have-mod-91 :ARG2 5))' g = norm(decode('(a / alpha :mod-of (b / beta))')) assert encode( g) == '(a / alpha :ARG2-of (_ / have-mod-91 :ARG1 (b / beta)))' g = norm(decode('(a / alpha :mod-of (b / beta :polarity -))')) assert encode(g) == ( '(a / alpha :ARG2-of (_ / have-mod-91 ' ':ARG1 (b / beta :ARG1-of (_2 / have-polarity-91 :ARG2 -))))') g = norm(decode('(a / alpha :mod-of~1 (b / beta~2 :polarity -))')) assert encode(g) == ( '(a / alpha :ARG2-of (_ / have-mod-91~1 ' ':ARG1 (b / beta~2 :ARG1-of (_2 / have-polarity-91 :ARG2 -))))')