Skip to content

Commit

Permalink
Merge pull request #113 from gunthercox/confidence
Browse files Browse the repository at this point in the history
Make sure that closest match adapter confidence returns value between 0 and 1
  • Loading branch information
DarkmatterVale committed Jan 26, 2016
2 parents 73945d4 + d3f8575 commit 249cc4d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chatterbot/adapters/logic/closest_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
38 changes: 38 additions & 0 deletions tests/logic_adapter_tests/test_closest_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 249cc4d

Please sign in to comment.