From 9e125a67cad2a2da5c83cad2a50f03d28b77bd3a Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Mon, 9 Jan 2017 22:47:55 -0500 Subject: [PATCH] Update tests for confidence attribute --- chatterbot/logic/best_match.py | 6 ++++-- chatterbot/logic/specific_response.py | 8 ++++---- .../test_levenshtein_distance.py | 4 ++++ .../test_sentiment_comparison.py | 2 ++ .../test_synset_distance.py | 1 + .../test_low_confidence_adapter.py | 2 ++ .../test_mathematical_evaluation.py | 15 +++++++++++++++ tests/logic_adapter_tests/test_multi_adapter.py | 1 + .../logic_adapter_tests/test_specific_response.py | 2 ++ tests/logic_adapter_tests/test_time.py | 2 ++ 10 files changed, 37 insertions(+), 6 deletions(-) diff --git a/chatterbot/logic/best_match.py b/chatterbot/logic/best_match.py index a63a1d14f..c77600ccc 100644 --- a/chatterbot/logic/best_match.py +++ b/chatterbot/logic/best_match.py @@ -22,7 +22,9 @@ def get(self, input_statement): 'No statements have known responses. ' + 'Choosing a random response to return.' ) - return 0, self.chatbot.storage.get_random() + random_response = self.chatbot.storage.get_random() + random_response.confidence = 0 + return random_response.confidence, random_response else: raise self.EmptyDatasetException() @@ -37,6 +39,7 @@ def get(self, input_statement): max_confidence = confidence closest_match = statement + closest_match.confidence = max_confidence return max_confidence, closest_match def can_process(self, statement): @@ -81,5 +84,4 @@ def process(self, input_statement): # Set confidence to zero because a random response is selected confidence = 0 - response.confidence = confidence return confidence, response diff --git a/chatterbot/logic/specific_response.py b/chatterbot/logic/specific_response.py index cb5ffb0b0..aad91e81e 100644 --- a/chatterbot/logic/specific_response.py +++ b/chatterbot/logic/specific_response.py @@ -23,10 +23,10 @@ def can_process(self, statement): return False def process(self, statement): - confidence = 0 if statement == self.input_text: - confidence = 1 + self.response_statement.confidence = 1 + else: + self.response_statement.confidence = 0 - self.response_statement.confidence = confidence - return confidence, self.response_statement + return self.response_statement.confidence, self.response_statement diff --git a/tests/logic_adapter_tests/best_match_integration_tests/test_levenshtein_distance.py b/tests/logic_adapter_tests/best_match_integration_tests/test_levenshtein_distance.py index 093ea22fd..37ae8b2fe 100644 --- a/tests/logic_adapter_tests/best_match_integration_tests/test_levenshtein_distance.py +++ b/tests/logic_adapter_tests/best_match_integration_tests/test_levenshtein_distance.py @@ -52,6 +52,7 @@ def test_confidence_exact_match(self): confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 1) + self.assertEqual(match.confidence, 1) def test_confidence_half_match(self): possible_choices = [ @@ -63,6 +64,7 @@ def test_confidence_half_match(self): confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 0.5) + self.assertEqual(match.confidence, 0.5) def test_confidence_no_match(self): possible_choices = [ @@ -74,6 +76,7 @@ def test_confidence_no_match(self): confidence, match = self.adapter.get(statement) self.assertEqual(confidence, 0) + self.assertEqual(match.confidence, 0) def test_no_known_responses(self): """ @@ -90,4 +93,5 @@ def test_no_known_responses(self): confidence, match = self.adapter.process(Statement("Blah")) self.assertEqual(confidence, 0) + self.assertEqual(match.confidence, 0) self.assertEqual(match.text, "Random") diff --git a/tests/logic_adapter_tests/best_match_integration_tests/test_sentiment_comparison.py b/tests/logic_adapter_tests/best_match_integration_tests/test_sentiment_comparison.py index 12671d826..fd7fffa88 100644 --- a/tests/logic_adapter_tests/best_match_integration_tests/test_sentiment_comparison.py +++ b/tests/logic_adapter_tests/best_match_integration_tests/test_sentiment_comparison.py @@ -32,6 +32,7 @@ def test_exact_input(self): 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): @@ -48,3 +49,4 @@ def test_close_input(self): 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) diff --git a/tests/logic_adapter_tests/best_match_integration_tests/test_synset_distance.py b/tests/logic_adapter_tests/best_match_integration_tests/test_synset_distance.py index a79ccebc1..57a5373c0 100644 --- a/tests/logic_adapter_tests/best_match_integration_tests/test_synset_distance.py +++ b/tests/logic_adapter_tests/best_match_integration_tests/test_synset_distance.py @@ -76,4 +76,5 @@ def test_no_known_responses(self): confidence, match = self.adapter.process(Statement('Blah')) self.assertEqual(confidence, 0) + self.assertEqual(match.confidence, 0) self.assertEqual(match.text, 'Random') diff --git a/tests/logic_adapter_tests/test_low_confidence_adapter.py b/tests/logic_adapter_tests/test_low_confidence_adapter.py index b72531965..ba278fc92 100644 --- a/tests/logic_adapter_tests/test_low_confidence_adapter.py +++ b/tests/logic_adapter_tests/test_low_confidence_adapter.py @@ -47,6 +47,7 @@ def test_high_confidence(self): confidence, match = self.adapter.process(statement) self.assertEqual(confidence, 0) + self.assertEqual(match.confidence, 0) self.assertEqual(match, self.adapter.default_response) def test_low_confidence(self): @@ -57,4 +58,5 @@ def test_low_confidence(self): confidence, match = self.adapter.process(statement) self.assertEqual(confidence, 1) + self.assertEqual(match.confidence, 1) self.assertEqual(match, self.adapter.default_response) diff --git a/tests/logic_adapter_tests/test_mathematical_evaluation.py b/tests/logic_adapter_tests/test_mathematical_evaluation.py index bfa351c3c..f546f6592 100644 --- a/tests/logic_adapter_tests/test_mathematical_evaluation.py +++ b/tests/logic_adapter_tests/test_mathematical_evaluation.py @@ -65,20 +65,24 @@ def test_addition_operator(self): statement = Statement('What is 100 + 54?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( 100 + 54 ) = 154') + self.assertEqual(response.confidence, 1) def test_subtraction_operator(self): statement = Statement('What is 100 - 58?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( 100 - 58 ) = 42') + self.assertEqual(response.confidence, 1) def test_multiplication_operator(self): statement = Statement('What is 100 * 20') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( 100 * 20 ) = 2000') + self.assertEqual(response.confidence, 1) def test_division_operator(self): statement = Statement('What is 100 / 20') confidence, response = self.adapter.process(statement) + self.assertEqual(response.confidence, 1) if self.python_version <= 2: self.assertEqual(response.text, '( 100 / 20 ) = 5') @@ -89,16 +93,19 @@ def test_parenthesized_multiplication_and_addition(self): statement = Statement('What is 100 + ( 1000 * 2 )?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( 100 + ( ( 1000 * ( 2 ) ) ) ) = 2100') + self.assertEqual(response.confidence, 1) def test_parenthesized_with_words(self): statement = Statement('What is four plus 100 + ( 100 * 2 )?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( 4 + ( 100 + ( ( 100 * ( 2 ) ) ) ) ) = 304') + self.assertEqual(response.confidence, 1) def test_word_numbers_addition(self): statement = Statement('What is one hundred + four hundred?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( 100 + 400 ) = 500') + self.assertEqual(response.confidence, 1) def test_word_division_operator(self): statement = Statement('What is 100 divided by 100?') @@ -109,6 +116,8 @@ def test_word_division_operator(self): else: self.assertEqual(response.text, '( 100 / 100 ) = 1.0') + self.assertEqual(response.confidence, 1) + def test_large_word_division_operator(self): statement = Statement('What is one thousand two hundred four divided by one hundred?') confidence, response = self.adapter.process(statement) @@ -118,23 +127,29 @@ def test_large_word_division_operator(self): else: self.assertEqual(response.text, '( 1000 + 200 + 4 ) / ( 100 ) = 12.04') + self.assertEqual(response.confidence, 1) + def test_negative_multiplication(self): statement = Statement('What is -105 * 5') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( -105 * 5 ) = -525') + self.assertEqual(response.confidence, 1) def test_negative_decimal_multiplication(self): statement = Statement('What is -100.5 * 20?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '( -100.5 * 20 ) = -2010.0') + self.assertEqual(response.confidence, 1) def test_constants(self): statement = Statement('What is pi plus e ?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, '3.141693 + 2.718281 = 5.859974') + self.assertEqual(response.confidence, 1) def test_math_functions(self): statement = Statement('What is log ( 5 + 6 ) * sqrt ( 12 ) ?') confidence, response = self.adapter.process(statement) self.assertEqual(response.text, 'log ( ( 5 + ( 6 ) * sqrt ( ( 12 ) ) ) ) = 3.24977779033') + self.assertEqual(response.confidence, 1) diff --git a/tests/logic_adapter_tests/test_multi_adapter.py b/tests/logic_adapter_tests/test_multi_adapter.py index 58185b296..cda696603 100644 --- a/tests/logic_adapter_tests/test_multi_adapter.py +++ b/tests/logic_adapter_tests/test_multi_adapter.py @@ -42,6 +42,7 @@ def test_sub_adapter_agreement(self): confidence, statement = self.adapter.process(Statement('Howdy!')) self.assertEqual(confidence, 0.5) + self.assertEqual(statement.confidence, 0.5) self.assertEqual(statement, 'Good morning.') def test_get_greatest_confidence(self): diff --git a/tests/logic_adapter_tests/test_specific_response.py b/tests/logic_adapter_tests/test_specific_response.py index b610dc935..ee216e27f 100644 --- a/tests/logic_adapter_tests/test_specific_response.py +++ b/tests/logic_adapter_tests/test_specific_response.py @@ -23,6 +23,7 @@ def test_exact_match(self): confidence, match = self.adapter.process(statement) self.assertEqual(confidence, 1) + self.assertEqual(match.confidence, 1) self.assertEqual(match, self.adapter.response_statement) def test_not_exact_match(self): @@ -33,4 +34,5 @@ def test_not_exact_match(self): confidence, match = self.adapter.process(statement) self.assertEqual(confidence, 0) + self.assertEqual(match.confidence, 0) self.assertEqual(match, self.adapter.response_statement) diff --git a/tests/logic_adapter_tests/test_time.py b/tests/logic_adapter_tests/test_time.py index 2d21e3c6a..f5359112f 100644 --- a/tests/logic_adapter_tests/test_time.py +++ b/tests/logic_adapter_tests/test_time.py @@ -13,6 +13,7 @@ def test_positive_input(self): confidence, response = self.adapter.process(statement) self.assertEqual(confidence, 1) + self.assertEqual(response.confidence, 1) self.assertIn("The current time is ", response.text) def test_negative_input(self): @@ -20,5 +21,6 @@ def test_negative_input(self): confidence, response = self.adapter.process(statement) self.assertEqual(confidence, 0) + self.assertEqual(response.confidence, 0) self.assertIn("The current time is ", response.text)