From d3f8575e1268e4e756a27931763bd598ed9b1e24 Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Mon, 25 Jan 2016 17:46:10 -0500 Subject: [PATCH] Added tests for closest match adapter confidence. These tests ensure that the confidence value returned by the closest match adapter range between 0 and 1. --- chatterbot/adapters/logic/closest_match.py | 3 ++ .../logic_adapter_tests/test_closest_match.py | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/chatterbot/adapters/logic/closest_match.py b/chatterbot/adapters/logic/closest_match.py index 903efddaa..351182952 100644 --- a/chatterbot/adapters/logic/closest_match.py +++ b/chatterbot/adapters/logic/closest_match.py @@ -35,6 +35,9 @@ def get(self, input_statement, statement_list=None): limit=1 )[0] + # Convert the confidence integer to a percent + confidence /= 100.0 + return confidence, next( (s for s in statement_list if s.text == closest_match), None ) diff --git a/tests/logic_adapter_tests/test_closest_match.py b/tests/logic_adapter_tests/test_closest_match.py index 8bc21beeb..883e940ce 100644 --- a/tests/logic_adapter_tests/test_closest_match.py +++ b/tests/logic_adapter_tests/test_closest_match.py @@ -37,3 +37,41 @@ def test_get_closest_statement(self): 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?")]) + ] + + statement = Statement("What is your quest?") + + confidence, match = self.adapter.get( + statement, possible_choices + ) + + self.assertEqual(confidence, 1) + + def test_confidence_half_match(self): + possible_choices = [ + Statement("xxyy", in_response_to=[Response("xxyy")]) + ] + + statement = Statement("wwxx") + + confidence, match = self.adapter.get( + statement, possible_choices + ) + + self.assertEqual(confidence, 0.5) + + def test_confidence_no_match(self): + possible_choices = [ + Statement("xxx", in_response_to=[Response("xxx")]) + ] + + statement = Statement("yyy") + + confidence, match = self.adapter.get( + statement, possible_choices + ) + + self.assertEqual(confidence, 0)