Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new low-confidence response logic adapter
- Loading branch information
1 parent
8fea7ba
commit f52f363
Showing
6 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from .logic_adapter import LogicAdapter | ||
from .approximate_sentence_match import ApproximateSentenceMatchAdapter | ||
from .closest_match import ClosestMatchAdapter | ||
from .closest_meaning import ClosestMeaningAdapter | ||
from .time_adapter import TimeLogicAdapter | ||
from .low_confidence import LowConfidenceAdapter | ||
from .mathematical_evaluation import MathematicalEvaluation | ||
from .multi_adapter import MultiLogicAdapter | ||
from .no_knowledge_adapter import NoKnowledgeAdapter | ||
from .mathematical_evaluation import MathematicalEvaluation | ||
from .approximate_sentence_match import ApproximateSentenceMatchAdapter | ||
from .sentiment_adapter import SentimentAdapter | ||
from .time_adapter import TimeLogicAdapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
from chatterbot.conversation import Statement | ||
from .base_match import BaseMatchAdapter | ||
|
||
|
||
class LowConfidenceAdapter(BaseMatchAdapter): | ||
""" | ||
Returns a default response with a high confidence | ||
when a high confidence response is not known. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
super(LowConfidenceAdapter, self).__init__(**kwargs) | ||
|
||
self.confidence_threshold = kwargs.get('threshold', 0.65) | ||
self.default_response = kwargs.get( | ||
'default_response', | ||
"I'm sorry, I do not understand." | ||
) | ||
|
||
def process(self, input_statement): | ||
""" | ||
Return a default response with a high confidence if | ||
a high confidence response is not known. | ||
""" | ||
# Select the closest match to the input statement | ||
confidence, closest_match = self.get(input_statement) | ||
|
||
# Confidence should be high only if it is less than the threshold | ||
if confidence < self.confidence_threshold: | ||
confidence = 1 | ||
else: | ||
confidence = 0 | ||
|
||
return confidence, Statement(self.default_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from chatterbot import ChatBot | ||
|
||
|
||
# Create a new instance of a ChatBot | ||
bot = ChatBot( | ||
'Default Response Example Bot', | ||
storage_adapter='chatterbot.adapters.storage.JsonFileStorageAdapter', | ||
logic_adapters=[ | ||
{ | ||
'import_path': 'chatterbot.adapters.logic.ClosestMatchAdapter' | ||
}, | ||
{ | ||
'import_path': 'chatterbot.adapters.logic.LowConfidenceAdapter', | ||
'threshold': 0.65, | ||
'default_response': 'I am sorry, but I do not understand.' | ||
} | ||
], | ||
trainer='chatterbot.trainers.ListTrainer' | ||
) | ||
|
||
# Train the chat bot with a few responses | ||
bot.train([ | ||
'How can I help you?', | ||
'I want to create a chat bot', | ||
'Have you read the documentation?', | ||
'No, I have not', | ||
'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html' | ||
]) | ||
|
||
# Get a response for some unexpected input | ||
response = bot.get_response('How do I make an omelette?') | ||
print(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from unittest import TestCase | ||
from mock import MagicMock | ||
from chatterbot.adapters.logic import LowConfidenceAdapter | ||
from chatterbot.conversation import Statement, Response | ||
from .test_closest_match import MockContext | ||
|
||
|
||
class LowConfidenceAdapterTestCase(TestCase): | ||
""" | ||
Test cases for the LowConfidenceAdapter | ||
""" | ||
|
||
def setUp(self): | ||
super(LowConfidenceAdapterTestCase, self).setUp() | ||
self.adapter = LowConfidenceAdapter() | ||
|
||
# Add a mock storage adapter to the context | ||
self.adapter.set_context(MockContext()) | ||
|
||
possible_choices = [ | ||
Statement('Who do you love?', in_response_to=[ | ||
Response('I hear you are going on a quest?') | ||
]), | ||
Statement('What is the meaning of life?', in_response_to=[ | ||
Response('Yuck, black licorice jelly beans.') | ||
]), | ||
Statement('I am Iron Man.', in_response_to=[ | ||
Response('What... is your quest?') | ||
]), | ||
Statement('What... is your quest?', in_response_to=[ | ||
Response('I am Iron Man.') | ||
]), | ||
Statement('Yuck, black licorice jelly beans.', in_response_to=[ | ||
Response('What is the meaning of life?') | ||
]), | ||
Statement('I hear you are going on a quest?', in_response_to=[ | ||
Response('Who do you love?') | ||
]), | ||
] | ||
self.adapter.context.storage.filter = MagicMock(return_value=possible_choices) | ||
|
||
def test_high_confidence(self): | ||
""" | ||
Test the case that I high confidence response is known. | ||
""" | ||
statement = Statement('What is your quest?') | ||
confidence, match = self.adapter.process(statement) | ||
|
||
self.assertEqual(confidence, 0) | ||
self.assertEqual(match, self.adapter.default_response) | ||
|
||
def test_low_confidence(self): | ||
""" | ||
Test the case that a high confidence response is not known. | ||
""" | ||
statement = Statement('Is this a tomato?') | ||
confidence, match = self.adapter.process(statement) | ||
|
||
self.assertEqual(confidence, 1) | ||
self.assertEqual(match, self.adapter.default_response) |