chatterbot_train.py (2117B)
1 from chatterbot import ChatBot 2 from chatterbot.conversation import Statement 3 import en_core_web_sm 4 5 # Uncomment the following line to enable verbose logging 6 # import logging 7 # logging.basicConfig(level=logging.INFO) 8 9 # Create a new instance of a ChatBot 10 cbot = ChatBot( 11 'Sylvie', 12 storage_adapter='chatterbot.storage.SQLStorageAdapter', 13 logic_adapters=[ 14 'chatterbot.logic.MathematicalEvaluation', 15 #'chatterbot.logic.TimeLogicAdapter', 16 'chatterbot.logic.BestMatch' 17 ], 18 database_uri='sqlite:///sylvie_db.sqlite3' 19 ) 20 21 learning_mode = False 22 23 def get_feedback(): 24 25 text = input() 26 27 if 'yes' in text.lower(): 28 return True 29 elif 'no' in text.lower(): 30 return False 31 else: 32 print('Please type either "Yes" or "No"') 33 return get_feedback() 34 35 # The following loop will execute each time the user enters input 36 while True: 37 try: 38 user_input = input("Say something to Sylvie: ") 39 40 if user_input.lower() == "engage in learning mode": 41 print('Learning mode engaged.') 42 learning_mode = True 43 continue 44 elif user_input.lower() == "exit learning mode": 45 print('Exiting learning mode.') 46 learning_mode = False 47 continue 48 49 if learning_mode == True: 50 51 input_statement = Statement(user_input) 52 response = cbot.generate_response( 53 input_statement 54 ) 55 56 print('\n Is "{}" a coherent response to "{}"? \n'.format( 57 response.text, 58 input_statement.text 59 )) 60 if get_feedback() is False: 61 print('Please input the correct one.') 62 correct_response = Statement(text=input()) 63 cbot.learn_response(correct_response, input_statement) 64 print('Responses added to bot!') 65 66 else: 67 bot_response = cbot.get_response(user_input) 68 print(bot_response) 69 70 # Press ctrl-c or ctrl-d on the keyboard to exit 71 except (KeyboardInterrupt, EOFError, SystemExit): 72 break