-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
215 lines (198 loc) · 7.15 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import tkinter as tk
from tkinter import *
import os
from tkinter import filedialog
from newspaper import Article
import random
#import string
FORMAT = "utf-8"
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import warnings
warnings.filterwarnings('ignore')
text_contents=dict()
nltk.download('punkt',quiet=True)
arc=Article('https://developer.ibm.com/languages/python/articles/introduction-to-machine-learning/')
arc.download()
arc.parse()
arc.nlp()
corpus=arc.text
#tokenisation
text=corpus
sentencelist=nltk.sent_tokenize(text)#list of sentences
def greet_res(text):
text=text.lower()
bot_greet=['hi','hello','hola','hey','howdy']
usr_greet=['hi','hey','hello','hola','greetings','wassup','whats up']
for word in text.split():
if word in usr_greet:
return random.choice(bot_greet)
def index_sort(list_var):
length=len(list_var)
list_index=list(range(0,length))
x=list_var
for i in range(length):
for j in range(length):
if x[list_index[i]] > x[list_index[j]]:
temp=list_index[i]
list_index[i]=list_index[j]
list_index[j]=temp
return list_index
# bot response
def bot_ress(usr_input):
usr_input = usr_input.lower()
sentencelist.append(usr_input)
bot_res = ''
cm = CountVectorizer().fit_transform(sentencelist)
similarity_scores = cosine_similarity(cm[-1], cm)
similarity_scores_list = similarity_scores.flatten()
index = index_sort(similarity_scores_list)
index = index[1:]
response_flag = 0
j = 0
for i in range(len(index)):
if similarity_scores_list[index[i]] > 0.0:
bot_res = bot_res + ' ' + sentencelist[index[i]]
response_flag = 1
j = j + 1
if j > 2:
break
if response_flag == 0:
bot_res = bot_res + ' ' + 'I am sorry, I don\'t understand.'
sentencelist.remove(usr_input)
return bot_res
def widget_get():
text_widget = root.nametowidget(textcon)
return text_widget.get('1.0','end-1c')
def saveas(event=None):
global file_path,filename
file_path= filedialog.asksaveasfilename( defaultextension=".txt")
try:
filename=os.path.basename(file_path)
root.title(f"Chat Bot - {filename}")
content=widget_get()
with open(file_path ,"w") as file:
file.write(content)
text_contents[str(textcon)]=hash(content)
print("Operation successfull")
except(FileNotFoundError):
print("Operation not successfull")
return None
file_path =None
def save(event=None):
global file_path,filename
try:
if(file_path is None):
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
filename=os.path.basename(file_path)
root.title(f"Chat Bot - {filename}")
content=widget_get()
with open(file_path ,"w") as file:
file.write(content)
text_contents[str(textcon)] = hash(content)
print("Operation successfull")
except(FileNotFoundError):
print("Operation not successfull")
return None
def new(event=None):
textcon.delete('2.0', 'end-1c')
global file_path,filename
file_path = None
content = widget_get()
text_contents[str(textcon)] = hash(content)
filename=None
"""def check_changes(event=None):
global filename
current=widget_get()
content=current.get()
name =filename
if hash(current)!=text_contents[str(textcon)]:
if name[-1]!="*":
filename=name+"*"
elif name[-1]=="*":
filename=name
root.title(f"Chat Bot - {filename}")
"""
def clear(event=None):
textcon.delete('2.0', 'end-1c')
content = widget_get()
text_contents[str(textcon)] = hash(content)
def fopen(event=None):
global file_path,filename
file_path = filedialog.askopenfilename(defaultextension=".txt")
try:
filename = os.path.basename(file_path)
root.title(f"Chat Bot - {filename}")
text_widget = root.nametowidget(textcon)
with open(file_path, "r") as file:
content=file.read()
textcon.delete('1.0', 'end-1c')
text_contents[str(textcon)] = hash(content)
text_widget.insert(END,content)
print("Operation successfull")
except(FileNotFoundError):
print("Operation not successfull")
return None
exit_list = ['exit','break','quit','see you later','chat with you later','end the chat','bye','ok bye']
def send(event=None):
usr_input = message.get()
usr_input = usr_input.lower()
textcon.insert(END, f'User: {usr_input}'+'\n','usr')
if usr_input in exit_list:
textcon.config(fg='yellow')
textcon.insert(END,"Bot:Ok bye! Chat with you later\n")
return root.destroy()
else:
textcon.config(fg='yellow')
if greet_res(usr_input) != None:
lab=f"Bot: {greet_res(usr_input)}"+'\n'
textcon.insert(END,lab)
else:
lab = f"Bot: {bot_ress(usr_input)}"+'\n'
textcon.insert(END, lab)
root=tk.Tk()
filename="Untitled.txt"
root.title(f"Chat Bot - Untitled.txt")
root.geometry('500x400')
root.resizable(False, False)
main_menu=Menu(root)
file_menu=Menu(root)
file_menu.add_command(label='Open <Ctrl+O>',command=fopen)
file_menu.add_command(label='New <Ctrl+N>',command=new)
file_menu.add_command(label='Save <Ctrl+S>',command=save)
file_menu.add_command(label='Save as <Ctrl+Shift+S>',command=saveas)
edit_menu=Menu(root)
edit_menu.add_command(label='Clear <Delete>',command=clear)
edit_menu.add_command(label='Preferences')
main_menu.add_cascade(label="File",menu=file_menu)
main_menu.add_cascade(label="Edit",menu=edit_menu)
main_menu.add_command(label="Quit",command=root.destroy)
root.config(menu=main_menu)
message=tk.StringVar()
chat_win=Frame(root,bd=1,bg='black',width=50,height=8)
chat_win.place(x=6,y=6,height=300,width=480)
textcon=tk.Text(chat_win,bd=1,bg='black',width=50,height=8)
textcon.pack(fill="both",expand=True)
mes_win=Entry(root,width=30,xscrollcommand=True,textvariable=message)
mes_win.place(x=6,y=310,height=60,width=366)
mes_win.focus()
textcon.config(fg='yellow')
textcon.tag_config('usr',foreground='white')
textcon.insert(END,"Bot: This is your chat bot to assist you about Machine Learning!\n\n")
mssg=mes_win.get()
button=Button(root,text='Send',bg='yellow',activebackground='orange',command=send,width=12,height=5,font=('Arial'))
button.place(x=376,y=310,height=60,width=110)
scrollbar=tk.Scrollbar(textcon)
scrollbar.pack(fill='y')
scrollbar.place(relheight = 1,relx = 1)
scrollbar.config(command = textcon.yview)
content = widget_get()
text_contents[str(textcon)] = hash(content)
root.bind('<Control-s>',save,file_menu)
root.bind('<Control-Shift-s>',saveas,file_menu)
root.bind('<Return>', send,button)
root.bind('<Control-n>', new,file_menu)
root.bind('<Delete>', clear,edit_menu)
root.bind('<Control-o>', fopen,file_menu)
root.mainloop()