def testGpred(self):
     p = Pred.grammarpred('pron_rel')
     assert p.type == Pred.GRAMMARPRED
     assert p.string == 'pron_rel'
     assert p.lemma == 'pron'
     assert p.pos == None
     assert p.sense == None
     assert p.short_form() == 'pron'
     p = Pred.grammarpred('udef_q_rel')
     assert p.string == 'udef_q_rel'
     assert p.lemma == 'udef'
     assert p.pos == 'q'
     assert p.sense == None
     assert p.short_form() == 'udef_q'
     p = Pred.grammarpred('udef_q')
     assert p.string == 'udef_q'
     assert p.lemma == 'udef'
     assert p.pos == 'q'
     assert p.sense == None
     assert p.short_form() == 'udef_q'
     p = Pred.grammarpred('abc_def_ghi_rel')
     assert p.type == Pred.GRAMMARPRED
     assert p.string == 'abc_def_ghi_rel'
     # pos must be a single character, so we get abc_def, ghi, rel
     assert p.lemma == 'abc_def'
     assert p.pos == None
     assert p.sense == 'ghi'
     assert p.short_form() == 'abc_def_ghi'
     repr(p)  # no error
Example #2
0
    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 testGpred(self):
     p = Pred.grammarpred('pron_rel')
     assert p.type == Pred.GRAMMARPRED
     assert p.string == 'pron_rel'
     assert p.lemma == 'pron'
     assert p.pos == None
     assert p.sense == None
     assert p.short_form() == 'pron'
     p = Pred.grammarpred('udef_q_rel')
     assert p.string == 'udef_q_rel'
     assert p.lemma == 'udef'
     assert p.pos == 'q'
     assert p.sense == None
     assert p.short_form() == 'udef_q'
     p = Pred.grammarpred('udef_q')
     assert p.string == 'udef_q'
     assert p.lemma == 'udef'
     assert p.pos == 'q'
     assert p.sense == None
     assert p.short_form() == 'udef_q'
     p = Pred.grammarpred('abc_def_ghi_rel')
     assert p.type == Pred.GRAMMARPRED
     assert p.string == 'abc_def_ghi_rel'
     # pos must be a single character, so we get abc_def, ghi, rel
     assert p.lemma == 'abc_def'
     assert p.pos == None
     assert p.sense == 'ghi'
     assert p.short_form() == 'abc_def_ghi'
     repr(p)  # no error
Example #4
0
    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
Example #5
0
 def testEq(self):
     assert spred('_dog_n_rel') == Pred.realpred(lemma='dog', pos='n')
     assert spred('_dog_n_rel') == '_dog_n_rel'
     assert '_dog_n_rel' == Pred.realpred(lemma='dog', pos='n')
     assert spred('"_dog_n_rel"') == spred("'_dog_n_rel")
     assert Pred.grammarpred('pron_rel') == 'pron_rel'
     assert Pred.string_or_grammar_pred('_dog_n_rel') != Pred.string_or_grammar_pred('dog_n_rel')
     assert (spred('_dog_n_rel') == None) == False
Example #6
0
 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 ] > ]')
     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
Example #7
0
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')])
Example #8
0
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')])
Example #9
0
 def testEq(self):
     assert spred('_dog_n_rel') == Pred.realpred(lemma='dog', pos='n')
     assert spred('_dog_n_rel') == '_dog_n_rel'
     assert '_dog_n_rel' == Pred.realpred(lemma='dog', pos='n')
     assert spred('"_dog_n_rel"') == spred("'_dog_n_rel")
     assert Pred.abstract('pron_rel') == 'pron_rel'
     assert Pred.surface_or_abstract(
         '_dog_n_rel') != Pred.surface_or_abstract('dog_n_rel')
     assert (spred('_dog_n_rel') == None) == False
     assert spred('_dog_n_1_rel') == spred('_Dog_N_1_rel')
     assert spred('_dog_n_1_rel') == spred('_dog_n_1')
