Skip to content

Commit

Permalink
Added check to filter out any statements that do not have another
Browse files Browse the repository at this point in the history
statement that lists them in the in_response_to field.

This change prevents random responses from being returned when the
closest match to the input statement is not a response of any known
statement.
  • Loading branch information
gunthercox committed Nov 24, 2015
1 parent f1f5ab2 commit a5f73c0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
16 changes: 16 additions & 0 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ def get_response(self, input_text):

all_statements = self.storage.filter()

'''
Filter out all statements that are not in response to another statement.
A statement must exist which lists the closest matching statement in the
in_response_to field. Otherwise, the logic adapter may find a closest
matching statement that does not have a known response.
'''
for statement in all_statements:
response_exists = False
for s in all_statements:
if statement in s.in_response_to:
response_exists = True
break # Exit for loop since one exists

if not response_exists:
all_statements.remove(statement)

# Select the closest match to the input statement
closest_match = self.logic.get(
input_statement,
Expand Down
9 changes: 5 additions & 4 deletions tests/test_chatbot_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def test_statement_added_to_recent_response_list(self):
def test_response_known(self):
self.chatbot.storage.update(self.test_statement)

print "!!!!!", self.chatbot.storage.database.data()
response = self.chatbot.get_response("Hi")

self.assertEqual(response, self.test_statement.text)
Expand All @@ -170,12 +171,12 @@ def test_second_response_format(self):
response = self.chatbot.get_response("Hi")
# response = "Hello"
second_response = self.chatbot.get_response("How are you?")
statement_object = self.chatbot.storage.find(second_response)
statement = self.chatbot.storage.find(second_response)

self.assertEqual(second_response, self.test_statement.text)
#TODO: self.assertEqual(statement_object.get_response_count(), 2)
self.assertEqual(len(statement_object.in_response_to), 1)
self.assertIn("Hi", statement_object.in_response_to)
#TODO: self.assertEqual(statement.get_response_count(), 2)
self.assertEqual(len(statement.in_response_to), 1)
self.assertIn("Hi", statement.in_response_to)


class ChatterBotStorageIntegrationTests(UntrainedChatBotTestCase):
Expand Down
21 changes: 21 additions & 0 deletions tests/training_tests/test_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,24 @@ def test_training_with_unicode_characters(self):

self.assertEqual(response, conversation[2])

def test_similar_sentence_gets_same_response_multiple_times(self):
"""
Tests if the bot returns the same response for the same question (which
is similar to the one present in the training set) when asked repeatedly.
"""
training = [
'how do you login to gmail?',
'Goto gmail.com, enter your login information and hit enter!'
]

similar_question = 'how do I login to gmail?'

self.chatbot.train(training)

response_to_trained_set = self.chatbot.get_response('how do you login to gmail?')
response_to_similar_question_1 = self.chatbot.get_response(similar_question)
response_to_similar_question_2 = self.chatbot.get_response(similar_question)

self.assertEqual(response_to_trained_set, response_to_similar_question_1)
self.assertEqual(response_to_similar_question_1, response_to_similar_question_2)

0 comments on commit a5f73c0

Please sign in to comment.