|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + |
| 4 | +@description: |
| 5 | +""" |
| 6 | + |
| 7 | +from chatterbot import ChatBot |
| 8 | +from chatterbot.conversation import Statement |
| 9 | +from chatterbot.trainers import ListTrainer |
| 10 | +import logging |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | +""" |
| 13 | +This example shows how to create a chat bot that |
| 14 | +will learn responses based on an additional feedback |
| 15 | +element from the user. |
| 16 | +""" |
| 17 | + |
| 18 | +# Uncomment the following line to enable verbose logging |
| 19 | +# import logging |
| 20 | +# logging.basicConfig(level=logging.INFO) |
| 21 | + |
| 22 | +# Create a new instance of a ChatBot |
| 23 | +bot = ChatBot( |
| 24 | + 'Feedback Learning Bot', |
| 25 | + logic_adapters=[ |
| 26 | + { |
| 27 | + 'import_path': 'chatterbot.logic.BestMatch', |
| 28 | + 'default_response': 'I am sorry, but I do not understand.', |
| 29 | + 'maximum_similarity_threshold': 0.90 |
| 30 | + } |
| 31 | + ], |
| 32 | + storage_adapter='chatterbot.storage.SQLStorageAdapter' |
| 33 | +) |
| 34 | + |
| 35 | +trainer = ListTrainer(bot) |
| 36 | + |
| 37 | +trainer.train([ |
| 38 | + "Hi, can I help you?", |
| 39 | + "Sure, I'd like to book a flight to Iceland.", |
| 40 | + "Your flight has been booked.", |
| 41 | + 'hi', |
| 42 | + 'hello', |
| 43 | + 'what is your sex?', |
| 44 | + 'female', |
| 45 | + 'bye', |
| 46 | + 'byebye' |
| 47 | +]) |
| 48 | + |
| 49 | + |
| 50 | +def get_feedback(): |
| 51 | + text = input() |
| 52 | + |
| 53 | + if 'yes' in text.lower() or 'y' in text.lower(): |
| 54 | + return True |
| 55 | + elif 'no' in text.lower() or 'n' in text.lower(): |
| 56 | + return False |
| 57 | + else: |
| 58 | + print('Please type either "yes" or "no"') |
| 59 | + return get_feedback() |
| 60 | + |
| 61 | + |
| 62 | +print('Type something to begin...') |
| 63 | + |
| 64 | +# The following loop will execute each time the user enters input |
| 65 | +while True: |
| 66 | + try: |
| 67 | + input_statement = Statement(text=input()) |
| 68 | + response = bot.get_response(input_statement) |
| 69 | + print(response) |
| 70 | + print('\n Is "{}" a right response to "{}"? \n'.format( |
| 71 | + response.text, |
| 72 | + input_statement.text |
| 73 | + )) |
| 74 | + if not get_feedback(): |
| 75 | + print('please input the correct one') |
| 76 | + correct_response = Statement(text=input()) |
| 77 | + bot.learn_response(correct_response, input_statement) |
| 78 | + print('Responses added to bot!') |
| 79 | + |
| 80 | + # Press ctrl-c or ctrl-d on the keyboard to exit |
| 81 | + except (KeyboardInterrupt, EOFError, SystemExit): |
| 82 | + break |
0 commit comments