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.chatbot.set_trainer(ListTrainer) self.adapter = BestMatch( statement_comparison_function=sentiment_comparison ) self.adapter.set_chatbot(self.chatbot) def test_exact_input(self): self.chatbot.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.') confidence, response = self.adapter.process(happy_statement) self.assertEqual(confidence, 1) self.assertEqual(response.confidence, 1) self.assertEqual(response.text, 'I am glad to hear that.') def test_close_input(self): self.chatbot.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.') confidence, response = self.adapter.process(happy_statement) self.assertEqual(response.text, 'I am glad to hear that.') self.assertAlmostEqual(confidence, 0.75, places=1) self.assertAlmostEqual(response.confidence, 0.75, places=1)
def test_best_match(self): from chatterbot.logic import BestMatch adapter = BestMatch() adapter.set_chatbot(self.chatbot) statement1 = Statement(text='Do you like programming?') statement1.save() statement2 = Statement(text='Yes') statement2.save() response = Response(statement=statement1, response=statement2) response.save() response = adapter.process(statement1) self.assertEqual(response.text, 'Yes') self.assertEqual(response.confidence, 1)
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.chatbot.set_trainer(ListTrainer) self.adapter = BestMatch( statement_comparison_function=sentiment_comparison) self.adapter.set_chatbot(self.chatbot) def test_exact_input(self): self.chatbot.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.') confidence, response = self.adapter.process(happy_statement) self.assertEqual(confidence, 1) self.assertEqual(response.text, 'I am glad to hear that.') def test_close_input(self): self.chatbot.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.') confidence, response = self.adapter.process(happy_statement) self.assertEqual(response.text, 'I am glad to hear that.') self.assertAlmostEqual(confidence, 0.75, places=1)
class BestMatchTestCase(ChatBotTestCase): """ Unit tests for the BestMatch logic adapter. """ def setUp(self): super(BestMatchTestCase, self).setUp() self.adapter = BestMatch() self.adapter.set_chatbot(self.chatbot) def test_no_choices(self): """ An exception should be raised if there is no data in the database. """ self.adapter.chatbot.storage.filter = MagicMock(return_value=[]) self.adapter.chatbot.storage.count = MagicMock(return_value=0) statement = Statement('What is your quest?') with self.assertRaises(BestMatch.EmptyDatasetException): self.adapter.get(statement)
def test_best_match(self): from chatterbot.logic import BestMatch adapter = BestMatch() adapter.set_chatbot(self.chatbot) statement1 = Statement.objects.create( text='Do you like programming?', conversation='test' ) Statement.objects.create( text='Yes', in_response_to=statement1.text, conversation='test' ) response = adapter.process(statement1) self.assertEqual(response.text, 'Yes') self.assertEqual(response.confidence, 1)
class BestMatchTestCase(TestCase): """ Unit tests for the BestMatch logic adapter. """ def setUp(self): from chatterbot.comparisons import levenshtein_distance self.adapter = BestMatch() # Add a mock chatbot to the logic adapter self.adapter.set_chatbot(MockChatBot()) def test_no_choices(self): """ An exception should be raised if there is no data in the database. """ self.adapter.chatbot.storage.filter = MagicMock(return_value=[]) statement = Statement('What is your quest?') with self.assertRaises(BestMatch.EmptyDatasetException): self.adapter.get(statement)
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 BestMatchLevenshteinDistanceTestCase(TestCase): """ Integration tests for the BestMatch logic adapter using Levenshtein distance as a comparison function. """ def setUp(self): from chatterbot.comparisons import levenshtein_distance self.adapter = BestMatch( statement_comparison_function=levenshtein_distance) # Add a mock chatbot to the logic adapter self.adapter.set_chatbot(MockChatBot()) 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( "Who do you love?", in_response_to=[Response("I hear you are going on a quest?")]), Statement("What is the meaning of life?", in_response_to=[ Response("Yuck, black licorice jelly beans.") ]), Statement("I am Iron Man.", in_response_to=[Response("What... is your quest?")]), Statement("What... is your quest?", in_response_to=[Response("I am Iron Man.")]), Statement( "Yuck, black licorice jelly beans.", in_response_to=[Response("What is the meaning of life?")]), Statement("I hear you are going on a quest?", in_response_to=[Response("Who do you love?")]), ] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement("What is your quest?") confidence, match = self.adapter.get(statement) self.assertEqual("What... is your quest?", match) def test_confidence_exact_match(self): possible_choices = [ Statement("What is your quest?", in_response_to=[Response("What is your quest?")]) ] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement("What is your quest?") confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 1) def test_confidence_half_match(self): possible_choices = [ Statement("xxyy", in_response_to=[Response("xxyy")]) ] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement("wwxx") confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 0.5) def test_confidence_no_match(self): possible_choices = [Statement("xxx", in_response_to=[Response("xxx")])] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement("yyy") confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 0) 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.filter = MagicMock(return_value=[]) self.adapter.chatbot.storage.get_random = MagicMock( return_value=Statement("Random")) confidence, match = self.adapter.process(Statement("Blah")) self.assertEqual(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(BestMatchSynsetDistanceTestCase, self).setUp() from chatterbot.utils import nltk_download_corpus from chatterbot.comparisons import synset_distance nltk_download_corpus('stopwords') nltk_download_corpus('wordnet') nltk_download_corpus('punkt') self.adapter = BestMatch( statement_comparison_function=synset_distance ) # 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 BestMatchLevenshteinDistanceTestCase(ChatBotTestCase): """ Integration tests for the BestMatch logic adapter using Levenshtein distance as a comparison function. """ def setUp(self): super(BestMatchLevenshteinDistanceTestCase, self).setUp() from chatterbot.comparisons import levenshtein_distance self.adapter = BestMatch( statement_comparison_function=levenshtein_distance ) 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("Who do you love?", in_response_to=[Response("I hear you are going on a quest?")]), Statement("What is the meaning of life?", in_response_to=[Response("Yuck, black licorice jelly beans.")]), Statement("I am Iron Man.", in_response_to=[Response("What... is your quest?")]), Statement("What... is your quest?", in_response_to=[Response("I am Iron Man.")]), Statement("Yuck, black licorice jelly beans.", in_response_to=[Response("What is the meaning of life?")]), Statement("I hear you are going on a quest?", in_response_to=[Response("Who do you love?")]), ] self.adapter.chatbot.storage.filter = MagicMock(return_value=possible_choices) statement = Statement("What is your quest?") confidence, match = self.adapter.get(statement) self.assertEqual("What... is your quest?", match) def test_confidence_exact_match(self): possible_choices = [ Statement("What is your quest?", in_response_to=[Response("What is your quest?")]) ] self.adapter.chatbot.storage.filter = MagicMock(return_value=possible_choices) statement = Statement("What is your quest?") confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 1) def test_confidence_half_match(self): possible_choices = [ Statement("xxyy", in_response_to=[Response("xxyy")]) ] self.adapter.chatbot.storage.filter = MagicMock(return_value=possible_choices) statement = Statement("wwxx") confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 0.5) def test_confidence_no_match(self): possible_choices = [ Statement("xxx", in_response_to=[Response("xxx")]) ] self.adapter.chatbot.storage.filter = MagicMock(return_value=possible_choices) statement = Statement("yyy") confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 0) 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") ) confidence, match = self.adapter.process(Statement("Blah")) self.assertEqual(confidence, 0) self.assertEqual(match.text, "Random")
class BestMatchLevenshteinDistanceTestCase(ChatBotTestCase): """ Integration tests for the BestMatch logic adapter using Levenshtein distance as a comparison function. """ def setUp(self): super(BestMatchLevenshteinDistanceTestCase, self).setUp() from chatterbot.comparisons import levenshtein_distance self.adapter = BestMatch( statement_comparison_function=levenshtein_distance) 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('Who do you love?', in_response_to='I hear you are going on a quest?'), Statement('What is the meaning of life?', in_response_to='Yuck, black licorice jelly beans.'), Statement('I am Iron Man.', in_response_to='What... is your quest?'), Statement('What... is your quest?', in_response_to='I am Iron Man.'), Statement('Yuck, black licorice jelly beans.', in_response_to='What is the meaning of life?'), Statement('I hear you are going on a quest?', in_response_to='Who do you love?'), ] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement('What is your quest?') match = self.adapter.get(statement) self.assertEqual('What... is your quest?', match) def test_confidence_exact_match(self): possible_choices = [ Statement('What is your quest?', in_response_to='What is your quest?') ] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement('What is your quest?') match = self.adapter.get(statement) self.assertEqual(match.confidence, 1) def test_confidence_half_match(self): possible_choices = [Statement('xxyy', in_response_to='xxyy')] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement('wwxx') match = self.adapter.get(statement) self.assertEqual(match.confidence, 0.5) def test_confidence_no_match(self): possible_choices = [Statement('xxx', in_response_to='xxx')] self.adapter.chatbot.storage.filter = MagicMock( return_value=possible_choices) statement = Statement('yyy') match = self.adapter.get(statement) self.assertEqual(match.confidence, 0) 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')