示例#1
0
    def test_production_rules_sequence_to_expression_string_not_terminal(self):
        prod_rule_strings = [
            'S -> S "+" T',
            'S -> T',
        ]
        prod_rules_sequence = [
            self.prod_rules_dict[prod_rule_string]
            for prod_rule_string in prod_rule_strings
        ]
        # Parsing tree:
        #  S
        #  |
        #  S "+" T
        #  |
        #  T
        # Expression (non-terminal):
        # T + T
        self.assertEqual(
            postprocessor.production_rules_sequence_to_expression_string(
                prod_rules_sequence, self.delimiter), 'T + T')

        with self.assertRaisesRegexp(ValueError,
                                     'Not all the symbols are terminal.'):
            postprocessor.production_rules_sequence_to_expression_string(
                prod_rules_sequence, self.delimiter, check_all_terminal=True)
示例#2
0
    def test_production_rules_sequence_to_expression_string(self):
        prod_rule_strings = [
            'S -> S "+" T',
            'S -> T',
            'T -> "x"',
            'T -> "x"',
            constants.DUMMY_PRODUCTION_RULE,
            constants.DUMMY_PRODUCTION_RULE,
        ]
        prod_rules_sequence = [
            self.prod_rules_dict[prod_rule_string]
            for prod_rule_string in prod_rule_strings
        ]

        # Expression:
        # x + x
        # Parsing tree:
        #  S
        #  |
        #  S "+" T
        #  |     |
        #  T    "x"
        #  |
        # "x"
        # Production rules sequence (preorder tree traversal)
        # 'S -> S "+" T'
        # 'S -> T'
        # 'T -> "x"
        # 'T -> "x"

        self.assertEqual(
            postprocessor.production_rules_sequence_to_expression_string(
                prod_rules_sequence, self.delimiter), 'x + x')
示例#3
0
def next_production_rule_info(expression_string, partial_sequence,
                              partial_sequence_length, next_production_rule,
                              unmasked_probabilities, masked_probabilities,
                              grammar):
    """Converts information of next production rule prediction to a string.

  Args:
    expression_string: String. Expression where the partial sequence is sampled
        from.
    partial_sequence: Integer numpy array with shape [max_length].
    partial_sequence_length: Integer. The length of partial sequence. The input
        partial_sequence has padding at the end.
        partial_sequence[:partial_sequence_length] is the actual partial
        sequence.
    next_production_rule: Integer. The index of the next production rule.
    unmasked_probabilities: Float numpy array with shape
        [num_production_rules]. The probabilities from the model prediction
        without valid production rule mask.
    masked_probabilities: Float numpy array with shape
        [num_production_rules]. The probabilities from the model prediction
        after applied valid production rule mask.
    grammar: arithmetic_grammar.Grammar object.

  Returns:
    String. The information of next production rule prediction.
  """
    output_info = ['expression string:', expression_string]

    prod_rules_sequence = [
        grammar.prod_rules[index]
        for index in partial_sequence[:partial_sequence_length]
    ]
    output_info.append('partial expression:')
    output_info.append(
        postprocessor.production_rules_sequence_to_expression_string(
            prod_rules_sequence=prod_rules_sequence, delimiter=' '))

    output_info.append('true next production rule:')
    output_info.append(str(grammar.prod_rules[next_production_rule]))

    output_info.append('unmasked prediction next production rule:')
    output_info.extend(
        probabilities_info_string(probabilities=unmasked_probabilities,
                                  next_production_rule=next_production_rule,
                                  grammar=grammar))

    output_info.append('masked prediction next production rule:')
    output_info.extend(
        probabilities_info_string(probabilities=masked_probabilities,
                                  next_production_rule=next_production_rule,
                                  grammar=grammar))

    # Add '\t' for markdown display in tensorboard.
    return '\n'.join(['\t' + line for line in output_info])
示例#4
0
  def generate_history(self):
    """Generates the history of the expression generation.

    For example, if the current production rules in production_rules_sequence
    is ['S -> S "+" T', 'S -> T', 'T -> "y"', 'T -> "x"']

    The expression generation history when each production rule is appended is
    ['S + T', 'T + T', 'y + T', 'y + x'].

    Returns:
      List of expression strings.
    """
    production_rules_sequence = self.production_rules_sequence
    history = []
    for partial_sequence_length in range(1, len(production_rules_sequence) + 1):
      history.append(
          postprocessor.production_rules_sequence_to_expression_string(
              prod_rules_sequence=production_rules_sequence[
                  :partial_sequence_length],
              delimiter=' ',
              check_all_terminal=False))
    return history
示例#5
0
 def test_production_rules_sequence_to_expression_string_empty(self):
     self.assertEqual(
         postprocessor.production_rules_sequence_to_expression_string(
             [], self.delimiter), '')