4
\$\begingroup\$

I'm wrote a basic chatterbot. What would you do to pretty much improve it and make it better than this?

Because I want this chatterbot to be the best it could be, and I really want other peoples' opinion of how they would improve it. Here is the code:

import tkinter as tk  

# --- functions ---

def cmd3():
    # default value at start
    x = None
    y = None
    
    # remove previous text
    label1['text'] = ""
    label2['text'] = ""
    
    text = text1.get()
    
    if text == "":
        label1['text'] = "Statement"
    elif text == "Hello":
        x = "Hello"
    elif text == "Goodbye":
        x = "Goodbye"
    elif text == "Hi":
        x = "Hello"
    else:
        label1['text'] = "Please enter a valid statement"

    text = text2.get()
     
    if text == "":
        label2['text'] = "Question"
    elif text == "How are You?":
        y = "I'm going good"
    elif text == "What is your name?":
        y = "I'm Turing"
    elif text == "When did your Creator start working on you?":
        y = "In the year 2022"
    elif text == "Do you like Star Wars?":
        y = "Yes, I was actually inspired by the personallities from droids from Star Wars."
    else:
        label2['text'] = "please enter a valid question"
        
    print(x, y)


    if (x is not None) and (y is not None):
        label_result['text'] = str(x + " " + y)

# --- main ---

new = tk.Tk()


label1 = tk.Label(new, text="", fg="red")
label1.pack()

text1 = tk.Entry(new, width="60")  
text1.pack()


label2 = tk.Label(new, text="", fg="red")
label2.pack()

text2 = tk.Entry(new, width="60")  
text2.pack()
        
btn3 = tk.Button(new, text="Enter", command=cmd3)
btn3.pack()   


label_result = tk.Label(new, text="", fg="green")
label_result.pack()

new.mainloop()
\$\endgroup\$
1
  • \$\begingroup\$ chatterbot = chatbot \$\endgroup\$ Commented Mar 28, 2022 at 19:59

1 Answer 1

3
\$\begingroup\$

cmd3 is not a very good function name. Consider instead something like on_enter.

x + " " + y does not need a str().

Consider writing a single response dictionary instead of multiple if statements.

I don't find your user interface to be very natural. An easy and intuitive interface for a chat bot would be a single Text widget that responds on each line.

You should probably ignore case when checking user questions.

Suggested

import tkinter as tk

RESPONSES = {
    "how are you?":
        "I'm going good",
    "what is your name?":
        "I'm Turing",
    "when did your creator start working on you?":
        "In the year 2022",
    "do you like star wars?":
        "Yes, I was actually inspired by the personalities from droids from Star Wars."
}


def setup(root: tk.Tk) -> None:
    def keyup(event: tk.Event):
        if event.keysym != 'Return':
            return

        nonlocal text
        line = text.get('end -2 lines', 'end').strip().lower()
        response = RESPONSES.get(line, 'Please enter a valid statement')
        text.insert('end', response + '\n', 'response')

    text = tk.Text(root)
    text.pack(fill=tk.BOTH, expand=True)
    text.tag_configure('response', foreground='green')
    text.bind('<KeyRelease>', keyup)


def main() -> None:
    root = tk.Tk()
    setup(root)
    root.mainloop()


if __name__ == '__main__':
    main()

Output

example run

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.