-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalbot.py
68 lines (54 loc) · 2.22 KB
/
finalbot.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QLineEdit, QPushButton, QVBoxLayout, QWidget
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
class ChatbotApp(QMainWindow):
def __init__(self):
super().__init__()
self.chatbot = ChatBot('Buddha')
self.trainer = ListTrainer(self.chatbot)
self.train_chatbot()
self.setWindowTitle("Chat with Buddha")
self.setGeometry(100, 100, 600, 400)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)
self.chat_display = QTextEdit()
self.chat_display.setReadOnly(True)
self.layout.addWidget(self.chat_display)
self.user_input = QLineEdit()
self.layout.addWidget(self.user_input)
self.send_button = QPushButton("Send")
self.send_button.clicked.connect(self.send_message)
self.layout.addWidget(self.send_button)
def train_chatbot(self):
FILE_PATH = "processed_chat.txt"
ADDITIONAL_FILE_PATH = "additional_chat.txt"
cleaned_lines = self.clean_file(FILE_PATH)
additional_lines = self.clean_file(ADDITIONAL_FILE_PATH)
cleaned_lines.extend(additional_lines)
self.trainer.train(cleaned_lines)
def clean_file(self, file_path):
cleaned_lines = []
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
for line in lines:
line = line.replace("'", "").replace('"', "").strip()
if line:
cleaned_lines.append(line)
except Exception as e:
print(f"An error occurred while cleaning the file: {e}")
return cleaned_lines
def send_message(self):
message = self.user_input.text()
if message.strip():
self.chat_display.append(f"You: {message}")
self.user_input.clear()
response = self.chatbot.get_response(message)
self.chat_display.append(f"Buddha: {response}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ChatbotApp()
window.show()
sys.exit(app.exec_())