예제 #1
0
def random_compose_substring(inputs, delimiter_dict, type_dict):
  """Samples random Compose expression."""
  while True:
    expr = dsl.Compose(
        random_expression(inputs, delimiter_dict, type_dict,
                          sampler_pool=ALL_MODIFICATION),
        random_expression(inputs, delimiter_dict, type_dict,
                          sampler_pool=ALL_SUBSTRING))
    if not _is_output_empty(expr, inputs):
      return expr
예제 #2
0
def random_compose_modification(inputs, delimiter_dict, type_dict):
  """Samples random Compose expression using only modify ops."""
  while True:
    expr = dsl.Compose(
        random_expression(inputs, delimiter_dict, type_dict,
                          sampler_pool=ALL_MODIFICATION),
        random_expression(inputs, delimiter_dict, type_dict,
                          sampler_pool=ALL_MODIFICATION))
    if not _is_output_empty(expr, inputs):
      return expr
예제 #3
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')
예제 #4
0
def random_compose(inputs, delimiter_dict, type_dict):
  """Samples random Compose expression."""
  nesting_or_substring = random.choice([
      random_nesting,
      random_substring,
  ])
  while True:
    expr = dsl.Compose(random_nesting(inputs, delimiter_dict, type_dict),
                       nesting_or_substring(inputs, delimiter_dict, type_dict))
    # 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
예제 #5
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')