def test_preds(self): sp = Pred.surface x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'}), (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) # works like xmrs.pred(), but with a list of nodeids ps = x.preds([10, 11]) assert [p.string for p in ps] == ['_v_v_rel', '_n_n_rel'] # order should be preserved ps = x.preds([12, 11]) assert [p.string for p in ps] == ['q_q_rel', '_n_n_rel'] # KeyError on bad nodeid with pytest.raises(KeyError): x.preds([10, 11, 13]) # with no arguments, all preds are returned in original order ps = x.preds() assert [p.string for p in ps] == ['_v_v_rel', '_n_n_rel', 'q_q_rel'] x = Xmrs( eps=[ (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}), (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'}), (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}) ], hcons=[('h6', 'qeq', 'h5')] ) ps = x.preds() assert [p.string for p in ps] == ['q_q_rel', '_v_v_rel', '_n_n_rel']
def test_hcon(self): x = Xmrs() with pytest.raises(TypeError): x.hcon() with pytest.raises(KeyError): x.hcon('h0') x.add_hcons([('h0', 'qeq', 'h1')]) assert x.hcon('h0') == ('h0', 'qeq', 'h1') with pytest.raises(KeyError): x.hcon('h1')
def test_ep(self): sp = Pred.surface x = Xmrs() with pytest.raises(TypeError): x.ep() with pytest.raises(KeyError): x.ep(10) x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.ep(10)[1] == sp('_n_n_rel')
def test_preds(self): sp = Pred.stringpred x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'}), (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) # works like xmrs.pred(), but with a list of nodeids ps = x.preds([10, 11]) assert [p.string for p in ps] == ['_v_v_rel', '_n_n_rel'] # order should be preserved ps = x.preds([12, 11]) assert [p.string for p in ps] == ['q_q_rel', '_n_n_rel'] # KeyError on bad nodeid with pytest.raises(KeyError): x.preds([10, 11, 13]) # with no arguments, all preds are returned in original order ps = x.preds() assert [p.string for p in ps] == ['_v_v_rel', '_n_n_rel', 'q_q_rel'] x = Xmrs( eps=[ (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}), (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'}), (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}) ], hcons=[('h6', 'qeq', 'h5')] ) ps = x.preds() assert [p.string for p in ps] == ['q_q_rel', '_v_v_rel', '_n_n_rel']
def test_hcons(self): x = Xmrs() assert len(x.hcons()) == 0 x.add_hcons([('h0', 'qeq', 'h1')]) hcs = x.hcons() assert len(hcs) == 1 assert hcs[0] == ('h0', 'qeq', 'h1') x.add_hcons([('h3', 'qeq', 'h5')]) hcs = sorted(x.hcons()) # hcons are not stored in sorted order assert len(hcs) == 2 assert hcs[1] == ('h3', 'qeq', 'h5')
def test_select_nodes(): def nids(nodes): return [n.nodeid for n in nodes] assert nids(query.select_nodes(Xmrs())) == [] assert nids(query.select_nodes(Xmrs(), nodeid=10000)) == [] assert nids( query.select_nodes(m)) == [10000, 10001, 10002, 10003, 10004, 10005] assert nids(query.select_nodes(m, nodeid=10000)) == [10000] assert nids(query.select_nodes(m, pred='"_chase_v_1_rel"')) == [10003] assert nids(query.select_nodes(m, nodeid=10003, pred='"_chase_v_1_rel"')) == [10003] assert nids(query.select_nodes(m, nodeid=10003, pred='"_dog_n_1_rel"')) == []
def test_labels(self): sp = Pred.stringpred # same as Xmrs.labels() but with a list of nodeids x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'}), (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) assert x.labels([10, 12]) == ['h3', 'h7'] with pytest.raises(KeyError): x.labels([12, 13]) # no argument means all labels, in EP order assert x.labels() == ['h3', 'h5', 'h7']
def test_find_argument_target(): assert query.find_argument_target(m, 10000, 'RSTR') == 10002 assert query.find_argument_target(m, 10001, 'ARG1') == 10002 assert query.find_argument_target(m, 10003, 'ARG2') == 10005 # test CARGs and unbound vars: "Abrams tried." m2 = Xmrs( top='h0', eps=[ EP(10000, sp('proper_q_rel'), 'h4', { 'ARG0': 'x3', 'RSTR': 'h5' }), EP(10001, sp('named_rel'), 'h7', { 'ARG0': 'x3', 'CARG': 'Abrams' }), EP(10002, sp('"_try_v_1_rel"'), 'h1', { 'ARG0': 'e2', 'ARG1': 'x3', 'ARG2': 'i9' }), ], hcons=[qeq('h0', 'h1'), qeq('h5', 'h7')], ) assert query.find_argument_target(m2, 10001, 'CARG') == 'Abrams' assert query.find_argument_target(m2, 10002, 'ARG2') == 'i9'
def test___contains__(self): x = Xmrs() assert 'x1' not in x assert 10000 not in x assert 0 not in x assert None not in x x = Xmrs(top='h0') assert 'h0' in x assert 10000 not in x assert 0 not in x x = read('[ TOP: h0 RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ] > ]') assert all(_ in x for _ in ('h0', 'h1', 'e2', 10000)) == True assert 10001 not in x assert '10000' not in x assert '_v_v_rel' not in x assert Pred.surface('_v_v_rel') not in x
def test_ep(self): sp = Pred.stringpred x = Xmrs() with pytest.raises(TypeError): x.ep() with pytest.raises(KeyError): x.ep(10) x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.ep(10)[1] == sp('_n_n_rel')
def test_select_eps(): def nids(eps): return [ep.nodeid for ep in eps] assert nids(query.select_eps(Xmrs())) == [] assert nids(query.select_eps(Xmrs(), nodeid=10000)) == [] assert nids( query.select_eps(m)) == [10000, 10001, 10002, 10003, 10004, 10005] assert nids(query.select_eps(m, nodeid=10005)) == [10005] assert nids(query.select_eps(m, iv='x3')) == [10000, 10002] assert nids(query.select_eps(m, label='h7')) == [10001, 10002] assert nids(query.select_eps(m, pred='udef_q_rel')) == [10000, 10004] assert nids(query.select_eps(m, iv='x3', pred='udef_q_rel')) == [10000] assert nids(query.select_eps(m, iv='x3', label='h7')) == [10002] assert nids(query.select_eps(m, iv='x3', label='h7', pred='udef_q_rel')) == []
def test_args(self): # return the argument dict of a nodeid, or KeyError for missing nodeid x = Xmrs( eps=[(10, Pred.stringpred('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'})] ) assert x.args(10) == {'ARG0': 'e2', 'ARG1': 'x4'} with pytest.raises(KeyError): x.args(11) # retrieved dict does not edit original x.args(10)['ARG1'] = 'x6' assert x.args(10)['ARG1'] == 'x4' # return empty arg dict for EP without specified args: x = Xmrs(eps=[(10, Pred.stringpred('_v_v_rel'), 'h3')]) assert x.args(10) == {}
def test_pred(self): x = Xmrs() # KeyError on bad nodeid with pytest.raises(KeyError): x.pred(10) # but otherwise preds can be retrieved by nodeid x.add_eps([(10, Pred.surface('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.pred(10).string == '_n_n_rel'
def test_args(self): # return the argument dict of a nodeid, or KeyError for missing nodeid x = Xmrs( eps=[(10, Pred.stringpred('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'})] ) assert x.args(10) == {'ARG0': 'e2', 'ARG1': 'x4'} with pytest.raises(KeyError): x.args(11) # retrieved dict does not edit original x.args(10)['ARG1'] = 'x6' assert x.args(10)['ARG1'] == 'x4'
def test_hcons(self): x = Xmrs() assert len(x.hcons()) == 0 x.add_hcons([('h0', 'qeq', 'h1')]) hcs = x.hcons() assert len(hcs) == 1 assert hcs[0] == ('h0', 'qeq', 'h1') x.add_hcons([('h3', 'qeq', 'h5')]) hcs = sorted(x.hcons()) # hcons are not stored in sorted order assert len(hcs) == 2 assert hcs[1] == ('h3', 'qeq', 'h5')
def test_empty(self): x = Xmrs() assert x.top is None assert x.index is None assert x.xarg is None assert len(x.nodeids()) == 0 assert len(x.eps()) == 0 assert len(x.hcons()) == 0 assert len(x.icons()) == 0 assert len(x.variables()) == 0
def test_pred(self): x = Xmrs() # KeyError on bad nodeid with pytest.raises(KeyError): x.pred(10) # but otherwise preds can be retrieved by nodeid x.add_eps([(10, Pred.stringpred('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.pred(10).string == '_n_n_rel'
def test_select_nodeids(): assert query.select_nodeids(Xmrs()) == [] assert query.select_nodeids(m) == [ 10000, 10001, 10002, 10003, 10004, 10005 ] assert query.select_nodeids(m, iv='e2') == [10003] assert query.select_nodeids(m, iv='e4') == [] assert query.select_nodeids(m, pred='"_chase_v_1_rel"') == [10003] assert query.select_nodeids(m, pred='udef_q_rel') == [10000, 10004] assert query.select_nodeids(m, label='h7') == [10001, 10002] assert query.select_nodeids(m, label='h0') == [] assert query.select_nodeids(m, pred='udef_q_rel', label='h4') == [10000] assert query.select_nodeids(m, iv='x3', pred='"_cat_n_1_rel"') == []
def test_select_icons(): def l_rs(ics): return sorted(ics, key=lambda ic: int(ic[0][1:])) assert l_rs(query.select_icons(Xmrs())) == [] assert l_rs(query.select_icons(m)) == [('e2', 'topic', 'x9')] assert l_rs(query.select_icons(m, left='e2')) == [('e2', 'topic', 'x9')] assert l_rs(query.select_icons(m, right='x9')) == [('e2', 'topic', 'x9')] assert l_rs(query.select_icons(m, relation='topic')) == [('e2', 'topic', 'x9')] assert l_rs(query.select_icons(m, relation='focus')) == [] assert l_rs(query.select_icons(m, left='e2', right='x9')) == [('e2', 'topic', 'x9')]
def test_empty(self): x = Xmrs() assert x.top is None assert x.index is None assert x.xarg is None assert len(x.nodeids()) == 0 assert len(x.eps()) == 0 assert len(x.hcons()) == 0 assert len(x.icons()) == 0 assert len(x.variables()) == 0
def test_select_hcons(): def hi_los(hcs): return sorted(((hc[0], hc[2]) for hc in hcs), key=lambda hc: int(hc[0][1:])) assert hi_los(query.select_hcons(Xmrs())) == [] assert hi_los(query.select_hcons(m)) == [('h0', 'h1'), ('h5', 'h7'), ('h11', 'h13')] assert hi_los(query.select_hcons(m, hi='h0')) == [('h0', 'h1')] assert hi_los(query.select_hcons(m, lo='h0')) == [] assert hi_los(query.select_hcons(m, lo='h1')) == [('h0', 'h1')] assert hi_los(query.select_hcons(m, relation='qeq')) == [('h0', 'h1'), ('h5', 'h7'), ('h11', 'h13')] assert hi_los(query.select_hcons(m, hi='h0', lo='h1')) == [('h0', 'h1')] assert hi_los(query.select_hcons(m, hi='h0', lo='h7')) == []
def test_outgoing_args(self): sp = Pred.stringpred # Outgoing args are those that, from some start node, go to # another in some way. These ways include: # regular variable args x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2'}), (11, sp('_a_a_rel'), 'h5', {'ARG0': 'e4', 'ARG1': 'e2'}) ] ) assert x.outgoing_args(10) == {} # no outgoing args assert x.outgoing_args(11) == {'ARG1': 'e2'} # label equality args x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'h5'}), (11, sp('_v_v_rel'), 'h5', {'ARG0': 'e4'}) ], ) assert x.outgoing_args(10) == {'ARG1': 'h5'} assert x.outgoing_args(11) == {} # no outgoing args # handle constraints x = Xmrs( eps=[ (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) assert x.outgoing_args(11) == {} # no outgoing args assert x.outgoing_args(12) == {'RSTR': 'h6'} # not shared ARG0 # basic label equality is not "outgoing" x = Xmrs( eps=[ (11, sp('_nearly_x_deg_rel'), 'h5', {'ARG0': 'e4'}), (12, sp('q_q_rel'), 'h5', {'ARG0': 'x6', 'RSTR': 'h7'}) ], ) assert x.outgoing_args(11) == {} assert x.outgoing_args(12) == {} # RSTR would be if HCONS was there
def test_incoming_args(self): # incoming_args() is like the reverse of outgoing_args(), but # now it's many-to-one instead of one-to-many sp = Pred.stringpred x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2'}), (11, sp('_a_a_rel'), 'h5', {'ARG0': 'e4', 'ARG1': 'e2'}) ] ) assert x.incoming_args(10) == {11: {'ARG1': 'e2'}} assert x.incoming_args(11) == {} # no incoming args # label equality args x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'h5'}), (11, sp('_v_v_rel'), 'h5', {'ARG0': 'e4'}) ], ) assert x.incoming_args(10) == {} # no incoming args assert x.incoming_args(11) == {10: {'ARG1': 'h5'}} # handle constraints x = Xmrs( eps=[ (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) assert x.incoming_args(11) == {12: {'RSTR': 'h6'}} assert x.incoming_args(12) == {} # no incoming args # basic label equality is not "incoming" x = Xmrs( eps=[ (11, sp('_nearly_x_deg_rel'), 'h5', {'ARG0': 'e4'}), (12, sp('q_q_rel'), 'h5', {'ARG0': 'x6', 'RSTR': 'h7'}) ], ) assert x.incoming_args(11) == {} assert x.incoming_args(12) == {}
def test_labels(self): sp = Pred.surface # same as Xmrs.labels() but with a list of nodeids x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'x4'}), (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) assert x.labels([10, 12]) == ['h3', 'h7'] with pytest.raises(KeyError): x.labels([12, 13]) # no argument means all labels, in EP order assert x.labels() == ['h3', 'h5', 'h7']
def test_select_args(): assert query.select_args(Xmrs()) == [] assert query.select_args(m) == [ (10000, 'ARG0', 'x3'), (10000, 'RSTR', 'h5'), (10001, 'ARG0', 'e8'), (10001, 'ARG1', 'x3'), (10002, 'ARG0', 'x3'), (10003, 'ARG0', 'e2'), (10003, 'ARG1', 'x3'), (10003, 'ARG2', 'x9'), (10004, 'ARG0', 'x9'), (10004, 'RSTR', 'h11'), (10005, 'ARG0', 'x9') ] assert query.select_args(m, nodeid=10000) == [(10000, 'ARG0', 'x3'), (10000, 'RSTR', 'h5')] assert query.select_args(m, rargname='ARG1') == [(10001, 'ARG1', 'x3'), (10003, 'ARG1', 'x3')] assert query.select_args(m, value='x9') == [(10003, 'ARG2', 'x9'), (10004, 'ARG0', 'x9'), (10005, 'ARG0', 'x9')] assert query.select_args(m, nodeid=10000, rargname='RSTR') == [(10000, 'RSTR', 'h5')] assert query.select_args(m, nodeid=10000, value='h5') == [(10000, 'RSTR', 'h5')] assert query.select_args(m, nodeid=10000, rargname='ARG2') == []
def test_select_links(): def ends(ls): return [(l.start, l.end) for l in ls] assert ends(query.select_links(Xmrs())) == [] assert ends(query.select_links(m)) == [(0, 10003), (10000, 10002), (10001, 10002), (10003, 10002), (10003, 10005), (10004, 10005)] assert ends(query.select_links(m, start=0)) == [(0, 10003)] assert ends(query.select_links(m, start=10003)) == [(10003, 10002), (10003, 10005)] assert ends(query.select_links(m, end=10002)) == [(10000, 10002), (10001, 10002), (10003, 10002)] assert ends(query.select_links(m, rargname='ARG1')) == [(10001, 10002), (10003, 10002)] assert ends(query.select_links(m, post='H')) == [(0, 10003), (10000, 10002), (10004, 10005)] assert ends(query.select_links(m, start=10003, rargname='ARG1')) == [(10003, 10002)] assert ends(query.select_links(m, end=10002, post='H')) == [(10000, 10002)]
def test_icons(self): x = Xmrs() with pytest.raises(KeyError): x.icons(left='x5') assert len(x.icons()) == 0 x.add_icons([('e2', 'topic', 'e5')]) ics = x.icons() assert len(ics) == 1 assert ics[0] == ('e2', 'topic', 'e5') x.add_icons([('e2', 'focus', 'x4'), ('x7', 'info-str', 'x9')]) ics = x.icons(left='e2') # icons are not stored sorted assert len(ics) == 2 assert set(ics) == {('e2', 'topic', 'e5'), ('e2', 'focus', 'x4')} with pytest.raises(KeyError): assert len(x.icons(left='e5')) == 0 with pytest.raises(KeyError): assert len(x.icons(left='x4')) == 0 with pytest.raises(KeyError): assert len(x.icons(left='x9')) == 0 assert len(x.icons(left='x7')) == 1
def test_icons(self): x = Xmrs() with pytest.raises(KeyError): x.icons(left='x5') assert len(x.icons()) == 0 x.add_icons([('e2', 'topic', 'e5')]) ics = x.icons() assert len(ics) == 1 assert ics[0] == ('e2', 'topic', 'e5') x.add_icons([('e2', 'focus', 'x4'), ('x7', 'info-str', 'x9')]) ics = x.icons(left='e2') # icons are not stored sorted assert len(ics) == 2 assert set(ics) == {('e2', 'topic', 'e5'), ('e2', 'focus', 'x4')} with pytest.raises(KeyError): assert len(x.icons(left='e5')) == 0 assert len(x.icons(left='x4')) == 0 assert len(x.icons(left='x9')) == 0 assert len(x.icons(left='x7')) == 1
def test_hcon(self): x = Xmrs() with pytest.raises(TypeError): x.hcon() with pytest.raises(KeyError): x.hcon('h0') x.add_hcons([('h0', 'qeq', 'h1')]) assert x.hcon('h0') == ('h0', 'qeq', 'h1') with pytest.raises(KeyError): x.hcon('h1')
def test_add_hcons(self): x = Xmrs() with pytest.raises(XmrsError): x.add_hcons([('h0')]) # only hi x.add_hcons([('h0', 'qeq')]) # only hi and relation # hi, relation, and lo (the minimum, but probably max, too) x.add_hcons([('h0', 'qeq', 'h1')]) assert len(x.hcons()) == 1 hc = x.hcon('h0') assert hc[0] == 'h0' assert hc[1] == 'qeq' assert hc[2] == 'h1' # cannot have more than one hcons with the same hi variable with pytest.raises(XmrsError): x.add_hcons([('h0', 'qeq', 'h2')]) assert len(x.hcons()) == 1
def test_add_hcons(self): x = Xmrs() with pytest.raises(XmrsError): x.add_hcons([('h0')]) # only hi with pytest.raises(XmrsError): x.add_hcons([('h0', 'qeq')]) # only hi and relation # hi, relation, and lo (the minimum, but probably max, too) x.add_hcons([('h0', 'qeq', 'h1')]) assert len(x.hcons()) == 1 hc = x.hcon('h0') assert hc[0] == 'h0' assert hc[1] == 'qeq' assert hc[2] == 'h1' # cannot have more than one hcons with the same hi variable with pytest.raises(XmrsError): x.add_hcons([('h0', 'qeq', 'h2')]) assert len(x.hcons()) == 1
def test_nodeids(self): sp = Pred.stringpred x = Xmrs(eps=[(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.nodeids() == [10] assert x.nodeids(ivs=['x4']) == [10] assert x.nodeids(quantifier=False) == [10] assert x.nodeids(quantifier=None) == [10] assert x.nodeids(quantifier=True) == [] x.add_eps([(11, sp('_the_q_rel'), 'h5', {'ARG0': 'x4', 'RSTR': 'h6'})]) x.add_hcons([('h6', 'qeq', 'h3')]) assert sorted(x.nodeids()) == [10, 11] assert sorted(x.nodeids(ivs=['x4'])) == [10, 11] assert sorted(x.nodeids(ivs=['x4'], quantifier=True)) == [11] assert sorted(x.nodeids(ivs=['x4'], quantifier=False)) == [10]
def test_outgoing_args(self): sp = Pred.surface # Outgoing args are those that, from some start node, go to # another in some way. These ways include: # regular variable args x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2'}), (11, sp('_a_a_rel'), 'h5', {'ARG0': 'e4', 'ARG1': 'e2'}) ] ) assert x.outgoing_args(10) == {} # no outgoing args assert x.outgoing_args(11) == {'ARG1': 'e2'} # label equality args x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'h5'}), (11, sp('_v_v_rel'), 'h5', {'ARG0': 'e4'}) ], ) assert x.outgoing_args(10) == {'ARG1': 'h5'} assert x.outgoing_args(11) == {} # no outgoing args # handle constraints x = Xmrs( eps=[ (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) assert x.outgoing_args(11) == {} # no outgoing args assert x.outgoing_args(12) == {'RSTR': 'h6'} # not shared ARG0 # basic label equality is not "outgoing" x = Xmrs( eps=[ (11, sp('_nearly_x_deg_rel'), 'h5', {'ARG0': 'e4'}), (12, sp('q_q_rel'), 'h5', {'ARG0': 'x6', 'RSTR': 'h7'}) ], ) assert x.outgoing_args(11) == {} assert x.outgoing_args(12) == {} # RSTR would be if HCONS was there
def test_label(self): # retrieve the label for a single ep, or KeyError if no such nodeid x = Xmrs(eps=[(10, Pred.surface('_v_v_rel'), 'h3', {'ARG0': 'e2'})]) assert x.label(10) == 'h3' with pytest.raises(KeyError): x.label(11)
def test_add_icons(self): x = Xmrs() with pytest.raises(XmrsError): x.add_icons([('x0')]) # only left x.add_icons([('x0', 'topic')]) # only left and relation # hi, relation, and lo (the minimum, but probably max, too) x.add_icons([('x0', 'topic', 'x1')]) assert len(x.icons()) == 1 ics = x.icons('x0') assert len(ics) == 1 assert ics[0][0] == 'x0' assert ics[0][1] == 'topic' assert ics[0][2] == 'x1' # can have more than one icons with the same left variable x.add_icons([('x0', 'focus', 'x2')]) assert len(x.icons()) == 2
def test_variables_and_properties(self): sp = Pred.surface # variables can be passed in with properties x = Xmrs(vars={'x1':{'PERS':'3','NUM':'sg'}, 'e2':{'SF':'prop'}}) assert len(x.variables()) == 2 assert x.properties('x1')['PERS'] == '3' assert x.properties('e2')['SF'] == 'prop' # when there's no EP, you cannot retrieve properties via a nodeid with pytest.raises(KeyError): x.properties(10) # but when an EP with an ARG0 exists, you can x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x1'})]) assert x.properties(10) == {'PERS': '3', 'NUM': 'sg'} # variables can also be inferred from structural things x = Xmrs(top='h0', index='e2', xarg='e5') assert set(x.variables()) == {'h0', 'e2', 'e5'} x = Xmrs(eps=[(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert set(x.variables()) == {'x4', 'h3'} x = Xmrs(hcons=[('h0', 'qeq', 'h1')]) assert set(x.variables()) == {'h0', 'h1'} x = Xmrs(icons=[('x4', 'focus', 'x6')]) assert set(x.variables()) == {'x4', 'x6'} # variables can be passed in and inferred x = Xmrs(icons=[('x4', 'focus', 'x6')], vars={'x4': {'PERS': '3'}}) assert set(x.variables()) == {'x4', 'x6'} assert x.properties('x4') == {'PERS': '3'} assert x.properties('x6') == {} # adding things later doesn't reset properties x = Xmrs(vars={'x4': {'PERS': '3'}}) x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert set(x.variables()) == {'x4', 'h3'} assert x.properties('x4') == {'PERS': '3'} # and properties can't be added via properties() x.properties('x4')['NUM'] = 'sg' assert x.properties('x4') == {'PERS': '3'} # TODO: how do we add properties? # constants are not variables x = Xmrs(eps=[(10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'CARG': '"dog"'})]) assert set(x.variables()) == {'e2', 'h3'} # Constants don't need to be the CARG role, and don't need # quotes (but if there are quotes, even var-looking things are # constants). pyDelphin differs from the LKB in the first # respect, but also maybe in the second. x = Xmrs(eps=[(10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': '1', 'ARG2': '"x5"'})]) assert set(x.variables()) == {'h3', 'e2'}
def test_label(self): # retrieve the label for a single ep, or KeyError if no such nodeid x = Xmrs(eps=[(10, Pred.stringpred('_v_v_rel'), 'h3', {'ARG0': 'e2'})]) assert x.label(10) == 'h3' with pytest.raises(KeyError): x.label(11)
def test_nodeid(self): sp = Pred.stringpred x = Xmrs() with pytest.raises(KeyError): x.nodeid('e2') x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.nodeid('x4') == 10 assert x.nodeid('x4', quantifier=False) == 10 assert x.nodeid('x4', quantifier=None) == 10 assert x.nodeid('x4', quantifier=True) == None x.add_eps([(11, sp('_the_q_rel'), 'h5', {'ARG0': 'x4', 'RSTR': 'h6'})]) x.add_hcons([('h6', 'qeq', 'h3')]) assert x.nodeid('x4') == 10 assert x.nodeid('x4', quantifier=False) == 10 assert x.nodeid('x4', quantifier=True) == 11
def test_add_eps(self): x = Xmrs() # only nodeid with pytest.raises(XmrsError): x.add_eps([(10000,)]) assert len(x.eps()) == 0 # nodeid and pred with pytest.raises(XmrsError): x.add_eps([(10000, Pred.surface('_v_v_rel'))]) assert len(x.eps()) == 0 # nodeid, pred, and label (the minimum) x.add_eps([(10000, Pred.surface('_v_v_rel'), 'h1')]) # make sure it was entered correctly and is unchanged assert len(x.eps()) == 1 assert x.eps()[0][0] == 10000 ep = x.ep(10000) assert isinstance(ep[1], Pred) and ep[1].string == '_v_v_rel' assert ep[2] == 'h1' # nodeid, pred, label, and argdict x = Xmrs() x.add_eps([(10000, Pred.surface('_v_v_rel'), 'h1', {})]) assert len(x.eps()) == 1 assert x.eps()[0][0] == 10000 ep = x.ep(10000) assert ep[0] == 10000 assert isinstance(ep[1], Pred) and ep[1].string == '_v_v_rel' assert ep[2] == 'h1' assert ep[3] == {} # cannot have more than one ep with the same nodeid with pytest.raises(XmrsError): x.add_eps([(10000, Pred.surface('_n_n_rel'), 'h3', {})]) assert len(x.eps()) == 1
def test_xarg(self): x = Xmrs(xarg='e0') assert x.xarg == 'e0'
def test_eps(self): sp = Pred.stringpred x = Xmrs() assert len(x.eps()) == 0 x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) eps = x.eps() assert len(eps) == 1 assert eps[0][0] == 10 assert eps[0][1] == sp('_n_n_rel') assert eps[0][2] == 'h3' assert eps[0][3] == {'ARG0': 'x4'} x.add_eps([ (11, sp('_q_q_rel'), 'h5', {'ARG0': 'x4', 'RSTR': 'h6'}), (12, sp('_v_v_rel'), 'h7', {'ARG0': 'e2', 'ARG1': 'x4'}) ]) eps = x.eps() assert len(eps) == 3 assert eps[0][1] == sp('_n_n_rel') assert eps[1][1] == sp('_q_q_rel') assert eps[2][1] == sp('_v_v_rel') # make sure order is preserved x = Xmrs() x.add_eps([ (12, sp('_v_v_rel'), 'h7', {'ARG0':'e2', 'ARG1': 'x4'}), (11, sp('_q_q_rel'), 'h5', {'ARG0':'x4', 'RSTR': 'h6'}), (10, sp('_n_n_rel'), 'h3', {'ARG0':'x4'}) ]) eps = x.eps() assert eps[0][1] == sp('_v_v_rel') assert eps[1][1] == sp('_q_q_rel') assert eps[2][1] == sp('_n_n_rel') # only get with given nodeids and in that order eps = x.eps(nodeids=[10, 11]) assert eps[0][1] == sp('_n_n_rel') assert eps[1][1] == sp('_q_q_rel') # but asking for a non-existing one raises a KeyError with pytest.raises(KeyError): x.eps(nodeids=[10, 13])
def test_nodeid(self): sp = Pred.surface x = Xmrs() with pytest.raises(KeyError): x.nodeid('e2') x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.nodeid('x4') == 10 assert x.nodeid('x4', quantifier=False) == 10 assert x.nodeid('x4', quantifier=None) == 10 assert x.nodeid('x4', quantifier=True) == None x.add_eps([(11, sp('_the_q_rel'), 'h5', {'ARG0': 'x4', 'RSTR': 'h6'})]) x.add_hcons([('h6', 'qeq', 'h3')]) assert x.nodeid('x4') == 10 assert x.nodeid('x4', quantifier=False) == 10 assert x.nodeid('x4', quantifier=True) == 11
def test_nodeids(self): sp = Pred.surface x = Xmrs(eps=[(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert x.nodeids() == [10] assert x.nodeids(ivs=['x4']) == [10] assert x.nodeids(quantifier=False) == [10] assert x.nodeids(quantifier=None) == [10] assert x.nodeids(quantifier=True) == [] x.add_eps([(11, sp('_the_q_rel'), 'h5', {'ARG0': 'x4', 'RSTR': 'h6'})]) x.add_hcons([('h6', 'qeq', 'h3')]) assert sorted(x.nodeids()) == [10, 11] assert sorted(x.nodeids(ivs=['x4'])) == [10, 11] assert sorted(x.nodeids(ivs=['x4'], quantifier=True)) == [11] assert sorted(x.nodeids(ivs=['x4'], quantifier=False)) == [10]
def test_add_eps(self): x = Xmrs() # only nodeid with pytest.raises(XmrsError): x.add_eps([(10000,)]) assert len(x.eps()) == 0 # nodeid and pred with pytest.raises(XmrsError): x.add_eps([(10000, Pred.stringpred('_v_v_rel'))]) assert len(x.eps()) == 0 # nodeid, pred, and label (the minimum) x.add_eps([(10000, Pred.stringpred('_v_v_rel'), 'h1')]) # make sure it was entered correctly and is unchanged assert len(x.eps()) == 1 assert x.eps()[0][0] == 10000 ep = x.ep(10000) assert isinstance(ep[1], Pred) and ep[1].string == '_v_v_rel' assert ep[2] == 'h1' # nodeid, pred, label, and argdict x = Xmrs() x.add_eps([(10000, Pred.stringpred('_v_v_rel'), 'h1', {})]) assert len(x.eps()) == 1 assert x.eps()[0][0] == 10000 ep = x.ep(10000) assert ep[0] == 10000 assert isinstance(ep[1], Pred) and ep[1].string == '_v_v_rel' assert ep[2] == 'h1' assert ep[3] == {} # cannot have more than one ep with the same nodeid with pytest.raises(XmrsError): x.add_eps([(10000, Pred.stringpred('_n_n_rel'), 'h3', {})]) assert len(x.eps()) == 1
def test_incoming_args(self): # incoming_args() is like the reverse of outgoing_args(), but # now it's many-to-one instead of one-to-many sp = Pred.surface x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2'}), (11, sp('_a_a_rel'), 'h5', {'ARG0': 'e4', 'ARG1': 'e2'}) ] ) assert x.incoming_args(10) == {11: {'ARG1': 'e2'}} assert x.incoming_args(11) == {} # no incoming args # label equality args x = Xmrs( eps=[ (10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': 'h5'}), (11, sp('_v_v_rel'), 'h5', {'ARG0': 'e4'}) ], ) assert x.incoming_args(10) == {} # no incoming args assert x.incoming_args(11) == {10: {'ARG1': 'h5'}} # handle constraints x = Xmrs( eps=[ (11, sp('_n_n_rel'), 'h5', {'ARG0': 'x4'}), (12, sp('q_q_rel'), 'h7', {'ARG0': 'x4', 'RSTR': 'h6'}) ], hcons=[('h6', 'qeq', 'h5')] ) assert x.incoming_args(11) == {12: {'RSTR': 'h6'}} assert x.incoming_args(12) == {} # no incoming args # basic label equality is not "incoming" x = Xmrs( eps=[ (11, sp('_nearly_x_deg_rel'), 'h5', {'ARG0': 'e4'}), (12, sp('q_q_rel'), 'h5', {'ARG0': 'x6', 'RSTR': 'h7'}) ], ) assert x.incoming_args(11) == {} assert x.incoming_args(12) == {}
def test_eps(self): sp = Pred.surface x = Xmrs() assert len(x.eps()) == 0 x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) eps = x.eps() assert len(eps) == 1 assert eps[0][0] == 10 assert eps[0][1] == sp('_n_n_rel') assert eps[0][2] == 'h3' assert eps[0][3] == {'ARG0': 'x4'} x.add_eps([ (11, sp('_q_q_rel'), 'h5', {'ARG0': 'x4', 'RSTR': 'h6'}), (12, sp('_v_v_rel'), 'h7', {'ARG0': 'e2', 'ARG1': 'x4'}) ]) eps = x.eps() assert len(eps) == 3 assert eps[0][1] == sp('_n_n_rel') assert eps[1][1] == sp('_q_q_rel') assert eps[2][1] == sp('_v_v_rel') # make sure order is preserved x = Xmrs() x.add_eps([ (12, sp('_v_v_rel'), 'h7', {'ARG0':'e2', 'ARG1': 'x4'}), (11, sp('_q_q_rel'), 'h5', {'ARG0':'x4', 'RSTR': 'h6'}), (10, sp('_n_n_rel'), 'h3', {'ARG0':'x4'}) ]) eps = x.eps() assert eps[0][1] == sp('_v_v_rel') assert eps[1][1] == sp('_q_q_rel') assert eps[2][1] == sp('_n_n_rel') # only get with given nodeids and in that order eps = x.eps(nodeids=[10, 11]) assert eps[0][1] == sp('_n_n_rel') assert eps[1][1] == sp('_q_q_rel') # but asking for a non-existing one raises a KeyError with pytest.raises(KeyError): x.eps(nodeids=[10, 13])
def test_is_connected(self): # empty Xmrs objects cannot be checked for connectedness with pytest.raises(XmrsError): Xmrs().is_connected() with pytest.raises(XmrsError): Xmrs(top='h0').is_connected() with pytest.raises(XmrsError): Xmrs(hcons=[('h0', 'qeq', 'h1')]).is_connected() # just a pred is fine (even without ARG0) x = Xmrs(eps=[(10, Pred.surface('_v_v_rel'), 'h1', {})]) assert x.is_connected() == True x = Xmrs(eps=[(10, Pred.surface('_v_v_rel'), 'h1', {'ARG0':'e2'})]) assert x.is_connected() == True # disconnected top is fine x = read('[ TOP: h0 RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ] > ]') assert x.is_connected() == True # and a broken HCONS is ok, too? (maybe revise later) x = read('[ TOP: h0 RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ] > ' ' HCONS: < h0 qeq h2 > ]') assert x.is_connected() == True # two disconnected EPs are definitely bad x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ] > ]') assert x.is_connected() == False # if they're linked, they're fine again x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ] > ]') # they can link to previous EPs, too x = read('[ TOP: h0 ' ' RELS: < [ _n_n_rel LBL: h3 ARG0: x4 ]' ' [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ] > ]') assert x.is_connected() == True # they can also be linked by LBL x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ]' ' [ _v_v_rel LBL: h1 ARG0: e4 ] > ]') # or by handle arguments x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: h3 ]' ' [ _v_v_rel LBL: h3 ARG0: e4 ] > ]') assert x.is_connected() == True # or by qeqs x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: h3 ]' ' [ _v_v_rel LBL: h5 ARG0: e4 ] > ' ' HCONS: < h3 qeq h5 > ]') assert x.is_connected() == True # simply being connected to something isn't enough x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ]' ' [ _v_v_rel LBL: h5 ARG0: e6 ARG1: x8 ]' ' [ _n_n_rel LBL: h7 ARG0: x8 ] > ]') assert x.is_connected() == False # some linkage needs to connect disparate subgraphs (even weird ones) x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ]' ' [ _v_v_rel LBL: h5 ARG0: e2 ARG1: x8 ]' ' [ _n_n_rel LBL: h7 ARG0: x8 ] > ]') assert x.is_connected() == True
def test_variables_and_properties(self): sp = Pred.stringpred # variables can be passed in with properties x = Xmrs(vars={'x1':{'PERS':'3','NUM':'sg'}, 'e2':{'SF':'prop'}}) assert len(x.variables()) == 2 assert x.properties('x1')['PERS'] == '3' assert x.properties('e2')['SF'] == 'prop' # when there's no EP, you cannot retrieve properties via a nodeid with pytest.raises(KeyError): x.properties(10) # but when an EP with an ARG0 exists, you can x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x1'})]) assert x.properties(10) == {'PERS': '3', 'NUM': 'sg'} # variables can also be inferred from structural things x = Xmrs(top='h0', index='e2', xarg='e5') assert set(x.variables()) == {'h0', 'e2', 'e5'} x = Xmrs(eps=[(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert set(x.variables()) == {'x4', 'h3'} x = Xmrs(hcons=[('h0', 'qeq', 'h1')]) assert set(x.variables()) == {'h0', 'h1'} x = Xmrs(icons=[('x4', 'focus', 'x6')]) assert set(x.variables()) == {'x4', 'x6'} # variables can be passed in and inferred x = Xmrs(icons=[('x4', 'focus', 'x6')], vars={'x4': {'PERS': '3'}}) assert set(x.variables()) == {'x4', 'x6'} assert x.properties('x4') == {'PERS': '3'} assert x.properties('x6') == {} # adding things later doesn't reset properties x = Xmrs(vars={'x4': {'PERS': '3'}}) x.add_eps([(10, sp('_n_n_rel'), 'h3', {'ARG0': 'x4'})]) assert set(x.variables()) == {'x4', 'h3'} assert x.properties('x4') == {'PERS': '3'} # and properties can't be added via properties() x.properties('x4')['NUM'] = 'sg' assert x.properties('x4') == {'PERS': '3'} # TODO: how do we add properties? # constants are not variables x = Xmrs(eps=[(10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'CARG': '"dog"'})]) assert set(x.variables()) == {'e2', 'h3'} # Constants don't need to be the CARG role, and don't need # quotes (but if there are quotes, even var-looking things are # constants). pyDelphin differs from the LKB in the first # respect, but also maybe in the second. x = Xmrs(eps=[(10, sp('_v_v_rel'), 'h3', {'ARG0': 'e2', 'ARG1': '1', 'ARG2': '"x5"'})]) assert set(x.variables()) == {'h3', 'e2'}
def test_add_icons(self): x = Xmrs() with pytest.raises(XmrsError): x.add_icons([('x0')]) # only left with pytest.raises(XmrsError): x.add_icons([('x0', 'topic')]) # only left and relation # hi, relation, and lo (the minimum, but probably max, too) x.add_icons([('x0', 'topic', 'x1')]) assert len(x.icons()) == 1 ics = x.icons('x0') assert len(ics) == 1 assert ics[0][0] == 'x0' assert ics[0][1] == 'topic' assert ics[0][2] == 'x1' # can have more than one icons with the same left variable x.add_icons([('x0', 'focus', 'x2')]) assert len(x.icons()) == 2
def test_is_connected(self): # empty Xmrs objects cannot be checked for connectedness with pytest.raises(XmrsError): Xmrs().is_connected() Xmrs(top='h0').is_connected() Xmrs(hcons=[('h0', 'qeq', 'h1')]).is_connected() # just a pred is fine (even without ARG0) x = Xmrs(eps=[(10, Pred.stringpred('_v_v_rel'), 'h1', {})]) assert x.is_connected() == True x = Xmrs(eps=[(10, Pred.stringpred('_v_v_rel'), 'h1', {'ARG0':'e2'})]) assert x.is_connected() == True # disconnected top is fine x = read('[ TOP: h0 RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ] > ]') assert x.is_connected() == True # and a broken HCONS is ok, too? (maybe revise later) x = read('[ TOP: h0 RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ] > ' ' HCONS: < h0 qeq h2 > ]') assert x.is_connected() == True # two disconnected EPs are definitely bad x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ] > ]') assert x.is_connected() == False # if they're linked, they're fine again x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ] > ]') # they can link to previous EPs, too x = read('[ TOP: h0 ' ' RELS: < [ _n_n_rel LBL: h3 ARG0: x4 ]' ' [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ] > ]') assert x.is_connected() == True # they can also be linked by LBL x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ]' ' [ _v_v_rel LBL: h1 ARG0: e4 ] > ]') # or by handle arguments x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: h3 ]' ' [ _v_v_rel LBL: h3 ARG0: e4 ] > ]') assert x.is_connected() == True # or by qeqs x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: h3 ]' ' [ _v_v_rel LBL: h5 ARG0: e4 ] > ' ' HCONS: < h3 qeq h5 > ]') assert x.is_connected() == True # simply being connected to something isn't enough x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ]' ' [ _v_v_rel LBL: h5 ARG0: e6 ARG1: x8 ]' ' [ _n_n_rel LBL: h7 ARG0: x8 ] > ]') assert x.is_connected() == False # some linkage needs to connect disparate subgraphs (even weird ones) x = read('[ TOP: h0 ' ' RELS: < [ _v_v_rel LBL: h1 ARG0: e2 ARG1: x4 ]' ' [ _n_n_rel LBL: h3 ARG0: x4 ]' ' [ _v_v_rel LBL: h5 ARG0: e2 ARG1: x8 ]' ' [ _n_n_rel LBL: h7 ARG0: x8 ] > ]') assert x.is_connected() == True