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 ) 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. """ self.chatbot.storage.create_many([ 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.') ]) 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): self.adapter.chatbot.storage.create_many([ Statement(text='Who are you?'), Statement(text='Are you good?'), Statement(text='You are good') ]) 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')
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) 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. """ self.chatbot.storage.create_many([ 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.') ]) 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): self.adapter.chatbot.storage.create_many([ Statement(text='Who are you?'), Statement(text='Are you good?'), Statement(text='You are good') ]) 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')
class BestMatchTestCase(ChatBotTestCase): """ Unit tests for the BestMatch logic adapter. """ def setUp(self): super().setUp() self.adapter = BestMatch(self.chatbot) def test_no_choices(self): """ An exception should be raised if there is no data in the database. """ statement = Statement(text='What is your quest?') response = self.adapter.get(statement) self.assertEqual(response.text, 'What is your quest?') self.assertEqual(response.confidence, 0) def test_excluded_words(self): """ Test that the logic adapter cannot return a response containing any of the listed words for exclusion. """ self.chatbot.storage.create(text='I like to count.') self.chatbot.storage.create(text='Counting is dumb.', in_response_to='I like to count.') self.chatbot.storage.create(text='Counting is fun!', in_response_to='I like to count.') self.adapter.excluded_words = ['dumb'] response = self.adapter.process(Statement(text='I like to count.')) self.assertEqual(response.confidence, 1) self.assertEqual(response.text, 'Counting is fun!')
class BestMatchTestCase(ChatBotTestCase): """ Unit tests for the BestMatch logic adapter. """ def setUp(self): super().setUp() self.adapter = BestMatch(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(text='What is your quest?') with self.assertRaises(BestMatch.EmptyDatasetException): self.adapter.get(statement)
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 BestMatchTestCase(ChatBotTestCase): """ Unit tests for the BestMatch logic adapter. """ def setUp(self): super().setUp() self.adapter = BestMatch(self.chatbot) def test_no_choices(self): """ An exception should be raised if there is no data in the database. """ statement = Statement(text='What is your quest?') response = self.adapter.get(statement) self.assertEqual(response.text, 'What is your quest?') self.assertEqual(response.confidence, 0) def test_excluded_words(self): """ Test that the logic adapter cannot return a response containing any of the listed words for exclusion. """ self.chatbot.storage.create( text='I like to count.' ) self.chatbot.storage.create( text='Counting is dumb.', in_response_to='I like to count.' ) self.chatbot.storage.create( text='Counting is fun!', in_response_to='I like to count.' ) self.adapter.excluded_words = ['dumb'] response = self.adapter.process(Statement(text='I like to count.')) self.assertEqual(response.confidence, 1) self.assertEqual(response.text, 'Counting is fun!')
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')) confidence, match = self.adapter.process(Statement('Blah')) self.assertEqual(confidence, 0) 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().setUp() from chatterbot.comparisons import levenshtein_distance self.adapter = BestMatch( self.chatbot, statement_comparison_function=levenshtein_distance) 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. """ self.chatbot.storage.create_many([ Statement(text='Who do you love?', in_response_to='I hear you are going on a quest?'), Statement(text='What is the meaning of life?', in_response_to='Yuck, black licorice jelly beans.'), Statement(text='I am Iron Man.', in_response_to='What... is your quest?'), Statement(text='What... is your quest?', in_response_to='I am Iron Man.'), Statement(text='Yuck, black licorice jelly beans.', in_response_to='What is the meaning of life?'), Statement(text='I hear you are going on a quest?', in_response_to='Who do you love?'), ]) statement = Statement(text='What is your quest?') match = self.adapter.get(statement) self.assertEqual('What... is your quest?', match) def test_confidence_exact_match(self): self.chatbot.storage.create(text='What is your quest?', in_response_to='What is your quest?') statement = Statement(text='What is your quest?') match = self.adapter.get(statement) self.assertEqual(match.confidence, 1) def test_confidence_half_match(self): self.chatbot.storage.create(text='xxyy', in_response_to='xxyy') statement = Statement(text='wwxx') match = self.adapter.get(statement) self.assertEqual(match.confidence, 0.5) def test_confidence_no_match(self): self.chatbot.storage.create(text='xxx', in_response_to='xxx') statement = Statement(text='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(text='Random')) match = self.adapter.process(Statement(text='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(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') ) confidence, match = self.adapter.process(Statement('Blah')) self.assertEqual(confidence, 0) self.assertEqual(match.confidence, 0) self.assertEqual(match.text, 'Random')