Example #10
0
 def test_args(self):
     # return the argument dict of a nodeid, or KeyError for missing nodeid
     x = Xmrs(
         eps=[(10, Pred.surface('_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.surface('_v_v_rel'), 'h3')])
     assert x.args(10) == {}
Example #11
0
 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) == {}
Example #12
0
def _decode_pred(elem):
    # <!ELEMENT pred (#PCDATA)>
    # <!ELEMENT spred (#PCDATA)>
    # <!ELEMENT realpred EMPTY>
    # <!ATTLIST realpred
    #           lemma CDATA #REQUIRED
    #           pos (v|n|j|r|p|q|c|x|u|a|s) #REQUIRED
    #           sense CDATA #IMPLIED >
    if elem.tag == 'pred':
        return Pred.grammarpred(elem.text)
    elif elem.tag == 'spred':
        return Pred.stringpred(elem.text)
    elif elem.tag == 'realpred':
        return Pred.readpred(elem.get('lemma'), elem.get('pos'),
                             elem.get('sense'))
Example #13
0
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')
        ]
    )
Example #14
0
def decode_pred(elem):
    # <!ELEMENT pred (#PCDATA)>
    # <!ELEMENT spred (#PCDATA)>
    # <!ELEMENT realpred EMPTY>
    # <!ATTLIST realpred
    #           lemma CDATA #REQUIRED
    #           pos (v|n|j|r|p|q|c|x|u|a|s) #REQUIRED
    #           sense CDATA #IMPLIED >
    if elem.tag == 'pred':
        return Pred.grammarpred(elem.text)
    elif elem.tag == 'spred':
        return Pred.stringpred(elem.text)
    elif elem.tag == 'realpred':
        return Pred.readpred(elem.get('lemma'),
                             elem.get('pos'),
                             elem.get('sense'))
Example #15
0
def read_ep(tokens, variables=None):
    """Read and return an ElementaryPredication."""
    # [ pred LBL : lbl ARG : variable-or-handle ... ]
    # or [ pred < lnk > ...
    if variables is None:
        variables = {}
    validate_token(tokens.popleft(), _left_bracket)
    pred = Pred.string_or_grammar_pred(tokens.popleft())
    lnk = read_lnk(tokens)
    if tokens[0].startswith('"'):
        surface = tokens.popleft()[1:-1] # get rid of first quotes
    else:
        surface = None
    _, label = read_featval(tokens, feat=_lbl, sort=HANDLESORT,
                            variables=variables)
    args = []
    while tokens[0] != _right_bracket:
        args.append(read_argument(tokens, variables=variables))
    tokens.popleft()  # we know this is a right bracket
    return ElementaryPredication(None,  # no nodeid in MRS
                                 pred,
                                 label,
                                 args=args,
                                 lnk=lnk,
                                 surface=surface)
Example #16
0
def _read_ep(tokens, nid, vars_):
    # reassign these locally to avoid global lookup
    CARG = CONSTARG_ROLE
    _var_re = var_re
    # begin parsing
    _read_literals(tokens, '[')
    pred = Pred.string_or_grammar_pred(tokens.popleft())
    lnk = _read_lnk(tokens)
    surface = label = None
    if tokens[0].startswith('"'):
        surface = tokens.popleft()[1:-1]  # get rid of first quotes
    if tokens[0].upper() == 'LBL':
        tokens.popleft()  # LBL
        _read_literals(tokens, ':')
        label = tokens.popleft()
        vars_[label] = []
    args = {}
    while tokens[0] != ']':
        role = tokens.popleft().upper()
        _read_literals(tokens, ':')
        val = tokens.popleft()
        if _var_re.match(val) is not None and role.upper() != CARG:
            props = _read_props(tokens)
            if val not in vars_:
                vars_[val] = []
            vars_[val].extend(props)
        args[role] = val
    tokens.popleft()  # ]
    return (nid, pred, label, args, lnk, surface)
Example #17
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.surface('_n_n_rel'), 'h3', {'ARG0': 'x4'})])
     assert x.pred(10).string == '_n_n_rel'
Example #18
0
 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)
Example #19
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'
Example #20
0
def _read_ep(tokens, nid, vars_):
    # reassign these locally to avoid global lookup
    CARG = CONSTARG_ROLE
    _var_re = var_re
    # begin parsing
    tokens.popleft()  # [
    pred = Pred.string_or_grammar_pred(tokens.popleft())
    lnk = read_lnk(tokens)
    surface = label = None
    if tokens[0].startswith('"'):
        surface = tokens.popleft()[1:-1]  # get rid of first quotes
    if tokens[0] == _lbl:
        tokens.popleft()  # LBL
        tokens.popleft()  # :
        label = tokens.popleft()
        vars_[label] = []
    args = {}
    while tokens[0] != _right_bracket:
        role = tokens.popleft()
        tokens.popleft()  # :
        val = tokens.popleft()
        if _var_re.match(val) is not None and role.upper() != CARG:
            props = _read_props(tokens)
            if val not in vars_:
                vars_[val] = []
            vars_[val].extend(props)
        args[role] = val
    tokens.popleft()  # ]
    return (nid, pred, label, args, lnk, surface)
