class BestMatchSentimentComparisonTestCase(ChatBotTestCase):
    """
    Integration tests for the BestMatch logic adapter
    using the similarity of sentiment polarity as a comparison function.
    """

    def setUp(self):
        super().setUp()
        from chatterbot.trainers import ListTrainer
        from chatterbot.comparisons import sentiment_comparison

        self.trainer = ListTrainer(
            self.chatbot,
            show_training_progress=False
        )

        self.adapter = BestMatch(
            self.chatbot,
            statement_comparison_function=sentiment_comparison
        )
        self.adapter.initialize()

    def test_exact_input(self):
        self.trainer.train([
            'What is your favorite flavor of ice cream?',
            'I enjoy raspberry ice cream.',
            'I am glad to hear that.',
            'Thank you.'
        ])

        happy_statement = Statement(text='I enjoy raspberry ice cream.')
        response = self.adapter.process(happy_statement)

        self.assertEqual(response.confidence, 1)
        self.assertEqual(response.text, 'I am glad to hear that.')

    def test_close_input(self):

        self.trainer.train([
            'What is your favorite flavor of ice cream?',
            'I enjoy raspberry ice cream.',
            'I am glad to hear that.',
            'Thank you.'
        ])

        happy_statement = Statement(text='I enjoy raspberry.')
        response = self.adapter.process(happy_statement)

        self.assertEqual(response.text, 'I am glad to hear that.')
        self.assertAlmostEqual(response.confidence, 0.75, places=1)
Exemple #2
0
class BestMatchSentimentComparisonTestCase(ChatBotTestCase):
    """
    Integration tests for the BestMatch logic adapter
    using the similarity of sentiment polarity as a comparison function.
    """

    def setUp(self):
        super(BestMatchSentimentComparisonTestCase, self).setUp()
        from chatterbot.trainers import ListTrainer
        from chatterbot.comparisons import sentiment_comparison

        self.trainer = ListTrainer(
            self.chatbot,
            show_training_progress=False
        )

        self.adapter = BestMatch(
            statement_comparison_function=sentiment_comparison
        )
        self.adapter.set_chatbot(self.chatbot)
        self.adapter.initialize()

    def test_exact_input(self):
        self.trainer.train([
            'What is your favorite flavor of ice cream?',
            'I enjoy raspberry ice cream.',
            'I am glad to hear that.',
            'Thank you.'
        ])

        happy_statement = Statement('I enjoy raspberry ice cream.')
        response = self.adapter.process(happy_statement)

        self.assertEqual(response.confidence, 1)
        self.assertEqual(response.text, 'I am glad to hear that.')

    def test_close_input(self):

        self.trainer.train([
            'What is your favorite flavor of ice cream?',
            'I enjoy raspberry ice cream.',
            'I am glad to hear that.',
            'Thank you.'
        ])

        happy_statement = Statement('I enjoy raspberry.')
        response = self.adapter.process(happy_statement)

        self.assertEqual(response.text, 'I am glad to hear that.')
        self.assertAlmostEqual(response.confidence, 0.75, places=1)
Exemple #3
0
class BestMatchSynsetDistanceTestCase(ChatBotTestCase):
    """
    Integration tests for the BestMatch logic adapter
    using the synset_distance comparison function.
    """
    def setUp(self):
        super(BestMatchSynsetDistanceTestCase, self).setUp()
        from chatterbot.comparisons import synset_distance

        self.adapter = BestMatch(statement_comparison_function=synset_distance)

        self.adapter.initialize()

        # Add a mock storage adapter to the logic adapter
        self.adapter.set_chatbot(self.chatbot)

    def test_get_closest_statement(self):
        """
        Note, the content of the in_response_to field for each of the
        test statements is only required because the logic adapter will
        filter out any statements that are not in response to a known statement.
        """
        possible_choices = [
            Statement('This is a lovely bog.',
                      in_response_to=[Response('This is a lovely bog.')]),
            Statement('This is a beautiful swamp.',
                      in_response_to=[Response('This is a beautiful swamp.')]),
            Statement('It smells like a swamp.',
                      in_response_to=[Response('It smells like a swamp.')])
        ]
        self.adapter.chatbot.storage.filter = MagicMock(
            return_value=possible_choices)

        statement = Statement('This is a lovely swamp.')
        match = self.adapter.get(statement)

        self.assertEqual('This is a lovely bog.', match)

    def test_different_punctuation(self):
        possible_choices = [
            Statement('Who are you?'),
            Statement('Are you good?'),
            Statement('You are good')
        ]
        self.adapter.chatbot.storage.get_response_statements = MagicMock(
            return_value=possible_choices)

        statement = Statement('Are you good')
        match = self.adapter.get(statement)

        self.assertEqual('Are you good?', match)

    def test_no_known_responses(self):
        """
        In the case that a match is selected which has no known responses.
        In this case a random response will be returned, but the confidence
        should be zero because it is a random choice.
        """
        self.adapter.chatbot.storage.update = MagicMock()
        self.adapter.chatbot.storage.count = MagicMock(return_value=1)
        self.adapter.chatbot.storage.get_random = MagicMock(
            return_value=Statement('Random'))

        match = self.adapter.process(Statement('Blah'))

        self.assertEqual(match.confidence, 0)
        self.assertEqual(match.text, 'Random')
class BestMatchSynsetDistanceTestCase(ChatBotTestCase):
    """
    Integration tests for the BestMatch logic adapter
    using the synset_distance comparison function.
    """

    def setUp(self):
        super().setUp()
        from chatterbot.comparisons import synset_distance

        self.adapter = BestMatch(
            self.chatbot,
            statement_comparison_function=synset_distance
        )
        self.adapter.initialize()

    def test_get_closest_statement(self):
        """
        Note, the content of the in_response_to field for each of the
        test statements is only required because the logic adapter will
        filter out any statements that are not in response to a known statement.
        """
        possible_choices = [
            Statement(text='This is a lovely bog.', in_response_to='This is a lovely bog.'),
            Statement(text='This is a beautiful swamp.', in_response_to='This is a beautiful swamp.'),
            Statement(text='It smells like a swamp.', in_response_to='It smells like a swamp.')
        ]
        self.adapter.chatbot.storage.filter = MagicMock(
            return_value=possible_choices
        )

        statement = Statement(text='This is a lovely swamp.')
        match = self.adapter.get(statement)

        self.assertEqual('This is a lovely bog.', match)

    def test_different_punctuation(self):
        possible_choices = [
            Statement(text='Who are you?'),
            Statement(text='Are you good?'),
            Statement(text='You are good')
        ]
        self.adapter.chatbot.storage.get_response_statements = MagicMock(
            return_value=possible_choices
        )

        statement = Statement(text='Are you good')
        match = self.adapter.get(statement)

        self.assertEqual('Are you good?', match)

    def test_no_known_responses(self):
        """
        In the case that a match is selected which has no known responses.
        In this case a random response will be returned, but the confidence
        should be zero because it is a random choice.
        """
        self.adapter.chatbot.storage.update = MagicMock()
        self.adapter.chatbot.storage.count = MagicMock(return_value=1)
        self.adapter.chatbot.storage.get_random = MagicMock(
            return_value=Statement(text='Random')
        )

        match = self.adapter.process(Statement(text='Blah'))

        self.assertEqual(match.confidence, 0)
        self.assertEqual(match.text, 'Random')