def test_construct(self): # minimum is a nodeid and a pred with pytest.raises(TypeError): Node() with pytest.raises(TypeError): Node(10000) n = Node(10000, spred('_dog_n_rel')) assert n.nodeid == 10000 assert n.pred == '_dog_n_rel'
def test_from_triples(self): assert Dmrs.from_triples([]) == Dmrs() d1 = Dmrs.from_triples([(10, 'predicate', '_rain_v_1'), (0, 'top', 10)]) # by default nodeids get remapped from 10000 d2 = Dmrs(nodes=[Node(10000, sp('"_rain_v_1_rel"'))], links=[Link(0, 10000, None, 'H')]) assert d1 == d2 d1 = Dmrs.from_triples([ (10, 'predicate', '_rain_v_1'), ]) assert d1 == d2 d1 = Dmrs.from_triples([ (10, 'predicate', '_rain_v_1'), (20, 'predicate', '_or_c'), (30, 'predicate', '_snow_v_1'), (0, 'top', 20), (20, 'L-INDEX-NEQ', 10), (20, 'L-HNDL-HEQ', 10), (20, 'R-INDEX-NEQ', 30), (20, 'R-HNDL-HEQ', 30) ]) d2 = Dmrs(nodes=[ Node(10000, sp('"_rain_v_1_rel"')), Node(10001, sp('_or_c_rel')), Node(10002, sp('"_snow_v_1_rel"')) ], links=[ Link(0, 10001, None, 'H'), Link(10001, 10000, 'L-INDEX', 'NEQ'), Link(10001, 10000, 'L-HNDL', 'HEQ'), Link(10001, 10002, 'R-INDEX', 'NEQ'), Link(10001, 10002, 'R-HNDL', 'HEQ') ]) assert d1 == d2
def test_cvarsort(self): n = Node(10000, spred('_dog_n_rel')) assert n.cvarsort == None n.cvarsort = 'x' assert n.cvarsort == 'x' assert n.sortinfo == dict([(CVARSORT, 'x')]) n = Node(10000, spred('_run_v_rel'), sortinfo=dict([(CVARSORT, 'e')])) assert n.cvarsort == 'e'
def test_lnk(self): n = Node(10000, spred('_dog_n_rel')) assert n.lnk == None assert n.cfrom == -1 assert n.cto == -1 n = Node(10000, spred('_dog_n_rel'), lnk=Lnk.charspan(0, 1)) assert n.lnk == Lnk.charspan(0, 1) assert n.cfrom == 0 assert n.cto == 1
def eds_kim_probably_sleeps(): return eds.Eds(top='e9', nodes=[ Node('_1', Pred.stringpred('proper_q_rel')), Node('x3', Pred.stringpred('named_rel'), carg='Kim'), Node('e9', Pred.stringpred('_probable_a_1_rel')), Node('e2', Pred.stringpred('_sleep_v_1_rel')), ], edges=[('_1', 'BV', 'x3'), ('e9', 'ARG1', 'e2'), ('e2', 'ARG1', 'x3')])
def eds_dogs_chase_Kim(): return eds.Eds(top='e2', nodes=[ Node('_1', Pred.stringpred('udef_q_rel')), Node('x4', Pred.stringpred('"_dog_n_1_rel"')), Node('e2', Pred.stringpred('"_chase_v_1_rel"')), Node('_2', Pred.stringpred('proper_q_rel')), Node('x6', Pred.stringpred('named_rel'), carg='Kim') ], edges=[('_1', 'BV', 'x4'), ('_2', 'BV', 'x6'), ('e2', 'ARG1', 'x4'), ('e2', 'ARG2', 'x6')])
def test_sortinfo(self): n = Node(10000, spred('_dog_n_rel')) assert len(n.sortinfo) == 0 n = Node(10000, spred('_dog_n_rel'), sortinfo=[(CVARSORT, 'x')]) assert len(n.sortinfo) == 1 n = Node(10000, spred('_dog_n_rel'), sortinfo=[(CVARSORT, 'x'), ('PER', '3')]) assert len(n.sortinfo) == 2 n2 = Node(10001, spred('_cat_n_rel'), sortinfo=dict([(CVARSORT,'x'), ('PER','3')])) assert n.sortinfo == n2.sortinfo
def test_properties(self): n = Node(10000, spred('_dog_n_rel')) assert len(n.properties) == 0 n = Node(10000, spred('_dog_n_rel'), sortinfo=[(CVARSORT, 'x')]) assert len(n.properties) == 0 n = Node(10000, spred('_dog_n_rel'), sortinfo=[(CVARSORT, 'x'), ('PER', '3')]) assert len(n.properties) == 1 n2 = Node(10001, spred('_cat_n_rel'), sortinfo=dict([(CVARSORT, 'x'), ('PER', '3')])) assert n.properties == n2.properties
def test_to_triples(self): assert Dmrs().to_triples() == [] x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'))]) assert x.to_triples() == [(10, 'predicate', '_rain_v_1'), (10, 'cvarsort', 'u')] x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'), {'cvarsort': 'e'})], links=[Link(0, 10, None, 'H')]) assert x.to_triples() == [(10, 'predicate', '_rain_v_1'), (10, 'cvarsort', 'e'), (0, 'top', 10)] assert x.to_triples(properties=False) == [(10, 'predicate', '_rain_v_1'), (0, 'top', 10)]
def from_dict(cls, d): """ Decode a dictionary, as from :meth:`to_dict`, into an Eds object. """ makepred, charspan = Pred.surface_or_abstract, Lnk.charspan top = d.get('top') nodes, edges = [], [] for nid, node in d.get('nodes', {}).items(): props = node.get('properties', {}) if 'type' in node: props[CVARSORT] = node['type'] if not props: props = None lnk = None if 'lnk' in node: lnk = charspan(node['lnk']['from'], node['lnk']['to']) nodes.append( Node( nodeid=nid, pred=makepred(node['label']), sortinfo=props, lnk=lnk, carg=node.get('carg') ) ) edges.extend( (nid, rargname, tgtnid) for rargname, tgtnid in node.get('edges', {}).items() ) nodes.sort(key=lambda n: (n.cfrom, -n.cto)) return cls(top, nodes=nodes, edges=edges)
def from_triples(cls, triples): lnk, surface, identifier = None, None, None nids, nd, edges = [], {}, [] for src, rel, tgt in triples: if src not in nd: nids.append(src) nd[src] = {'pred': None, 'lnk': None, 'carg': None, 'si': []} if rel == 'predicate': nd[src]['pred'] = Pred.string_or_grammar_pred(tgt) elif rel == 'lnk': cfrom, cto = tgt.strip('"<>').split(':') nd[src]['lnk'] = Lnk.charspan(int(cfrom), int(cto)) elif rel == 'carg': if (tgt[0], tgt[-1]) == ('"', '"'): tgt = tgt[1:-1] nd[src]['carg'] = tgt elif rel == 'type': nd[src]['si'].append((CVARSORT, tgt)) elif rel.islower(): nd[src]['si'].append((rel, tgt)) else: edges.append((src, rel, tgt)) nodes = [ Node(nodeid=nid, pred=nd[nid]['pred'], sortinfo=nd[nid]['si'], lnk=nd[nid]['lnk'], carg=nd[nid]['carg']) for nid in nids ] top = nids[0] if nids else None return cls(top=top, nodes=nodes, edges=edges)
def from_dict(cls, d): makepred, charspan = Pred.string_or_grammar_pred, Lnk.charspan top = d.get('top') nodes, edges = [], [] for nid, node in d.get('nodes', {}).items(): props = node.get('properties', {}) if 'type' in node: props[CVARSORT] = node['type'] if not props: props = None lnk = None if 'lnk' in node: lnk = charspan(node['lnk']['from'], node['lnk']['to']) nodes.append( Node( nodeid=nid, pred=makepred(node['label']), sortinfo=props, lnk=lnk, carg=node.get('carg') ) ) edges.extend( (nid, rargname, tgtnid) for rargname, tgtnid in node.get('edges', {}).items() ) nodes.sort(key=lambda n: (n.cfrom, -n.cto)) return cls(top, nodes=nodes, edges=edges)
def test_to_dict(self): assert Dmrs().to_dict() == {'nodes': [], 'links': []} x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'))]) assert x.to_dict() == { 'nodes': [{ 'nodeid': 10, 'predicate': '_rain_v_1', 'sortinfo': { 'cvarsort': UNKNOWNSORT } }], 'links': [] } x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'), {'cvarsort': 'e'})], links=[Link(0, 10, None, 'H')]) assert x.to_dict() == { 'nodes': [{ 'nodeid': 10, 'predicate': '_rain_v_1', 'sortinfo': { 'cvarsort': 'e' } }], 'links': [{ 'from': 0, 'to': 10, 'rargname': None, 'post': 'H' }] } assert x.to_dict(properties=False) == { 'nodes': [{ 'nodeid': 10, 'predicate': '_rain_v_1' }], 'links': [{ 'from': 0, 'to': 10, 'rargname': None, 'post': 'H' }] }
def test_single_node(self): # basic, one Node, no TOP x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'))]) check_xmrs(x, None, None, None, 1, 0, 0, 2) # variables don't need to be created predictably, but it's nice # to get the expected values for simple cases assert x.label(10) == 'h1' assert x.ep(10).iv == 'u2' # now with cvarsort x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'), {'cvarsort': 'e'})]) check_xmrs(x, None, None, None, 1, 0, 0, 2) assert x.label(10) == 'h1' assert x.ep(10).iv == 'e2' # now with TOP x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'), {'cvarsort': 'e'})], links=[Link(0, 10, None, 'H')]) check_xmrs(x, 'h0', None, None, 1, 1, 0, 3) assert x.label(10) == 'h1' assert x.ep(10).iv == 'e2'
def from_xmrs(cls, xmrs): eps = xmrs.eps() deps = _find_dependencies(xmrs, eps) ids = _unique_ids(eps, deps) root = _find_root(xmrs) if root is not None: root = ids[root] nodes = [Node(ids[n.nodeid], *n[1:]) for n in make_nodes(xmrs)] edges = [(ids[a], rarg, ids[b]) for a, deplist in deps.items() for rarg, b in deplist] return cls(top=root, nodes=nodes, edges=edges)
def eds_it_rains(): return eds.Eds( top='e2', nodes=[ Node( 'e2', Pred.surface('"_rain_v_1_rel"'), sortinfo={ 'SF': 'prop', 'TENSE': 'pres', 'MOOD': 'indicative', 'PROG': '-', 'PERF': '-', CVARSORT: 'e'}, lnk=Lnk.charspan(3, 9) ) ], edges=[] )
def test_from_dict(self): assert Dmrs.from_dict({}) == Dmrs() d1 = Dmrs.from_dict({ 'nodes': [{ 'nodeid': 10, 'predicate': '_rain_v_1' }], 'links': [{ 'from': 0, 'to': 10, 'rargname': None, 'post': 'H' }], }) d2 = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'))], links=[Link(0, 10, None, 'H')]) assert d1 == d2
def from_xmrs(cls, xmrs, predicate_modifiers=False, **kwargs): """ Instantiate an Eds from an Xmrs (lossy conversion). Args: xmrs (:class:`~delphin.mrs.xmrs.Xmrs`): Xmrs instance to convert from predicate_modifiers (function, bool): function that is called as `func(xmrs, deps)` after finding the basic dependencies (`deps`), returning a mapping of predicate-modifier dependencies; the form of `deps` and the returned mapping are `{head: [(role, dependent)]}`; if *predicate_modifiers* is `True`, the function is created using :func:`non_argument_modifiers` as: `non_argument_modifiers(role="ARG1", connecting=True); if *predicate_modifiers* is `False`, only the basic dependencies are returned """ eps = xmrs.eps() deps = _find_basic_dependencies(xmrs, eps) # if requested, find additional dependencies not captured already if predicate_modifiers is True: func = non_argument_modifiers(role='ARG1', only_connecting=True) addl_deps = func(xmrs, deps) elif predicate_modifiers is False or predicate_modifiers is None: addl_deps = {} elif hasattr(predicate_modifiers, '__call__'): addl_deps = predicate_modifiers(xmrs, deps) else: raise TypeError('a boolean or callable is required') for nid, deplist in addl_deps.items(): deps.setdefault(nid, []).extend(deplist) ids = _unique_ids(eps, deps) root = _find_root(xmrs) if root is not None: root = ids[root] nodes = [Node(ids[n.nodeid], *n[1:]) for n in make_nodes(xmrs)] edges = [(ids[a], rarg, ids[b]) for a, deplist in deps.items() for rarg, b in deplist] return cls(top=root, nodes=nodes, edges=edges)
def test_subgraph(self): nodes = [Node(1,Pred.surface('verb'))] links = [Link(0,1,'','H')] graph = Dmrs(nodes,links) new_graph = graph.subgraph([1]) assert len(new_graph._eps) == 1
def _make_nodedata(d): nid = d[0] node = Node(nid, pred=d[1], sortinfo=d[4], lnk=d[2], carg=d[3]) edges = [(nid, rarg, tgt) for rarg, tgt in d[5]] return (node, edges)