From 9c5d49d7b4e6efa97d8e21b8000230893031ebad Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sun, 20 Dec 2020 17:15:37 -0500 Subject: [PATCH] Fixed to work with latest PySimpleGUI. Used really old code! Still needs version 0.8.7 of Chatterbot --- DemoPrograms/Demo_Chatterbot.py | 37 +++++++++++++++++++----- DemoPrograms/Demo_Chatterbot_With_TTS.py | 24 ++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/DemoPrograms/Demo_Chatterbot.py b/DemoPrograms/Demo_Chatterbot.py index 37c8edbf..67726c6d 100644 --- a/DemoPrograms/Demo_Chatterbot.py +++ b/DemoPrograms/Demo_Chatterbot.py @@ -1,11 +1,17 @@ #!/usr/bin/env python import PySimpleGUI as sg -from chatterbot import ChatBot import chatterbot.utils ''' Demo_Chatterbot.py + +Note - this code was written using version 0.8.7 of Chatterbot... to install: + +python -m pip install chatterbot==0.8.7 + +It still runs fine with the old version. + A GUI wrapped arouind the Chatterbot package. The GUI is used to show progress bars during the training process and to collect user input that is sent to the chatbot. The reply is displayed in the GUI window @@ -50,15 +56,32 @@ def print_progress_bar(description, iteration_counter, total_items, progress_bar # redefine the chatbot text based progress bar with a graphical one chatterbot.utils.print_progress_bar = print_progress_bar + +from chatterbot import ChatBot +from chatterbot.trainers import ChatterBotCorpusTrainer + +chatbot = ChatBot('Ron Obvious') + +# Create a new trainer for the chatbot +trainer = ChatterBotCorpusTrainer(chatbot) + +# Train based on the english corpus +trainer.train("chatterbot.corpus.english") + +# Train based on english greetings corpus +trainer.train("chatterbot.corpus.english.greetings") + +# Train based on the english conversations corpus +trainer.train("chatterbot.corpus.english.conversations") chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTrainer') # Train based on the english corpus -chatbot.train("chatterbot.corpus.english") +# chatbot.train("chatterbot.corpus.english") ################# GUI ################# -layout = [[sg.Output(size=(80, 20))], - [sg.MLine(size=(70, 5), enter_submits=True), +layout = [[sg.Multiline(size=(80, 20), reroute_stdout=True, echo_stdout_stderr=True)], + [sg.MLine(size=(70, 5), key='-MLINE IN-', enter_submits=True, do_not_clear=False), sg.Button('SEND', bind_return_key=True), sg.Button('EXIT')]] window = sg.Window('Chat Window', layout, @@ -66,11 +89,11 @@ window = sg.Window('Chat Window', layout, # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # while True: - event, (value,) = window.read() + event, values = window.read() if event != 'SEND': break - string = value.rstrip() + string = values['-MLINE IN-'].rstrip() print(' ' + string) # send the user input to chatbot to get a response - response = chatbot.get_response(value.rstrip()) + response = chatbot.get_response(values['-MLINE IN-'].rstrip()) print(response) diff --git a/DemoPrograms/Demo_Chatterbot_With_TTS.py b/DemoPrograms/Demo_Chatterbot_With_TTS.py index d1ceec8c..ca6775c9 100644 --- a/DemoPrograms/Demo_Chatterbot_With_TTS.py +++ b/DemoPrograms/Demo_Chatterbot_With_TTS.py @@ -9,6 +9,14 @@ import os ''' Demo_Chatterbot.py + + +Note - this code was written using version 0.8.7 of Chatterbot... to install: + +python -m pip install chatterbot==0.8.7 + +It still runs fine with the old version. + A GUI wrapped arouind the Chatterbot package. The GUI is used to show progress bars during the training process and to collect user input that is sent to the chatbot. The reply is displayed in the GUI window @@ -72,22 +80,22 @@ chatbot.train("chatterbot.corpus.english") ################# GUI ################# -layout = [[sg.Output(size=(80, 20))], - [sg.MLine(size=(70, 5), enter_submits=True), +layout = [[sg.Multiline(size=(80, 20), reroute_stdout=True, echo_stdout_stderr=True)], + [sg.MLine(size=(70, 5), key='-MLINE IN-', enter_submits=True, do_not_clear=False), sg.Button('SEND', bind_return_key=True), sg.Button('EXIT')]] -window = sg.Window('Chat Window', layout, default_element_size=(30, 2)) +window = sg.Window('Chat Window', layout, + default_element_size=(30, 2)) # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # while True: - event, (value,) = window.read() + event, values = window.read() if event != 'SEND': break - string = value.rstrip() - print(' '+string) + string = values['-MLINE IN-'].rstrip() + print(' ' + string) # send the user input to chatbot to get a response - response = chatbot.get_response(value.rstrip()) + response = chatbot.get_response(values['-MLINE IN-'].rstrip()) print(response) - speak(str(response)) window.close() \ No newline at end of file