Esempio n. 1
0
 def test_get_valid_actions_uses_top_of_stack(self):
     state = GrammarState(['s'], {}, {'s': [1, 2], 't': [3, 4]}, {}, is_nonterminal)
     assert state.get_valid_actions() == [1, 2]
     state = GrammarState(['t'], {}, {'s': [1, 2], 't': [3, 4]}, {}, is_nonterminal)
     assert state.get_valid_actions() == [3, 4]
     state = GrammarState(['e'], {}, {'s': [1, 2], 't': [3, 4], 'e': []}, {}, is_nonterminal)
     assert state.get_valid_actions() == []
Esempio n. 2
0
 def test_get_valid_actions_adds_lambda_productions_only_for_correct_type(
         self):
     state = GrammarState(['t'], {('s', 'x'): ['t']}, {
                                      's': [1, 2],
                                      't': [3, 4]
                                  }, {'s -> x': 5}, is_nonterminal)
     assert state.get_valid_actions() == [3, 4]
     # We're doing this assert twice to make sure we haven't accidentally modified the state.
     assert state.get_valid_actions() == [3, 4]
Esempio n. 3
0
 def test_get_valid_actions_adds_lambda_productions_only_for_correct_type(self):
     state = GrammarState(['t'],
                          {('s', 'x'): ['t']},
                          {'s': [1, 2], 't': [3, 4]},
                          {'s -> x': 5},
                          is_nonterminal)
     assert state.get_valid_actions() == [3, 4]
     # We're doing this assert twice to make sure we haven't accidentally modified the state.
     assert state.get_valid_actions() == [3, 4]
Esempio n. 4
0
 def test_get_valid_actions_uses_top_of_stack(self):
     state = GrammarState(['s'], {}, {
         's': [1, 2],
         't': [3, 4]
     }, {}, is_nonterminal)
     assert state.get_valid_actions() == [1, 2]
     state = GrammarState(['t'], {}, {
         's': [1, 2],
         't': [3, 4]
     }, {}, is_nonterminal)
     assert state.get_valid_actions() == [3, 4]
     state = GrammarState(['e'], {}, {
         's': [1, 2],
         't': [3, 4],
         'e': []
     }, {}, is_nonterminal)
     assert state.get_valid_actions() == []
Esempio n. 5
0
 def test_get_valid_actions_adds_lambda_productions(self):
     state = GrammarState(
         ['s'], {('s', 'x'): ['s']},
         {
             's': {
                 'global':
                 (torch.Tensor([1, 1]), torch.Tensor([2, 2]), [1, 2])
             }
         }, {'s -> x':
             (torch.Tensor([5]), torch.Tensor([6]), 5)}, is_nonterminal)
     actions = state.get_valid_actions()
     assert_almost_equal(actions['global'][0].cpu().numpy(), [1, 1, 5])
     assert_almost_equal(actions['global'][1].cpu().numpy(), [2, 2, 6])
     assert actions['global'][2] == [1, 2, 5]
     # We're doing this assert twice to make sure we haven't accidentally modified the state.
     actions = state.get_valid_actions()
     assert_almost_equal(actions['global'][0].cpu().numpy(), [1, 1, 5])
     assert_almost_equal(actions['global'][1].cpu().numpy(), [2, 2, 6])
     assert actions['global'][2] == [1, 2, 5]
Esempio n. 6
0
 def test_get_valid_actions_uses_top_of_stack(self):
     s_actions = object()
     t_actions = object()
     e_actions = object()
     state = GrammarState(['s'], {}, {
         's': s_actions,
         't': t_actions
     }, {}, is_nonterminal)
     assert state.get_valid_actions() == s_actions
     state = GrammarState(['t'], {}, {
         's': s_actions,
         't': t_actions
     }, {}, is_nonterminal)
     assert state.get_valid_actions() == t_actions
     state = GrammarState(['e'], {}, {
         's': s_actions,
         't': t_actions,
         'e': e_actions
     }, {}, is_nonterminal)
     assert state.get_valid_actions() == e_actions
 def test_get_valid_actions_adds_lambda_productions(self):
     state = GrammarState([u's'], {(u's', u'x'): [u's']}, {u's': [1, 2]},
                          {u's -> x': 5}, is_nonterminal)
     assert state.get_valid_actions() == [1, 2, 5]
     # We're doing this assert twice to make sure we haven't accidentally modified the state.
     assert state.get_valid_actions() == [1, 2, 5]