示例#1
0
 def test_Pred_from_string(self):
     """
     Pred.from_string should normalise the string as necessary
     """
     cat_pred = RealPred.from_normalised_string('_cat_n_1')
     self.assertEqual(Pred.from_string('_cat_n_1_rel'), cat_pred)
     self.assertEqual(Pred.from_string('"_cat_n_1_rel"'), cat_pred)
     self.assertEqual(Pred.from_string('_CAT_N_1_REL'), cat_pred)
     
     the_pred = GPred.from_normalised_string('the')
     self.assertEqual(Pred.from_string('the_rel'), the_pred)
     self.assertEqual(Pred.from_string('"the_rel"'), the_pred)
     self.assertEqual(Pred.from_string('THE_REL'), the_pred)
示例#2
0
 def test_Pred_from_string(self):
     """
     Pred.from_string should normalise the string as necessary
     """
     cat_pred = RealPred.from_normalised_string('_cat_n_1')
     self.assertEqual(Pred.from_string('_cat_n_1_rel'), cat_pred)
     self.assertEqual(Pred.from_string('"_cat_n_1_rel"'), cat_pred)
     with self.assertRaises(Warning):
         warnings.simplefilter('error')
         self.assertEqual(Pred.from_string('_CAT_N_1_REL'), cat_pred)
     warnings.resetwarnings()
     
     the_pred = GPred.from_normalised_string('the')
     self.assertEqual(Pred.from_string('the_rel'), the_pred)
     self.assertEqual(Pred.from_string('"the_rel"'), the_pred)
     with self.assertRaises(Warning):
         warnings.simplefilter('error')
         self.assertEqual(Pred.from_string('THE_REL'), the_pred)
     warnings.resetwarnings()
示例#3
0
def _read_elempred(string, vs, rs):
    assert string[0] == '[' and string[-1] == ']'
    l = 1
    while string[l] == ' ':
        l += 1
    m = string.find('<', l)
    if m >= 0:
        r = string.index('>', m)
        c = string.find(':', m, r)
        if c >= 0:
            cfrom = int(string[m + 1:c])
            cto = int(string[c + 1:r])
        else:
            assert m + 1 == r
            cfrom = None
            cto = None
    else:
        m = r = string.index(' ', l)
        cfrom = None
        cto = None
    if string[m - 15:m] == '_u_unknown_rel"':
        assert string[l:l + 2] == '"_'
        slash = string.index('/', l + 2, m - 15)
        pos = {
            'FW': 'u',
            'JJ': 'a',
            'NN': 'n',
            'NNS': 'n',
            'RB': 'a',
            'VB': 'v',
            'VBP': 'v',
            'VBG': 'v',
            'VBN': 'v'
        }  # ?????????????????????????????????????
        assert string[slash + 1:m -
                      15] in pos, 'Invalid unknown word POS: {}'.format(
                          string[slash + 1:m - 15])
        pred = Pred.from_string(string[l + 1:slash] + '_' +
                                pos[string[slash + 1:m - 15]] +
                                string[m - 13:m - 1])
    else:
        assert string[l:m].islower()
        if string[l] == '"':
            assert string[m - 1] == '"'
            pred = Pred.from_string(string[l + 1:m - 1])
        else:
            pred = Pred.from_string(string[l:m])
    l = find_next(string, start=r + 1)
    r = find_previous(string, start=l, end=len(string) - 1) + 1
    attributes = _read_attributes(string[l:r], not_lower=('carg', ))
    label = _read_reference(attributes.pop('lbl'), vs, rs)
    carg = attributes.pop('carg', None)
    assert (carg is not None) == (
        isinstance(pred, GPred)
        and pred.name in ('basic_card', 'basic_numbered_hour', 'card', 'dofm',
                          'dofw', 'mofy', 'named', 'named_n', 'numbered_hour',
                          'ord', 'season', 'year_range', 'yofc')), (
                              carg, pred)  # gpreds with CARG

    args = {}
    quantifier = False
    if isinstance(pred, GPred):
        if pred.name in (
                'def_explicit_q', 'def_implicit_q', 'def_poss_q', 'every_q',
                'free_relative_q', 'free_relative_ever_q', 'idiom_q_i',
                'number_q', 'pronoun_q', 'proper_q', 'some_q', 'udef_q',
                'which_q'):  # all quantifier gpreds according to core.smi
            quantifier = True
            handle = _read_reference(attributes.pop('rstr'), vs, rs)
            args['rstr'] = handle
            if 'body' in attributes:
                handle = _read_reference(attributes.pop('body'), vs, rs)
                args['body'] = handle
        elif pred.name in (
                'discourse', 'implicit_conj'
        ):  # all conjunction gpreds with potential L/R-HNDL according to core.smi
            if 'arg1' in attributes:
                ref = _read_reference(attributes.pop('arg1'), vs, rs)
                args['arg1'] = ref
                ref = _read_reference(attributes.pop('arg2'), vs, rs)
                args['arg2'] = ref
            else:
                ref = _read_reference(attributes.pop('l-index'), vs, rs)
                args['l-index'] = ref
                ref = _read_reference(attributes.pop('r-index'), vs, rs)
                args['r-index'] = ref
                if 'l-hndl' in attributes:
                    assert pred.name == 'implicit_conj'
                    handle = _read_reference(attributes.pop('l-hndl'), vs, rs)
                    args['l-hndl'] = handle
                    handle = _read_reference(attributes.pop('r-hndl'), vs, rs)
                    args['r-hndl'] = handle
        elif pred.name in (
                'fw_seq', 'num_seq'
        ):  # all conjunction gpreds without potential L/R-HNDL according to core.smi
            ref = _read_reference(attributes.pop('l-index'), vs, rs)
            args['l-index'] = ref
            ref = _read_reference(attributes.pop('r-index'), vs, rs)
            args['r-index'] = ref
        elif pred.name == 'unknown':  # only predicate with ARG
            ref = _read_reference(attributes.pop('arg'), vs, rs)
            args['arg'] = ref
        else:  # regular gpreds
            for n in range(1, 4):
                role = 'arg' + str(n)
                if role not in attributes:
                    break
                ref = _read_reference(attributes.pop(role), vs, rs)
                args[role] = ref

    elif isinstance(pred, RealPred):
        assert pred.pos in 'acnpquvx'
        if pred.pos == 'q':  # quantifier
            quantifier = True
            handle = _read_reference(attributes.pop('rstr'), vs, rs)
            args['rstr'] = handle
            if 'body' in attributes:
                handle = _read_reference(attributes.pop('body'), vs, rs)
                args['body'] = handle
        elif pred.pos == 'c' and pred.lemma not in (
                'vice+versa', ):  # conjunction
            if 'arg1' in attributes:
                ref = _read_reference(attributes.pop('arg1'), vs, rs)
                args['arg1'] = ref
                if 'arg2' in attributes:
                    ref = _read_reference(attributes.pop('arg2'), vs, rs)
                    args['arg2'] = ref
            else:
                ref = _read_reference(attributes.pop('l-index'), vs, rs)
                args['l-index'] = ref
                ref = _read_reference(attributes.pop('r-index'), vs, rs)
                args['r-index'] = ref
                if 'l-hndl' in attributes:  # optional L/R-HNDL
                    handle = _read_reference(attributes.pop('l-hndl'), vs, rs)
                    args['l-hndl'] = handle
                    handle = _read_reference(attributes.pop('r-hndl'), vs, rs)
                    args['r-hndl'] = handle
        else:  # regular realpreds
            for n in range(1, 5):
                role = 'arg' + str(n)
                if role not in attributes:
                    break
                ref = _read_reference(attributes.pop(role), vs, rs)
                args[role] = ref
    else:
        assert False

    if quantifier:
        intrinsic = _read_reference(attributes.pop('arg0'), vs, rs)
        var = None
    else:
        intrinsic, var = _read_variable(attributes.pop('arg0'), vs, rs)
    assert not attributes, 'Invalid attributes for predicate {}: {}'.format(
        pred, attributes)
    return ElemPred(label=label,
                    pred=pred,
                    intrinsic=intrinsic,
                    carg=carg,
                    args=args,
                    cfrom=cfrom,
                    cto=cto), var
