Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a list of options to be passed to the low confidence adapter #1178

Merged
merged 2 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chatterbot/logic/best_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BestMatch(LogicAdapter):
"""
A logic adater that returns a response based on known responses to
A logic adapter that returns a response based on known responses to
the closest matches to the input statement.
"""

Expand Down
26 changes: 20 additions & 6 deletions chatterbot/logic/low_confidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,31 @@ class LowConfidenceAdapter(BestMatch):
* *threshold* (``float``) --
The low confidence value that triggers this adapter.
Defaults to 0.65.
* *default_response* (``str``) --
* *default_response* (``str``) or (``iterable``)--
The response returned by this logic adaper.
* *response_selection_method* (``str``) or (``callable``)
The a response selection method.
Defaults to ``get_first_response``.
"""

def __init__(self, **kwargs):
super(LowConfidenceAdapter, self).__init__(**kwargs)

self.confidence_threshold = kwargs.get('threshold', 0.65)

default_response = kwargs.get(
default_responses = kwargs.get(
'default_response', "I'm sorry, I do not understand."
)

self.default_response = Statement(text=default_response)
# Convert a single string into a list
if isinstance(default_responses, str):
default_responses = [
default_responses
]

self.default_responses = [
Statement(text=default) for default in default_responses
]

def process(self, input_statement):
"""
Expand All @@ -35,10 +46,13 @@ def process(self, input_statement):
# Select the closest match to the input statement
closest_match = self.get(input_statement)

# Choose a response from the list of options
response = self.select_response(input_statement, self.default_responses)

# Confidence should be high only if it is less than the threshold
if closest_match.confidence < self.confidence_threshold:
self.default_response.confidence = 1
response.confidence = 1
else:
self.default_response.confidence = 0
response.confidence = 0

return self.default_response
return response
18 changes: 16 additions & 2 deletions tests/logic_adapter_tests/test_low_confidence_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_high_confidence(self):
match = self.adapter.process(statement)

self.assertEqual(match.confidence, 0)
self.assertEqual(match, self.adapter.default_response)
self.assertEqual(match, self.adapter.default_responses[0])

def test_low_confidence(self):
"""
Expand All @@ -56,4 +56,18 @@ def test_low_confidence(self):
match = self.adapter.process(statement)

self.assertEqual(match.confidence, 1)
self.assertEqual(match, self.adapter.default_response)
self.assertEqual(match, self.adapter.default_responses[0])

def test_low_confidence_options_list(self):
"""
Test the case that a high confidence response is not known.
"""
self.adapter.default_responses = [
Statement(text='No')
]

statement = Statement('Is this a tomato?')
match = self.adapter.process(statement)

self.assertEqual(match.confidence, 1)
self.assertEqual(match, 'No')
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_low_confidence(self):

response = adapter.process(statement)

self.assertEqual(response.text, adapter.default_response)
self.assertEqual(response.text, adapter.default_responses[0])

def test_mathematical_evaluation(self):
from chatterbot.logic import MathematicalEvaluation
Expand Down