Example #21
0
def mtr_entries(mtr, typemap):
    rules = tdl.parse(open(mtr, 'r'))
    for rule in rules:
        assert len(rule.supertypes) == 1

        lename = rule.identifier.rpartition('-')[0] + '_le'
        rule_supertype = rule.supertypes[0]

        if rule_supertype not in typemap:
            continue
        supertypes = typemap[rule_supertype]

        rels = rule['OUTPUT.RELS'].values()
        if (any('PRED' not in rel for rel in rels)
                or len(supertypes) != len(rels)):
            continue

        preds = [rel['PRED'] for rel in rels]
        for i in range(len(supertypes)):
            supertype = supertypes[i]

            pred = preds[i]
            # if pred is not a string, type symbol is the supertype
            if isinstance(pred, tdl.TdlDefinition):
                pred = pred.supertypes[0]
            pred = Pred.string_or_grammar_pred(pred)

            # lemma can be single (_pattern_n_1_rel) or multi (_at+all_a_1_rel)
            orth = ', '.join('"{}"'.format(part)
                             for part in pred.lemma.split('+'))

            yield (lename + str(i), supertype, orth, pred, None)
Example #22
0
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')
        ]
    )
Example #23
0
 def testRealPred(self):
     # basic, no sense arg
     p = Pred.realpred('dog', 'n')
     assert p.type == Pred.REALPRED
     assert p.string == '_dog_n_rel'
     assert p.lemma == 'dog'
     assert p.pos == 'n'
     assert p.sense == None
     assert p.short_form() == '_dog_n'
     # try with arg names, unicode, and sense
     p = Pred.realpred(lemma='犬', pos='n', sense='1')
     assert p.type == Pred.REALPRED
     assert p.string == '_犬_n_1_rel'
     assert p.lemma == '犬'
     assert p.pos == 'n'
     assert p.sense == '1'
     assert p.short_form() == '_犬_n_1'
     # in case sense is int, not str
     p = Pred.realpred('dog', 'n', 1)
     assert p.type == Pred.REALPRED
     assert p.string == '_dog_n_1_rel'
     assert p.lemma == 'dog'
     assert p.pos == 'n'
     assert p.sense == '1'
     assert p.short_form() == '_dog_n_1'
     with pytest.raises(TypeError): Pred.realpred(lemma='dog')
     with pytest.raises(TypeError): Pred.realpred(pos='n')
     repr(p)  # no error
Example #24
0
def eds_it_rains():
    return eds.Eds(
        top='e2',
        nodes=[
            Node(
                'e2',
                Pred.stringpred('"_rain_v_1_rel"'),
                sortinfo={
                    'SF': 'prop', 'TENSE': 'pres', 'MOOD': 'indicative',
                    'PROG': '-', 'PERF': '-', CVARSORT: 'e'},
                lnk=Lnk.charspan(3, 9)
            )
        ],
        edges=[]
    )
Example #25
0
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=[]
    )
