-
Notifications
You must be signed in to change notification settings - Fork 298
/
simplebot_nltk.py
94 lines (72 loc) · 2.36 KB
/
simplebot_nltk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import tkinter.scrolledtext as tks #creates a scrollable text window
from datetime import datetime
from tkinter import *
# Generating response
def get_bot_response(user_input):
return bot_response
def create_and_insert_user_frame(user_input):
userFrame = Frame(chatWindow, bg="#d0ffff")
Label(
userFrame,
text=user_input,
font=("Arial", 11),
bg="#d0ffff").grid(row=0, column=0, sticky="w", padx=5, pady=5)
Label(
userFrame,
text=datetime.now().strftime("%H:%M"),
font=("Arial", 7),
bg="#d0ffff"
).grid(row=1, column=0, sticky="w")
chatWindow.insert('end', '\n ', 'tag-right')
chatWindow.window_create('end', window=userFrame)
def create_and_insert_bot_frame(bot_response):
botFrame = Frame(chatWindow, bg="#ffffd0")
Label(
botFrame,
text=bot_response,
font=("Arial", 11),
bg="#ffffd0",
wraplength=400,
justify='left'
).grid(row=0, column=0, sticky="w", padx=5, pady=5)
Label(
botFrame,
text=datetime.now().strftime("%H:%M"),
font=("Arial", 7),
bg="#ffffd0"
).grid(row=1, column=0, sticky="w")
chatWindow.insert('end', '\n ', 'tag-left')
chatWindow.window_create('end', window=botFrame)
chatWindow.insert(END, "\n\n" + "")
def send(event):
chatWindow.config(state=NORMAL)
user_input = userEntryBox.get("1.0",'end-2c')
user_input_lc = user_input.lower()
bot_response = get_bot_response(user_input_lc)
create_and_insert_user_frame(user_input)
create_and_insert_bot_frame(bot_response)
chatWindow.config(state=DISABLED)
userEntryBox.delete("1.0","end")
chatWindow.see('end')
baseWindow = Tk()
baseWindow.title("The Simple Bot")
baseWindow.geometry("500x300")
chatWindow = tks.ScrolledText(baseWindow, font="Arial")
chatWindow.tag_configure('tag-left', justify='left')
chatWindow.tag_configure('tag-right', justify='right')
chatWindow.config(state=DISABLED)
sendButton = Button(
baseWindow,
font=("Verdana", 12, 'bold'),
text="Send",
bg="#fd94b4",
activebackground="#ff467e",
fg='#ffffff',
command=send)
sendButton.bind("<Button-1>", send)
baseWindow.bind('<Return>', send)
userEntryBox = Text(baseWindow, bd=1, bg="white", width=38, font="Arial")
chatWindow.place(x=1, y=1, height=270, width=500)
userEntryBox.place(x=3, y=272, height=27)
sendButton.place(x=430, y=270)
baseWindow.mainloop()