예제 #1
0
    def test_programs(self):
        program1 = dsl.Concat(dsl.GetToken(dsl.Type.ALPHANUM, 3),
                              dsl.GetFrom(':'), dsl.GetFirst(dsl.Type.CHAR, 4))
        self.assertEqual(program1('Ud 9:25,JV3 Obb'), '2525,JV3 ObbUd 9')
        self.assertEqual(program1('zLny xmHg 8:43 A44q'), '843 A44qzLny')

        program2 = dsl.Concat(
            dsl.Compose(
                dsl.Replace(' ', ','),
                dsl.GetSpan(dsl.Type.PROP_CASE, 1, dsl.Boundary.START,
                            dsl.Type.PROP_CASE, 4, dsl.Boundary.END)),
            dsl.ConstStr('.'), dsl.GetToken(dsl.Type.PROP_CASE, -1))
        self.assertEqual(program2('Jacob Ethan James Alexander Michael'),
                         'Jacob,Ethan,James,Alexander.Michael')
        self.assertEqual(program2('Earth Fire Wind Water Pluto Sun'),
                         'Earth,Fire,Wind,Water.Sun')
예제 #2
0
def random_get_token(inputs, delimiter_dict, type_dict):
  """Samples random GetToken expression."""
  del delimiter_dict
  while True:
    t = random_type(type_dict)
    indices = [i for i in dsl.INDEX if abs(i) <= type_dict[t]]
    i = random.choice(indices)

    expr = dsl.GetToken(t, i)
    if not _is_output_empty(expr, inputs):
      return expr
예제 #3
0
def random_get_token(inputs, delimiter_dict, type_dict):
    """Samples random GetToken expression."""
    del delimiter_dict
    while True:
        t = random_type(type_dict)
        indices = [i for i in dsl.INDEX if abs(i) <= type_dict[t]]
        i = random.choice(indices)

        expr = dsl.GetToken(t, i)
        # Make sure outputs are non-empty.
        try:
            if min(len(expr(input_value)) for input_value in inputs) == 0:
                continue
        except:  # pylint: disable=[bare-except]
            continue
        return expr
예제 #4
0
    def test_decode(self):
        id_token_table, token_id_table = tokens.build_token_tables()
        self.assertEqual(len(token_id_table), len(id_token_table))
        program = dsl.Concat(
            dsl.Compose(
                dsl.Replace(' ', ','),
                dsl.GetSpan(dsl.Type.PROP_CASE, 1, dsl.Boundary.START,
                            dsl.Type.PROP_CASE, 4, dsl.Boundary.END)),
            dsl.ConstStr('.'), dsl.GetToken(dsl.Type.PROP_CASE, -1))
        encoding = program.encode(token_id_table)
        self.assertEqual(encoding[-1], token_id_table[dsl.EOS])

        decoded_program = dsl.decode_program(encoding, id_token_table)
        self.assertEqual(
            decoded_program('Jacob Ethan James Alexander Michael'),
            'Jacob,Ethan,James,Alexander.Michael')
        self.assertEqual(decoded_program('Earth Fire Wind Water Pluto Sun'),
                         'Earth,Fire,Wind,Water.Sun')