示例#4
0
# Also remove pronouns
extended_filter = DEFAULT_FILTER | {GPred('pron')}

# Replace the first pred with the second:
rename = [(RealPred('forwards', 'p'), RealPred('forward', 'p', 'dir'))]

# Replace a pair of nodes with a single node
# (the first pred linked to the second pred, is replaced by the third pred)
shrink = [('_left_a_1', 'ARG1/EQ', 'place_n', '_left_n_1'),
          ('_right_a_1', 'ARG1/EQ', 'place_n', '_right_n_1'),
          ('loc_nonsp', 'ARG2/NEQ', '_left_n_1', '_left_p_dir'),
          ('loc_nonsp', 'ARG2/NEQ', '_right_n_1', '_right_p_dir'),
          ('_to_p', 'ARG2/NEQ', '_left_n_1', '_left_p_dir'),
          ('_to_p', 'ARG2/NEQ', '_right_n_1', '_right_p_dir')]

shrink = [(Pred.from_string(a), LinkLabel.from_string(b), Pred.from_string(c),
           Pred.from_string(d)) for a, b, c, d in shrink]


def simplify(dmrs):
    """
    Simplify an input DMRS to a form that can be converted to robot commands
    """
    # Remove unnecessary GPreds (defaults, plus pronouns)
    gpred_filtering(dmrs, extended_filter)

    # Remove quantifiers
    for node in copy(dmrs.nodes):
        if dmrs.is_quantifier(node.nodeid):
            dmrs.remove_node(node.nodeid)
示例#5
0
# Also remove pronouns
extended_filter = DEFAULT_FILTER | {GPred('pron')}

# Replace the first pred with the second:
rename = [(RealPred('forwards','p'), RealPred('forward','p','dir'))]

# Replace a pair of nodes with a single node
# (the first pred linked to the second pred, is replaced by the third pred)
shrink = [('_left_a_1', 'ARG1/EQ', 'place_n', '_left_n_1'),
          ('_right_a_1', 'ARG1/EQ', 'place_n', '_right_n_1'),
          ('loc_nonsp', 'ARG2/NEQ', '_left_n_1', '_left_p_dir'),
          ('loc_nonsp', 'ARG2/NEQ', '_right_n_1', '_right_p_dir'),
          ('_to_p', 'ARG2/NEQ', '_left_n_1', '_left_p_dir'),
          ('_to_p', 'ARG2/NEQ', '_right_n_1', '_right_p_dir')]

shrink = [(Pred.from_string(a),
           LinkLabel.from_string(b),
           Pred.from_string(c),
           Pred.from_string(d)) for a,b,c,d in shrink]

def simplify(dmrs):
    """
    Simplify an input DMRS to a form that can be converted to robot commands
    """
    # Remove unnecessary GPreds (defaults, plus pronouns)
    gpred_filtering(dmrs, extended_filter)
    
    # Remove quantifiers
    for node in copy(dmrs.nodes):
        if dmrs.is_quantifier(node.nodeid):
            dmrs.remove_node(node.nodeid)