-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_based_chatbot.py
72 lines (59 loc) · 2.68 KB
/
rule_based_chatbot.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
import random
import tkinter as tk
import nltk
from nltk.chat.util import Chat, reflections
import torch
from transformers import BertTokenizer, BertForQuestionAnswering
# load pre-trained BERT model and tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForQuestionAnswering.from_pretrained('bert-large-uncased')
class ChatBotUI:
def __init__(self, master):
self.master = master
master.title("Learnify Rule-Based ChatBot")
master.configure(bg="#FF69B4")
self.message_listbox = tk.Listbox(master, width=80, height=30)
self.message_listbox.grid(row=0, column=0, padx=10, pady=10, columnspan=2)
self.scrollbar = tk.Scrollbar(master, orient=tk.VERTICAL, command=self.message_listbox.yview)
self.scrollbar.grid(row=0, column=2, sticky='nsew')
self.message_listbox.config(yscrollcommand=self.scrollbar.set)
self.entry_field = tk.Entry(master, width=50)
self.entry_field.grid(row=1, column=0, padx=10, pady=10)
self.send_button = tk.Button(master, text="Send", command=self.send_message)
self.send_button.grid(row=1, column=1, padx=10, pady=10)
self.bot_responses = [
"How can I assist you today?",
"What can I do for you?",
"How may I help you?",
"How can I help you "
]
# Rule-based responses
self.rule_responses = {
"What course does this chatbot focus on?":
"This chatbot focuses on the Software Design course.",
"What is this course about?":
"This course focuses on design principles, and UML methodologies.",
"Why is this course important for SWE?":
"You focus on the user and business requirments needed during the intial development process."
}
def send_message(self):
# TODO: fix the intital response (outputs bot_responses)
message = self.entry_field.get()
if message:
self.message_listbox.insert(tk.END, "You: " + message)
self.entry_field.delete(0, tk.END)
# check if message matches any rule-based responses
for key in self.rule_responses:
if key.lower() in message.lower():
bot_response = self.rule_responses[key]
break
else:
bot_response = self.bot_responses[random.randint(0, len(self.bot_responses) - 1)]
self.message_listbox.insert(tk.END, "Groove: " + bot_response)
self.message_listbox.see(tk.END)
def main():
root = tk.Tk()
chatbot_ui = ChatBotUI(root)
root.mainloop()
if __name__ == "__main__":
main()