Beispiel #1
0
def random_get_span(inputs, delimiter_dict, type_dict):
  """Samples random GetSpan expression."""
  while True:
    is_delimiters = [random.randint(1, 2), random.randint(1, 2)]
    if delimiter_dict and is_delimiters[0] == 1:
      r1 = random_delimiter(delimiter_dict)
      indices = [i for i in dsl.INDEX if abs(i) <= delimiter_dict[r1]]
      i1 = random.choice(indices)
    else:
      r1 = random_type(type_dict)
      indices = [i for i in dsl.INDEX if abs(i) <= type_dict[r1]]
      i1 = random.choice(indices)
    if delimiter_dict and is_delimiters[1] == 1:
      r2 = random_delimiter(delimiter_dict)
      indices = [i for i in dsl.INDEX if abs(i) <= delimiter_dict[r2]]
      i2 = random.choice(indices)
    else:
      r2 = random_type(type_dict)
      indices = [i for i in dsl.INDEX if abs(i) <= type_dict[r2]]
      i2 = random.choice(indices)

    expr = dsl.GetSpan(
        r1, i1, random_boundary(), r2, i2, random_boundary())
    if not _is_output_empty(expr, inputs):
      return expr
Beispiel #2
0
def random_get_span(inputs, delimiter_dict, type_dict):
    """Samples random GetSpan expression."""
    while True:
        is_delimiters = [random.randint(1, 2), random.randint(1, 2)]
        if delimiter_dict and is_delimiters[0] == 1:
            r1 = random_delimiter(delimiter_dict)
            indices = [i for i in dsl.INDEX if abs(i) <= delimiter_dict[r1]]
            i1 = random.choice(indices)
        else:
            r1 = random_type(type_dict)
            indices = [i for i in dsl.INDEX if abs(i) <= type_dict[r1]]
            i1 = random.choice(indices)
        if delimiter_dict and is_delimiters[1] == 1:
            r2 = random_delimiter(delimiter_dict)
            indices = [i for i in dsl.INDEX if abs(i) <= delimiter_dict[r2]]
            i2 = random.choice(indices)
        else:
            r2 = random_type(type_dict)
            indices = [i for i in dsl.INDEX if abs(i) <= type_dict[r2]]
            i2 = random.choice(indices)

        expr = dsl.GetSpan(r1, i1, random_boundary(), r2, i2,
                           random_boundary())
        # 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
    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')
    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')