-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b08b3d4
commit 0f55cd9
Showing
2 changed files
with
59 additions
and
1 deletion.
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
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,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 |