Beispiel #1
0
    def test_atis_grammar_statelet(self):
        valid_actions = None
        world = AtisWorld([("give me all flights from boston to "
                            "philadelphia next week arriving after lunch")])

        action_seq = [
            'statement -> [query, ";"]',
            'query -> ["(", "SELECT", distinct, select_results, "FROM", table_refs, '
            'where_clause, ")"]',
            'where_clause -> ["WHERE", "(", conditions, ")"]',
            'conditions -> [condition]', 'condition -> [biexpr]',
            'biexpr -> ["city", ".", "city_name", binaryop, city_city_name_string]',
            'city_city_name_string -> ["\'BOSTON\'"]', 'binaryop -> ["="]',
            'table_refs -> [table_name]', 'table_name -> ["city"]',
            'select_results -> [col_refs]',
            'col_refs -> [col_ref, ",", col_refs]', 'col_refs -> [col_ref]',
            'col_ref -> ["city", ".", "city_name"]',
            'col_ref -> ["city", ".", "city_code"]', 'distinct -> ["DISTINCT"]'
        ]

        grammar_state = GrammarStatelet(['statement'], {},
                                        world.valid_actions, {},
                                        AtisSemanticParser.is_nonterminal,
                                        reverse_productions=False)
        for action in action_seq:
            grammar_state = grammar_state.take_action(action)
        assert grammar_state._nonterminal_stack == []
    def test_atis_grammar_statelet(self):
        valid_actions = None
        world = AtisWorld([("give me all flights from boston to "
                            "philadelphia next week arriving after lunch")])
        action_sequence = \
                ['statement -> [query, ";"]',
                 'query -> ["(", "SELECT", distinct, select_results, "FROM", table_refs, '
                 'where_clause, ")"]',
                 'distinct -> ["DISTINCT"]',
                 'select_results -> [col_refs]',
                 'col_refs -> [col_ref, ",", col_refs]',
                 'col_ref -> ["city", ".", "city_code"]',
                 'col_refs -> [col_ref]',
                 'col_ref -> ["city", ".", "city_name"]',
                 'table_refs -> [table_name]',
                 'table_name -> ["city"]',
                 'where_clause -> ["WHERE", "(", conditions, ")"]',
                 'conditions -> [condition]',
                 'condition -> [biexpr]',
                 'biexpr -> ["city", ".", "city_name", binaryop, city_city_name_string]',
                 'binaryop -> ["="]',
                 'city_city_name_string -> ["\'BOSTON\'"]']

        grammar_state = GrammarStatelet(['statement'],
                                        world.valid_actions,
                                        AtisSemanticParser.is_nonterminal)
        for action in action_sequence:
            grammar_state = grammar_state.take_action(action)
        assert grammar_state._nonterminal_stack == []
    def test_take_action_gives_correct_next_states_with_lambda_productions(
            self):
        # state.take_action() doesn't read or change these objects, it just passes them through, so
        # we'll use some sentinels to be sure of that.
        valid_actions = object()
        context_actions = object()

        state = GrammarStatelet(['t', '<s,d>'], {}, valid_actions,
                                context_actions, is_nonterminal)
        next_state = state.take_action('<s,d> -> [lambda x, d]')
        expected_next_state = GrammarStatelet(['t', 'd'], {('s', 'x'): ['d']},
                                              valid_actions, context_actions,
                                              is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('d -> [<s,r>, d]')
        expected_next_state = GrammarStatelet(['t', 'd', '<s,r>'],
                                              {('s', 'x'): ['d', '<s,r>']},
                                              valid_actions, context_actions,
                                              is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('<s,r> -> [lambda y, r]')
        expected_next_state = GrammarStatelet(['t', 'd', 'r'], {
            ('s', 'x'): ['d', 'r'],
            ('s', 'y'): ['r']
        }, valid_actions, context_actions, is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('r -> identity')
        expected_next_state = GrammarStatelet(['t', 'd'], {('s', 'x'): ['d']},
                                              valid_actions, context_actions,
                                              is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('d -> x')
        expected_next_state = GrammarStatelet(['t'], {}, valid_actions,
                                              context_actions, is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__
    def test_take_action_gives_correct_next_states_with_non_lambda_productions(
            self):
        # state.take_action() doesn't read or change these objects, it just passes them through, so
        # we'll use some sentinels to be sure of that.
        valid_actions = object()
        context_actions = object()

        state = GrammarStatelet(['s'], {}, valid_actions, context_actions,
                                is_nonterminal)
        next_state = state.take_action('s -> [t, r]')
        expected_next_state = GrammarStatelet(['r', 't'], {}, valid_actions,
                                              context_actions, is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = GrammarStatelet(['r', 't'], {}, valid_actions, context_actions,
                                is_nonterminal)
        next_state = state.take_action('t -> identity')
        expected_next_state = GrammarStatelet(['r'], {}, valid_actions,
                                              context_actions, is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__
    def test_grammar_statelet(self):
        valid_actions = None
        world = Text2SqlWorld(self.schema)

        sql = ["SELECT", "COUNT", "(", "*", ")", "FROM", "LOCATION", ",", "RESTAURANT", ";"]
        action_sequence, valid_actions = world.get_action_sequence_and_all_actions(sql)

        grammar_state = GrammarStatelet(
            ["statement"], valid_actions, Text2SqlParser.is_nonterminal, reverse_productions=True
        )
        for action in action_sequence:
            grammar_state = grammar_state.take_action(action)
        assert grammar_state._nonterminal_stack == []
    def test_grammar_statelet(self):
        valid_actions = None
        world = Text2SqlWorld(self.schema)

        sql = ['SELECT', 'COUNT', '(', '*', ')', 'FROM', 'LOCATION', ',', 'RESTAURANT', ';']
        action_sequence, valid_actions = world.get_action_sequence_and_all_actions(sql)

        grammar_state = GrammarStatelet(['statement'],
                                        valid_actions,
                                        Text2SqlParser.is_nonterminal,
                                        reverse_productions=True)
        for action in action_sequence:
            grammar_state = grammar_state.take_action(action)
        assert grammar_state._nonterminal_stack == [] # pylint: disable=protected-access
    def test_grammar_statelet(self):
        valid_actions = None
        world = Text2SqlWorld(self.schema)

        sql = [
            'SELECT', 'COUNT', '(', '*', ')', 'FROM', 'LOCATION', ',',
            'RESTAURANT', ';'
        ]
        action_sequence, valid_actions = world.get_action_sequence_and_all_actions(
            sql)

        grammar_state = GrammarStatelet(['statement'],
                                        valid_actions,
                                        Text2SqlParser.is_nonterminal,
                                        reverse_productions=True)
        for action in action_sequence:
            grammar_state = grammar_state.take_action(action)
        assert grammar_state._nonterminal_stack == []  # pylint: disable=protected-access
Beispiel #8
0
 def test_take_action_crashes_with_mismatched_types(self):
     with pytest.raises(AssertionError):
         state = GrammarStatelet(['s'], {}, is_nonterminal)
         state.take_action('t -> identity')
 def test_take_action_crashes_with_mismatched_types(self):
     with pytest.raises(AssertionError):
         state = GrammarStatelet(['s'], {}, is_nonterminal)
         state.take_action('t -> identity')