-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatbot.py
421 lines (322 loc) · 10.9 KB
/
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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os.path
import logging
import json
import pickle
import re
import random
import webbrowser
import numpy as np
import ssl
# import curses
import subprocess
import pyjokes as joke
import datetime as dt
import nltk
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from rich.console import Console
from tensorflow.keras.models import load_model
from chatbot_utils import (
get_date,
image_to_ascii_art,
play_song,
review,
read_email,
note,
news,
cpu,
internet_search,
screenshot,
write_email,
search_wikipedia,
weather_and_temperature,
relaxing_music,
server,
)
lemmatizer = WordNetLemmatizer()
intents = json.loads(open("intents.json").read())
words = pickle.load(open("models/words.pkl", "rb")) # nosec
classes = pickle.load(open("models/classes.pkl", "rb")) # nosec
types = pickle.load(open("models/types.pkl", "rb")) # nosec
model = load_model("models/chatbotmodel.h5")
ssl._create_default_https_context = (
ssl._create_unverified_context
) # potential security risk, but needed here
ARGUMENTS = ["", ""]
NO_ANSWER_RESPONSES = [
"Sorry, can't understand you",
"Please give me more info",
"Not sure I understand",
]
console = Console()
# logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
console.log(classes)
console.log(types)
console.log(words)
# TODO
"""
- Screenshot x
- Song x
- Wikipedia x
- Cpu x
- Jokes x
- Time x
- News (BBC) x
- Notes x
- Relaxing/Study Music x
- Email x
-------
Maybe
-------
+ Calculator
+ Open Website/App # need intents
https://autocomplete.clearbit.com/v1/companies/suggest?query=
returns json
+ News
Sample Code:
def timesofindia():
url = "https://timesofindia.indiatimes.com/home/headlines"
page_request = requests.get(url)
data = page_request.content
soup = BeautifulSoup(data,"html.parser")
counter = 0
for divtag in soup.find_all('div', {'class': 'headlines-list'}):
for ultag in divtag.find_all('ul', {'class': 'clearfix'}):
if (counter <= 10):
for litag in ultag.find_all('li'):
counter = counter + 1
print(str(counter) + " - https://timesofindia.indiatimes.com" + litag.find('a')['href'])
#print(str(counter) + "." + litag.text + " - https://timesofindia.indiatimes.com" + litag.find('a')['href'])
https://www.geeksforgeeks.org/fetching-top-news-using-news-api/
if __name__ == "__main__":
timesofindia()
+ Whatsapp [see <head><title> and maybe <span>containing "unread"]
"""
stop_words = list(set(stopwords.words("english")))
def remove_punctuation(sentence):
sentence = re.sub(r"[^\w\s]", "", sentence)
return sentence
def remove_stopword(sentence):
return [w for w in sentence if w not in stop_words]
def get_info(tag, request, previous=False):
if tag in ["weather", "temperature"]:
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
if tag == "temperature":
return weather_and_temperature(True)
return weather_and_temperature()
if tag == "screenshot":
file_name = screenshot()
ARGUMENTS[0] = "screenshot"
ARGUMENTS[1] = f"{os.path.abspath(file_name)}"
return
if tag == "song":
play_song(request)
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
return
if tag == "relaxing_music":
relaxing_music()
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
return
if tag == "wikipedia":
results, url, search_link = search_wikipedia(request)
ARGUMENTS[0] = "wikipedia"
ARGUMENTS[1] = search_link
return results, url
if tag == "internet_search":
return_links, search_link = internet_search(request)
ARGUMENTS[0] = "internet_search"
ARGUMENTS[1] = search_link
return return_links, search_link
if tag == "review":
rating, link, search_link = review(request)
ARGUMENTS[0] = "review"
ARGUMENTS[1] = search_link
return rating, link
if tag == "news":
analysis, url = news()
ARGUMENTS[0] = "news"
ARGUMENTS[1] = url
return analysis, url
if tag == "cpu":
usage, battery, plugged_in = cpu()
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
return usage, battery, plugged_in
if tag == "joke":
ARGUMENTS[0] = "joke"
ARGUMENTS[1] = joke.get_joke()
return joke.get_joke()
if tag == "time":
ARGUMENTS[0] = "time"
ARGUMENTS[1] = get_date()
return str(dt.datetime.now().time())[:5]
if tag == "date":
ARGUMENTS[0] = "date"
ARGUMENTS[1] = str(dt.datetime.now().time())[:5]
return get_date()
if tag == "note":
file_name = note()
ARGUMENTS[0] = "note"
ARGUMENTS[1] = f"notepad.exe {file_name}"
return file_name
if tag == "read_email":
body, sender, sender1 = read_email()
ARGUMENTS[0] = "read_email"
ARGUMENTS[1] = sender1
return body, sender
if tag == "write_email":
write_email()
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
return
if previous and ARGUMENTS[0] != "" and ARGUMENTS[1] != "":
if ARGUMENTS[0] in ["note", "screenshot"]:
subprocess.call(ARGUMENTS[1], shell=False)
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
elif ARGUMENTS[0] in ["internet_search", "review", "wikipedia", "news"]:
webbrowser.open(ARGUMENTS[1])
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
elif ARGUMENTS[0] in ["time", "date"]:
console.print(ARGUMENTS[1])
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
elif ARGUMENTS[0] == "joke":
console.print(f"{ARGUMENTS[1]}\nDo you want to hear another one?")
ARGUMENTS[0] = "joke"
ARGUMENTS[1] = joke.get_joke()
elif ARGUMENTS[0] == "read_email":
receiver = ARGUMENTS[1]
subject = input("Subject: ")
body = input("Content: ")
msg = f"Subject: {subject}\n\n{body}"
server.sendmail("JARVIS", receiver, msg)
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
def clean_up_sentence(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
return sentence_words
def bag_of_words(sentence):
sentence_words = clean_up_sentence(sentence)
bag = [0] * len(words)
for w in sentence_words:
for i, word in enumerate(words):
if word == w:
bag[i] = 1
return np.array(bag)
def predict_class(sentence):
bow = bag_of_words(sentence)
result_of_model = model.predict(np.array([bow]))[0]
error_threshold = 0.25
results = [[i, r] for i, r in enumerate(result_of_model) if r > error_threshold]
results.sort(key=lambda x: x[1], reverse=True)
sentence = sentence.replace(" ", "")
if sentence != "":
return [
{
"intent": classes[r[0]],
"probability": str(r[1]),
"type_of_intent": types[r[0]],
}
for r in results
]
return [
{
"intent": "no_answer",
"probability": "0.9629686",
"type_of_intent": "n",
}
]
def get_response(intents_list, intents_json, msg):
try:
tag = intents_list[0]["intent"]
type_of_intent = intents_list[0]["type_of_intent"]
list_of_intents = intents_json["intents"]
for i in list_of_intents:
if i["tag"] == tag:
if type_of_intent == "n":
result = random.choice(i["responses"])
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
elif tag != "continue_dialog":
info = get_info(tag, msg)
# print(info)
result = random.choice(i["responses"]).format(info)
elif ARGUMENTS[0] != "" and ARGUMENTS[1] != "":
get_info(tag, msg, previous=True)
result = ""
else:
result = "Ok..."
break
elif tag == "no_answer":
result = random.choice(NO_ANSWER_RESPONSES)
ARGUMENTS[0] = ""
ARGUMENTS[1] = ""
except IndexError:
result = ""
console.print("I don't understand!")
console.print("Do you want me search this on the internet?")
ARGUMENTS[0] = "internet_search"
ARGUMENTS[1] = message
return result
console.print("Started...")
"""
speech = "Chess.com acquired the rights and is an official broadcast partner. On our LIVE page, you'll be able to " \
"follow the live moves with computer analysis, live chat, and video commentary by grandmasters and special " \
"guests. GM Fabiano Caruana is just one of the world-class commentators who will be joining the team for
this event."
sentences = sent_tokenize(speech)
cleaned_sent = [remove_punctuation(sentence) for sentence in sentences]
speech_words = [word_tokenize(sentence) for sentence in cleaned_sent]
# print(f"Number of stopwords: {len(stop_words)}")
# print(f"First 30 stopwords:\n{stop_words[:30]}")
filtered = [remove_stopword(s) for s in speech_words]
word_count = len([w for words in speech_words for w in words])
word_count2 = len([w for words in filtered for w in words])
# print(f"Number of words before: {word_count}")
# print(f"Number of words after: {word_count2}")
# print(filtered)
POS = [nltk.pos_tag(tokenized_sent) for tokenized_sent in filtered]
# print(POS[:3])
speech_words2 = Text(word_tokenize(speech))
speech_words2.concordance("great")
"""
#
# def get_synonyms(word, pos):
# for synset in wn.synsets(word, pos=pos_to_wordnet_pos(pos)):
# for lemma in synset.lemmas():
# yield lemma.name()
#
#
#
# def pos_to_wordnet_pos(penntag, returnNone=False):
# morphy_tag = {'NN':wn.NOUN, 'JJ':wn.ADJ,
# 'VB':wn.VERB, 'RB':wn.ADV}
# try:
# return morphy_tag[penntag[:2]]
# except:
# return None if returnNone else ''
#
# text = nltk.word_tokenize("I refuse to pick up the refuse")
#
# for word, tag in nltk.pos_tag(text):
# print(f'word is {word}, POS is {tag}')
#
# # Filter for unique synonyms not equal to word and sort.
# unique = sorted(set(synonym for synonym in get_synonyms(word, tag) if synonym != word))
#
# for synonym in unique:
# print('\t', synonym)
if __name__ == "__main__":
print("\n" * 150, image_to_ascii_art("index.jpg", 0.37, 80))
console.print("\n", image_to_ascii_art("index.png", 0.37, 40))
while True:
message = input("> ")
ints = predict_class(message)
res = get_response(ints, intents, message)
console.print(res)