def _get_expression(self): """Gets the expression and symbols of current state. Returns: expression: String. symbols: List of symbols. """ symbols = postprocessor.production_rules_sequence_to_symbols( prod_rules_sequence=self.production_rules_sequence) return ' '.join([str(symbol) for symbol in symbols]), symbols
def test_production_rules_sequence_to_symbols_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 t, = nltk.grammar.nonterminals('T') self.assertEqual( postprocessor.production_rules_sequence_to_symbols( prod_rules_sequence), [t, '+', t])
def test_production_rules_sequence_to_symbols_terminal(self): prod_rule_strings = [ 'S -> S "+" T', 'S -> T', 'T -> "x"', 'T -> "x"', # The generation of symbols by grammar production rules sequence will # stop if all the symbols are terminal. For the grammar rules in this # unittest, the last two dummy rules are actually not used. 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_symbols( prod_rules_sequence), ['x', '+', 'x'])
def test_production_rules_sequence_to_symbols_empty(self): # Empty production rule sequence should have empty symbols. self.assertEqual( postprocessor.production_rules_sequence_to_symbols([]), [])