def test_initialize_list_copy(self): initialize_list = ['x'] stack = postprocessor.GrammarLhsStack(initialize_list) # The elements in stack should not change with initialize_list after # stack initialization. initialize_list[0] = ['y'] self.assertEqual(stack.pop(), 'x')
def __init__(self, production_rules_sequence, stack=None): """Initializer. If this state is the initial state with no production rules sequence, pass a list of one symbol string to stack argument. This will enforce the next production rule to append starting with this symbol. Args: production_rules_sequence: List of nltk.grammar.Production objects. This sequence is obtained by a preorder traversal of the context-free grammar parsing tree. stack: GrammarLhsStack object or list, the stack to store the string of left hand side symbol. The left hand side symbol of valid production rule to append must match the top element in the stack. If the input is a list, the last element in the list is the top element in the stack. Raises: ValueError: If stack is not list, GrammarLhsStack or None. """ self._production_rules_sequence = production_rules_sequence if stack is None: self._stack = postprocessor.production_rules_sequence_to_stack( production_rules_sequence) elif isinstance(stack, list): self._stack = postprocessor.GrammarLhsStack(stack) elif isinstance(stack, postprocessor.GrammarLhsStack): self._stack = stack.copy() else: raise ValueError('stack is expected to be list, GrammarLhsStack or ' 'None, but got %s.' % type(stack)) # Log the state information defined in __repr__. logging.info('Create %s', self)
def test_init_stack_grammar_lhs_stack(self): state = states.ProductionRulesState( production_rules_sequence=[], stack=postprocessor.GrammarLhsStack(['T', 'R'])) # Use assertIs to check exact type rather than assertIsInstance. # https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertIsInstance self.assertIs(type(state._stack), postprocessor.GrammarLhsStack) self.assertEqual(state._stack.to_list(), ['T', 'R'])
def test_to_list(self): stack = postprocessor.GrammarLhsStack(['a', 'b', 'c']) stack_list = stack.to_list() self.assertEqual(stack_list, ['a', 'b', 'c']) stack.pop() # stack_list is a copy. self.assertEqual(stack_list, ['a', 'b', 'c']) # to_list() will always get the current symbols in stack. self.assertEqual(stack.to_list(), ['a', 'b'])
def test_copy(self): stack = postprocessor.GrammarLhsStack(['a', 'b', 'c']) copy_stack = stack.copy() # Use assertIs to check exact type rather than assertIsInstance. # https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertIsInstance self.assertIs(type(copy_stack), postprocessor.GrammarLhsStack) self.assertEqual(copy_stack._stack, ['a', 'b', 'c']) # copy_stack is a copy of stack. The change in stack after copy will not # affect copy_stack. stack.pop() self.assertEqual(stack._stack, ['a', 'b']) self.assertEqual(copy_stack._stack, ['a', 'b', 'c'])
def test_push_reversed_list(self): stack = postprocessor.GrammarLhsStack(['a']) stack.push_reversed_list(['b', 'c']) self.assertEqual(stack.pop(), 'b') self.assertEqual(stack.pop(), 'c') self.assertEqual(stack.pop(), 'a')
def test_push(self): stack = postprocessor.GrammarLhsStack() stack.push('x') self.assertEqual(stack.pop(), 'x')
def test_pop(self): stack = postprocessor.GrammarLhsStack(['x']) # Non-empty stack. self.assertEqual(stack.pop(), 'x') # Empty stack. self.assertEqual(stack.pop(), constants.DUMMY_LHS_SYMBOL)
def test_peek_empty(self): stack = postprocessor.GrammarLhsStack() self.assertEqual(stack.peek(), constants.DUMMY_LHS_SYMBOL) # Peek does not change the stack. self.assertEqual(stack._stack, [])
def test_peek(self): stack = postprocessor.GrammarLhsStack(['x']) self.assertEqual(stack.peek(), 'x') # Peek does not change the stack. self.assertEqual(stack._stack, ['x'])
def test_is_empty(self): self.assertFalse( postprocessor.GrammarLhsStack(['a', 'b', 'c']).is_empty()) self.assertTrue(postprocessor.GrammarLhsStack([]).is_empty())