Skip to content

Commit

Permalink
Update tests for confidence attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jan 10, 2017
1 parent f22a075 commit 9e125a6
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 6 deletions.
6 changes: 4 additions & 2 deletions chatterbot/logic/best_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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):
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions chatterbot/logic/specific_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand All @@ -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):
"""
Expand All @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 2 additions & 0 deletions tests/logic_adapter_tests/test_low_confidence_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
15 changes: 15 additions & 0 deletions tests/logic_adapter_tests/test_mathematical_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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?')
Expand All @@ -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)
Expand All @@ -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)

1 change: 1 addition & 0 deletions tests/logic_adapter_tests/test_multi_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/logic_adapter_tests/test_specific_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
2 changes: 2 additions & 0 deletions tests/logic_adapter_tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ 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):
statement = Statement("What is an example of a pachyderm?")
confidence, response = self.adapter.process(statement)

self.assertEqual(confidence, 0)
self.assertEqual(response.confidence, 0)
self.assertIn("The current time is ", response.text)

0 comments on commit 9e125a6

Please sign in to comment.