Skip to content

Commit

Permalink
Add learning example.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Oct 30, 2016
1 parent b08b3d4 commit 0f55cd9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
1 change: 0 additions & 1 deletion chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def generate_response(self, input_statement):

# Select a response to the input statement
confidence, response = self.logic.process(input_statement)
self.logger.info(u'Selecting "{}" as response with a confidence of {}'.format(response.text, confidence))

return input_statement, response, confidence

Expand Down
59 changes: 59 additions & 0 deletions examples/learning_feedback_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
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
import logging


# Uncomment the following line to enable verbose logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot('Feedback Learning Bot',
storage_adapter='chatterbot.adapters.storage.JsonFileStorageAdapter',
logic_adapters=[
'chatterbot.adapters.logic.ClosestMatchAdapter'
],
input_adapter='chatterbot.adapters.input.TerminalAdapter',
output_adapter='chatterbot.adapters.output.TerminalAdapter'
)

def get_feedback():
from chatterbot.utils.read_input import input_function

text = input_function()

if 'Yes' in text:
return True
elif 'No' in text:
return False
else:
print('Please type either "Yes" or "No"')
return get_feedback()

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

# The following loop will execute each time the user enters input
while True:
try:
input_statement = bot.input.process_input_statement()
statement, response, confidence = bot.generate_response(input_statement)

print('\n Is "{}" this a coherent response to "{}"? \n'.format(response, input_statement))

if get_feedback():
bot.learn_response(response)

bot.output.process_response(response, confidence)

# Update the conversation history for the bot
# It is important that this happens last, after the learning step
bot.recent_statements.append(
(statement, response, )
)

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

0 comments on commit 0f55cd9

Please sign in to comment.