Example #26
0
 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 = read('[ 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.stringpred('_v_v_rel') not in x
Example #27
0
 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
Example #28
0
 def search_pred_string(pred_str, extend_lemma=True, ctx=None):
     if not pred_str:
         raise Exception("pred_str cannot be empty")
     if ctx is None:
         with PredSense.wn.ctx() as ctx:
             return PredSense.search_pred_string(pred_str, extend_lemma=extend_lemma, ctx=ctx)
     # ctx will never be null
     # ensure that pred_str is really a str
     pred_str = str(pred_str)
     pred = Pred.string_or_grammar_pred(pred_str)
     # use manual mapping whenever possible
     pred_str_search = pred_str if pred_str.endswith('_rel') else pred_str + '_rel'
     # if this is a known preds
     if pred_str_search in MWE_ERG_PRED_LEMMA:
         ss = PredSense.search_sense([MWE_ERG_PRED_LEMMA[pred_str_search]], pred.pos)
         if ss:
             return sorted(ss, key=lambda x: x.tagcount, reverse=True)
     return PredSense.search_pred(pred, extend_lemma)
Example #29
0
def read_ep(tokens, variables=None):
    """Read and return an ElementaryPredication."""
    # [ pred LBL : lbl ARG : variable-or-handle ... ]
    # or [ pred < lnk > ...
    if variables is None:
        variables = {}
    validate_token(tokens.popleft(), _left_bracket)
    pred = Pred.string_or_grammar_pred(tokens.popleft())
    lnk = read_lnk(tokens)
    if tokens[0].startswith('"'):
        surface = tokens.popleft()[1:-1]  # get rid of first quotes
    else:
        surface = None
    _, label = read_featval(tokens, feat=_lbl, sort=HANDLESORT, variables=variables)
    args = []
    while tokens[0] != _right_bracket:
        args.append(read_argument(tokens, variables=variables))
    tokens.popleft()  # we know this is a right bracket
    return ElementaryPredication(None, pred, label, args=args, lnk=lnk, surface=surface)  # no nodeid in MRS
Example #30
0
    def testRealPred(self):
        # basic, no sense arg
        p = Pred.realpred('dog', 'n')
        assert p.type == Pred.REALPRED
        assert p.string == '_dog_n_rel'
        assert p.lemma == 'dog'
        assert p.pos == 'n'
        assert p.sense == None
        assert p.short_form() == '_dog_n'
        # try with arg names, unicode, and sense
        p = Pred.realpred(lemma='犬', pos='n', sense='1')
        assert p.type == Pred.REALPRED
        assert p.string == '_犬_n_1_rel'
        assert p.lemma == '犬'
        assert p.pos == 'n'
        assert p.sense == '1'
        assert p.short_form() == '_犬_n_1'
        # in case sense is int, not str
        p = Pred.realpred('dog', 'n', 1)
        assert p.type == Pred.REALPRED
        assert p.string == '_dog_n_1_rel'
        assert p.lemma == 'dog'
        assert p.pos == 'n'
        assert p.sense == '1'
        assert p.short_form() == '_dog_n_1'
        # see https://github.com/delph-in/pydelphin/issues/129
        p = Pred.realpred('te', None, 'adjunct')
        assert p.type == Pred.REALPRED
        assert p.string == '_te_adjunct_rel'
        assert p.lemma == 'te'
        assert p.pos == None
        assert p.sense == 'adjunct'
        assert p.short_form() == '_te_adjunct'

        with pytest.raises(TypeError):
            Pred.realpred(lemma='dog')
        with pytest.raises(TypeError):
            Pred.realpred(pos='n')
        repr(p)  # no error
Example #31
0
 def testStringOrGrammarPred(self):
     p = Pred.string_or_grammar_pred('_dog_n_rel')
     assert p.type == Pred.STRINGPRED
     p = Pred.string_or_grammar_pred('pron_rel')
     assert p.type == Pred.GRAMMARPRED
Example #32
0
 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
Example #33
0
 def testSurfaceOrAbstractPred(self):
     p = Pred.surface_or_abstract('_dog_n_rel')
     assert p.type == Pred.SURFACE
     p = Pred.surface_or_abstract('pron_rel')
     assert p.type == Pred.ABSTRACT
Example #34
0
 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)
Example #35
0
 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)
Example #36
0
jrel = {}
id2rel = {}
# rel2lem = {}

print >> sys.stderr, "Reading Jacy lexicon"
for entry in tdl.parse(open(pjoin(jacydir, 'lexicon.tdl'))):
    identifier = entry.identifier
    orths = [o.strip('"') for o in entry[JACY_ORTH_FEAT].values()]
    rel = entry.get(JACY_PRED_FEAT, default='')
    # rel2lem[rel] = ' '.join(orths)
    id2rel[identifier] = rel
    if rel:
        if isinstance(rel, tdl.TdlDefinition):
            rel = rel.supertypes[0]
        lemma = Pred.string_or_grammar_pred(rel).lemma
        for orth in orths:
            jrel[orth] = jrel.get(orth, []) + [lemma]

## FIXME
## Added 'saseru' to the dictonaries. The _saseru_v_cause_rel is not specified
## in the Jacy lexicon.
jrel['させる'] = jrel.get('させる', []) + ["saseru"]
# rel2lem['"_saseru_v_cause_rel"'] = 'させる'
id2rel['saseru-intrans-end'] = '"_saseru_v_cause_rel"'
id2rel['saseru-trans-obj-end'] = '"_saseru_v_cause_rel"'
## End FIXME
jrelkeys = set(jrel.keys())

# Reading the ERG lexicon
# MWG 2017-03-15 : removed as it seems unused
Example #37
0
 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
Example #38
0
 def test_subgraph(self):
     nodes = [Node(1,Pred.stringpred('verb'))]
     links = [Link(0,1,'','H')]
     graph = Dmrs(nodes,links)
     new_graph = graph.subgraph([1])
     assert len(new_graph._eps) == 1