-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDaten_Project_program.py
389 lines (285 loc) · 13 KB
/
Daten_Project_program.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
'''
Chatbot that detects user tone and recommends corresponding activities. Uses IBM Watson's Conversation
and Tone Analyzer.
Authors: Prajakta Gaydhani (pag3862)
References : Conversation and Tone Analyser Integration Example by Watson Developer Cloud.
URL : https://github.com/watson-developer-cloud/python-sdk/tree/master/examples/conversation_tone_analyzer_integration
'''
from watson_developer_cloud import ConversationV1
from watson_developer_cloud import ToneAnalyzerV3
import csv
import random
from tkinter import *
from PIL import Image, ImageTk
import tkinter.scrolledtext as tkst
def getNBooks(books,detected_tone):
print(detected_tone)
genresToAvoid = []
genresToTake = []
if detected_tone == "anger" or detected_tone == "disgust":
genresToAvoid = ["Romance", "Horror", "Dystopia"]
genresToTake = ["Comedy", "Self-help", "Fantasy"]
elif detected_tone == "sadness":
genresToAvoid = ["Dystopia", "Horror", "Science"]
genresToTake = ["Comedy", "Drama", "Fantasy"]
elif detected_tone == "fear":
genresToAvoid = ["Horror", "War", "Thriller"]
genresToTake = ["Comedy", "Drama", "Children"]
else:
genresToAvoid = ["Horror", "War", "Thriller"]
genresToTake = ["Comedy", "Drama", "Children"]
print(genresToAvoid, genresToTake)
temp = random.randint(0, len(books)//2)
size = 2
temp_list = []
for i in range(temp,len(books)):
if (books[i][2].find(genresToTake[0]) != -1 or books[i][2].find(genresToTake[1]) != -1 or books[i][2].find(genresToTake[1]) != -1)\
and (books[i][2].find(genresToAvoid[0]) == -1) and (books[i][2].find(genresToAvoid[1]) == -1) and (books[i][2].find(genresToAvoid[2]) == -1) :
if len(temp_list) >= size:
return temp_list
else:
str = books[i][0] + " by " +books[i][1]
temp_list.append(str)
def getNMovies(movies,detected_tone):
genresToAvoid = []
genresToTake = []
if detected_tone == "anger" or detected_tone == "disgust":
genresToAvoid = ["Romance", "Horror", "War"]
genresToTake = ["Comedy", "Adventure", "Animation"]
elif detected_tone == "sadness" :
genresToAvoid = ["Romance", "Horror", "War"]
genresToTake = ["Comedy", "Drama", "Fantasy"]
elif detected_tone == "fear" :
genresToAvoid = [ "Horror", "War", "Thriller"]
genresToTake = ["Comedy", "Drama", "Children"]
else:
genresToAvoid = [ "Horror", "War", "Thriller"]
genresToTake = ["Comedy", "Drama", "Children"]
temp = random.randint(0, len(movies) // 2)
size = 2
temp_list = []
for i in range(temp, len(movies)):
if (movies[i][1].find(genresToTake[0]) != -1 or movies[i][1].find(genresToTake[1]) != -1 or movies[i][1].find(genresToTake[2]) != -1)\
and (movies[i][1].find(genresToAvoid[0]) == -1) and (movies[i][1].find(genresToAvoid[1]) == -1) and (movies[i][1].find(genresToAvoid[2]) == -1):
if len(temp_list) >= size:
return temp_list
else:
temp_list.append(movies[i][0])
def getPMovies(movies):
temp = random.randint(0, len(movies) // 2)
size = 2
temp_list = []
for i in range(temp, len(movies)):
if (movies[i][1].find("Comedy") != -1 or movies[i][1].find("Adventure") != -1 or movies[i][1].find("Fantasy") != -1 or movies[i][1].find("Romance") or movies[i][1].find("Drama") != -1) \
and (movies[i][1].find("War") == -1) and (movies[i][1].find("Horror") == -1) and (movies[i][1].find("sex") == -1) \
and (movies[i][0].find("sex") == -1):
if len(temp_list) >= size:
return temp_list
else:
temp_list.append(movies[i][0])
def getPBooks(books):
temp = random.randint(0, len(books)//2)
size = 2
temp_list = []
for i in range(temp,len(books)):
if (books[i][2].find("Comedy") != -1 or books[i][2].find("Romance") != -1 or books[i][2].find("Fantasy") != -1 or books[i][2].find("Thriller") != -1)\
and (books[i][2].find("Dystopia") == -1) and (books[i][2].find("Horror") == -1) \
and (books[i][2].find("sex") == -1) and (books[i][1].find("sex") == -1) :
if len(temp_list) >= size:
return temp_list
else:
str = books[i][0] + " by " +books[i][1]
temp_list.append(str)
def getJoke(jokes):
temp = random.randint(0,len(jokes)-1)
return jokes[temp]
def getShoppingWebsite():
shopping = ["https://www.amazon.com/gp/movers-and-shakers", "https://www.urbanoutfitters.com/", "https://www.macys.com/", "http://www.asos.com/",
"http://www.hm.com/us/","https://www.zara.com/us/","https://www.bloomingdales.com/"]
temp = random.randint(0,len(shopping)-1)
return shopping[temp]
def initUser():
return {
'user' : {
'tone' : {
'emotion' : {
'current' : None
}
}
}
}
def updateEmotionTone(user, emotionTone):
maxScore = 0
primaryEmotion = None
primaryEmotionScore = None
for tone in emotionTone['tones']:
if tone['score'] > maxScore:
maxScore = tone['score']
primaryEmotion = tone['tone_name'].lower()
primaryEmotionScore = tone['score']
if maxScore <= THRESHOLD:
primaryEmotion = 'neutral'
primaryEmotionScore = None
user['tone']['emotion']['current'] = primaryEmotion
def updateUserTone(conversationPayload, toneAnalyserPayload):
emotionTone = None
if 'context' not in conversationPayload:
conversationPayload['context'] = {}
if 'user' not in conversationPayload:
conversationPayload['context'] = initUser()
user = conversationPayload['context']['user']
if toneAnalyserPayload and toneAnalyserPayload['document_tone']:
for toneCategory in toneAnalyserPayload['document_tone']['tone_categories']:
if toneCategory['category_id'] == EMOTION_TONE_LABEL:
emotionTone = toneCategory
updateEmotionTone(user,emotionTone)
conversationPayload['context']['user'] = user
return conversationPayload
def startRecommendations(payload, root) :
tone = tone_analyser.tone(text = payload['input']['text'])
conversation_payload = updateUserTone(payload, tone)
response = conversation.message(workspace_id=workspace_id, message_input = {'text':payload['input']['text']})
tone_response = conversation.message(workspace_id=workspace_id, message_input=conversation_payload['input'],context=conversation_payload['context'])
if tone_response['intents'][0]['intent'] == "Goodbye" or tone_response['intents'][0]['intent'] == "thank":
stuff = response['output']['text'][0]
tt = tkst.ScrolledText(root, height=4, width=65, bg = 'LightSkyBlue1')
tt.insert(INSERT, stuff)
root.create_window(250, y1 + 50, window=tt)
print(response['output']['text'])
return False
elif tone_response['intents'][0]['intent'] == 'Negative_Consolation':
global emotion
emotion = conversation_payload['context']['user']['tone']['emotion']['current']
joke = getJoke(jokes)
print(response['output']['text'], joke)
stuff = response['output']['text'][0] + "\n" + joke
tt = tkst.ScrolledText(root, height = 4, width = 65, bg = 'LightSkyBlue1')
tt.insert(INSERT, stuff)
root.create_window(250, y1+ 50, window=tt)
txt = "Your reaction to the joke?"
elif tone_response['intents'][0]['intent'] == 'Positive_Consolation':
joke = getJoke(jokes)
print(response['output']['text'], joke)
stuff = response['output']['text'][0] + "\n" + joke
tt = tkst.ScrolledText(root, height=4, width=65, bg = 'LightSkyBlue1')
tt.insert(INSERT, stuff)
root.create_window(250, y1 + 50, window=tt)
txt = "Your reaction to the joke?"
else:
stuff = response['output']['text'][0]
print(response['output']['text'])
tt = tkst.ScrolledText(root, height=4, width=65, bg = 'LightSkyBlue1')
tt.insert(INSERT,stuff)
root.create_window(250, y1 + 50, window=tt)
if tone_response['intents'][0]['intent'] == 'Laugh':
txt = "Respond with Yes or No."
elif tone_response['intents'][0]['intent'] == 'Greeting':
txt = "Tell us about your day!"
elif tone_response['intents'][0]['intent'] == 'moreRecco':
txt = "Type More for more options or Thanks/Bye to end."
else:
txt = ""
if (tone_response['intents'][0]['intent'] == 'Yes' or tone_response['intents'][0]['intent'] == 'moreRecco') and emotion != "p":
book_list = getNBooks(books,emotion)
movie_list = getNMovies(movies,emotion)
print("Check out these books ",book_list)
print("Some good movies ",movie_list)
stuff = "Check out these books " + book_list[0] + " , " + book_list[1] + "\n" + "Some good movies " + movie_list[0]+ " , " + movie_list[1]
tt = tkst.ScrolledText(root, height=4, width=65, bg = 'LightSkyBlue1')
tt.insert(INSERT, stuff)
root.create_window(250, y1 + 50, window=tt)
txt = "Type More for more options or Thanks/Bye to end."
if (tone_response['intents'][0]['intent'] == 'Yes' or tone_response['intents'][0]['intent'] == 'moreRecco') and emotion == "p":
book_list = getPBooks(books)
movie_list = getPMovies(movies)
print("Check out these books ", book_list)
print("Some good movies ", movie_list)
print("Or we can go shopping!!!! Check out this cool website!", getShoppingWebsite())
stuff = "Check out these books " + book_list[0] + " , " + book_list[1] + "\n" + "Some good movies " + movie_list[0]+ " , " + movie_list[1] + "\n" +\
"OR we can go shopping!!!! Check out this cool website!" + getShoppingWebsite()
tt = tkst.ScrolledText(root, height=5, width=65, bg = 'LightSkyBlue1')
tt.insert(INSERT, stuff)
root.create_window(250, y1 + 50, window=tt)
txt = "Type More for more options or Thanks/Bye to end."
global text1
text1 = txt
def setInputText(text,root):
print(text)
payload['input']['text'] = text
flag = startRecommendations(payload, root)
if flag == False:
return
else:
global y_value
y_value = y_value + 110
global y1
y1 = y1 + 110
EntryGUI(root)
def EntryGUI(root):
image = Image.open("mark.jpeg")
img = image.resize((20, 20), Image.ANTIALIAS)
img.save('resized_image.jpg')
Photo = ImageTk.PhotoImage(img)
l1 = Label(root, image = Photo)
l1.image = Photo
root.create_window(775, y_value, window=l1)
l2 = Label(root,text="", width = 50)
l1.bind("<Enter>", lambda e: l2.configure(text=text1))
l1.bind("<Leave>", lambda e: l2.configure(text=" "))
root.create_window(700, y_value + 20, window = l2)
e1 = Entry(root, bg = 'LightSkyBlue1')
root.create_window(650, y_value, window=e1)
e1.bind('<Return>',lambda e : setInputText(e1.get(),root))
jokes = []
with open('funjokes.csv') as fileptr:
next(fileptr, None)
readCSV = csv.reader(fileptr)
for row in readCSV:
jokes.append(row[1])
books = []
with open('cleanedBooksData.csv') as fileptr:
next(fileptr, None)
readCSV = csv.reader(fileptr)
for row in readCSV:
temp = []
temp.append(row[1])
temp.append(row[2])
temp.append(row[3])
books.append(temp)
movies = []
with open('movies.csv') as fileptr:
next(fileptr, None)
readCSV = csv.reader(fileptr)
for row in readCSV:
temp = []
temp.append(row[1])
temp.append(row[2])
movies.append(temp)
emotion = "p"
y_value = 75
y1 = 75
text1 = "Start conversation with greeting!"
EMOTION_TONE_LABEL = 'emotion_tone'
THRESHOLD = 0.5
conversation = ConversationV1(username="6e7f1115-0c0a-4de1-958f-0b2775d67a20", password="vDvC7T7VxjHo", version="2017-04-25")
tone_analyser = ToneAnalyzerV3(username="5f400c71-48f3-47b8-840c-53d7e1611b77", password="3djj2OCvWd4n",
url="https://gateway.watsonplatform.net/tone-analyzer/api", version="2017-04-25")
workspace_id = "b3f9b097-f794-4dd9-8575-8fdb7180e1db"
payload = {'workspace_id' : workspace_id,
'input' : {
'text' : ""
}}
root = Tk()
root.title("Mood-Based Activity Recommendation System")
frame=Frame(root,width=900,height=1000,bg='white')
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='white',width=900,height=1000,scrollregion=(0,0,900,1500))
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config( yscrollcommand=vbar.set)
label = Label(text = 'Mood-Based Activity Recommendation System', justify = CENTER,font=("Comic Sans", 26),fg='OrangeRed2')
canvas.create_window(400, 20, window = label)
EntryGUI(canvas)
canvas.pack()
root.mainloop()