def test_from_dmrs(dogs_bark): from delphin import dmrs m = mrs.MRS(**dogs_bark) d = dmrs.DMRS( top=10002, index=10002, nodes=[ dmrs.Node(10000, 'udef_q'), dmrs.Node(10001, '_dog_n_1', type='x', properties={'NUM': 'pl'}), dmrs.Node(10002, '_bark_v_1', type='e', properties={'TENSE': 'pres'})], links=[ dmrs.Link(10000, 10001, 'RSTR', 'H'), dmrs.Link(10002, 10001, 'ARG1', 'NEQ')]) _m = mrs.from_dmrs(d) # Issue #248 labels = set(ep.label for ep in _m.rels) hcons = {hc.hi: hc.lo for hc in _m.hcons} assert _m.top not in labels assert _m.top in hcons assert hcons[_m.top] in labels # ensure equivalency assert mrs.is_isomorphic(m, _m) # try with no hook d.top = None d.index = None # it won't be isomorphic, just check for errors _m = mrs.from_dmrs(d) assert _m.top is None assert _m.index is None assert len(_m.rels) == 3 assert len(_m.hcons) == 1
def eds_to_dmrs(eds_rep): node_counter = 10000 new_node_ids = {} dmrs_nodes = [] for node in eds_rep.nodes: new_node_ids[node.id] = node_counter dmrs_nodes.append(d_dmrs.Node(node_counter, node.predicate, node.type, node.properties, node.carg, node.lnk, node.surface, node.base)) node_counter += 1 dmrs_links = [d_dmrs.Link(new_node_ids[edge[0]], new_node_ids[edge[2]], edge[1], "") for edge in eds_rep.edges] return d_dmrs.DMRS(new_node_ids[eds_rep.top], new_node_ids[eds_rep.top], dmrs_nodes, dmrs_links, eds_rep.lnk, eds_rep.surface, eds_rep.identifier)
def test_scopal_arguments(self, dogs_bark): d = dmrs.DMRS() assert d.scopal_arguments() == {} d = dmrs.DMRS(**dogs_bark) assert d.scopal_arguments() == { 10000: [], 10001: [('RSTR', 'qeq', 10002)], 10002: [] } _, scopes = d.scopes() scopemap = {} for lbl, nodes in scopes.items(): for node in nodes: scopemap[node.id] = lbl assert d.scopal_arguments(scopes=scopes) == { 10000: [], 10001: [('RSTR', 'qeq', scopemap[10002])], 10002: [] }
def test_arguments(self, dogs_bark): d = dmrs.DMRS() assert d.arguments() == {} assert d.arguments('h') == {} d = dmrs.DMRS(**dogs_bark) assert d.arguments() == { 10000: [('ARG1', 10002)], 10001: [('RSTR', 10002)], 10002: [] } assert d.arguments('h') == { 10000: [], 10001: [('RSTR', 10002)], 10002: [] } assert d.arguments('xei') == { 10000: [('ARG1', 10002)], 10001: [], 10002: [] }
def test__init__(self, dogs_bark): d = dmrs.DMRS() assert d.top is None assert d.index is None assert d.nodes == [] assert d.links == [] d = dmrs.DMRS(**dogs_bark) assert d.top == 10000 assert d.index == 10000 assert len(d.nodes) == 3 assert d.nodes[0].predicate == '_bark_v_1_rel' assert d.nodes[1].predicate == 'udef_q_rel' assert d.nodes[2].predicate == '_dog_n_1_rel' assert len(d.links) == 2 assert d.links[0].role == 'ARG1' assert d.links[1].role == 'RSTR' # make sure the old way of marking top still works dogs_bark2 = dict(dogs_bark) dogs_bark2['links'].append(dmrs.Link(0, dogs_bark['top'], None, 'H')) del dogs_bark2['top'] d2 = dmrs.DMRS(**dogs_bark2) assert d.top == d2.top
def from_mrs(m, representative_priority=None): """ Create a DMRS by converting from MRS *m*. In order for MRS to DMRS conversion to work, the MRS must satisfy the intrinsic variable property (see :func:`delphin.mrs.has_intrinsic_variable_property`). Args: m: the input MRS representative_priority: a function for ranking candidate representative nodes; see :func:`scope.representatives` Returns: DMRS Raises: DMRSError when conversion fails. """ hcmap = {hc.hi: hc for hc in m.hcons} reps = scope.representatives(m, priority=representative_priority) # EP id to node id map; create now to keep ids consistent id_to_nid = {ep.id: i for i, ep in enumerate(m.rels, dmrs.FIRST_NODE_ID)} iv_to_nid = { ep.iv: id_to_nid[ep.id] for ep in m.rels if not ep.is_quantifier() } top = _mrs_get_top(m.top, hcmap, reps, id_to_nid) # some bad MRSs have an INDEX that isn't the ARG0 of any EP, so # make sure it exists first index = None if m.index and m.index in iv_to_nid: index = iv_to_nid[m.index] nodes = _mrs_to_nodes(m, id_to_nid) links = _mrs_to_links(m, hcmap, reps, iv_to_nid, id_to_nid) return dmrs.DMRS(top=top, index=index, nodes=nodes, links=links, lnk=m.lnk, surface=m.surface, identifier=m.identifier)