Skip to content
Open
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
10 changes: 7 additions & 3 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,24 @@ def learn_response(self, statement, previous_statement=None):
"""
Learn that the statement provided is a valid response.
"""
from chatterbot.conversation import Statement

if not previous_statement:
previous_statement = statement.in_response_to

if not previous_statement:
previous_statement = self.get_latest_response(statement.conversation)
if previous_statement:
previous_statement = previous_statement.text

previous_statement_text = previous_statement

if not isinstance(previous_statement, (str, type(None), )):
if not isinstance(previous_statement, (Statement, str, type(None), )):
statement.in_response_to = previous_statement.text
elif isinstance(previous_statement, str):
statement.in_response_to = previous_statement
elif isinstance(previous_statement, Statement):
statement.in_response_to = previous_statement.text
statement.search_in_response_to = previous_statement.search_text
previous_statement_text = previous_statement.text

self.logger.info('Adding "{}" as a response to "{}"'.format(
statement.text,
Expand Down
44 changes: 27 additions & 17 deletions examples/learning_feedback_example.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from chatterbot import ChatBot
from chatterbot.conversation import Statement

"""
This example shows how to create a chat bot that
will learn responses based on an additional feedback
element from the user.
"""

from chatterbot import ChatBot
from chatterbot.conversation import Statement


# Uncomment the following line to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
Expand All @@ -18,38 +19,47 @@
)


def get_feedback():
def get_feedback(response_text: str, input_statement_text: str):
"""Get feedback

text = input()
Confirm whether response generated by bot is acceptable
:param response_text : response text generated by bot
:type response_text : str
:param input_statement_text: input statement text
:type input_statement_text: str
:return: True or False
:rtype: bool
"""

text = input('\n Is "{}" a coherent response to "{}"? \n'.format(
response_text,
input_statement_text
))

if 'yes' in text.lower():
return True
elif 'no' in text.lower():
return False
else:
print('Please type either "Yes" or "No"')
return get_feedback()
return get_feedback(response_text=response_text, input_statement_text=input_statement_text)


print('Type something to begin...')

# The following loop will execute each time the user enters input
while True:
try:
input_statement = Statement(text=input())
response = bot.generate_response(
input_statement = Statement(text=input("User: "))
response = bot.get_response(
input_statement
)

print('\n Is "{}" a coherent response to "{}"? \n'.format(
response.text,
input_statement.text
))
if get_feedback():
print('please input the correct one')
correct_response = Statement(text=input())
bot.learn_response(correct_response, input_statement)
print('Responses added to bot!')
if not get_feedback(response_text=response.text, input_statement_text=input_statement.text):
response = Statement(text=input("Please input the correct response: "))

bot.learn_response(response, input_statement)
print("Bot: {} # Added to bot".format(response.text))

# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
Expand Down