-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_project.py
More file actions
465 lines (365 loc) · 17.2 KB
/
final_project.py
File metadata and controls
465 lines (365 loc) · 17.2 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# This program creates a list of suggested movies for the user based on the
# information inputted by the user about preferred original language,
# preferred dubbed language, and genre of movie
# by Kristin Albright, Mika Cooney, and Jack Daley
import csv
from tkinter import *
class Survey:
"""
This class creates an object Survey that opens a tkinter
window and asks a series of preference questions.
The instance variables include language, dubLanguage, and
prefKeyList.
Language is a string that is initiated as empty, but changed
by the button the user clicks (changed to either 'fr' to
represent French, 'en' to represent English, or 'es' to represent
Spanish).
DubLanguage is a string that is initiated as empty, but
is changed by the button the user clicks (changed
to either 'fr' to represent French, 'en' to represent
English, or 'es' to represent Spanish).
PrefKeyList is a list that is initialized as empty, but
appended to add the genre types that the user selects
("Adventure", "Drama", "Family", "Animation", "Comedy",
"Fantasy", "Romance", "Horror", "Science Fiction").
"""
def __init__(self):
self.language = ""
self.dubLanguage = ""
self.prefKeyList = []
prefLang = self.language
prefDubLang = self.dubLanguage
prefKey = self.prefKeyList
def choose1Lang(self, language):
"""
choose1Lang takes chosenLang1, a string, as a
parameter which is given by the original language
buttons.
Returns: self.language (which updates the language to
the one selected by the user in the function drawQ1)
Preconditions: Must be passed a chosenLang1 from the
user pressing a button.
Postconditions: self.language is updated to the language
selected by the user.
"""
if language == "French":
self.language = "fr"
return self.language
elif language == "English":
self.language = "en"
return self.language
elif language == "Spanish":
self.language = "es"
return self.language
def choose2Lang(self, dubLanguage):
"""
choose2Lang takes chosenLang2, a string, as a
parameter which is given by the dubbed language
buttons.
Returns: self.dubLanguage (which updates the dubLanguage to
the one selected by the user in the function drawQ2andQ3).
Preconditions: Must be passed a chosenLang2 from the
user pressing a button.
Postconditions: self.dubLanguage is updated to the language
selected by the user.
"""
if dubLanguage == "French":
self.dubLanguage = "fr"
return self.dubLanguage
elif dubLanguage == "English":
self.dubLanguage = "en"
return self.dubLanguage
elif dubLanguage == "Spanish":
self.dubLanguage = "es"
return self.dubLanguage
def getPrefLang(self):
"""
Returns: the preferred original language of the user as a string.
"""
return self.language
def getPrefDubLang(self):
"""
Returns: the dubbed language of the user as a string.
"""
return self.dubLanguage
def getPrefKey(self):
"""
Returns: the preferred movie keywords of the user as a list of strings.
"""
return self.prefKeyList
def recordKey(varList, i):
"""
Updates the status of each of the genre options to either be
stored as on or off, based on number of clicks by the user.
Preconditions: varList is passed in correctly as a list.
"""
varList[i] = 1 - varList[i]
def submit(window, self, Survey, Movie, root, movie, varList):
"""
Calls compareMovie, defines movie_list as the return of
compareMovie, calls the function drawAllMovies and edits
preKeyList
Preconditions: The parameters are passed in correctly
"""
global movie_list
counter = 0
#Initializing a list of possible keywords
keyList = ["Adventure", "Drama", "Family", "Animation", "Comedy", "Fantasy", "Romance", "Horror", "Science Fiction"]
for i in range(len(varList)):
if varList[i] == 1:
self.prefKeyList.append(keyList[i])
prefLang = self.getPrefLang()
prefDubLang = self.getPrefDubLang()
prefKeyList = self.getPrefKey()
movie_list = compareMovie(prefKeyList, prefDubLang, prefLang, Movie, Survey)
movie.movieList = movie_list
top = ""
backSelect = 0
drawAllMovies(root, movie_list, counter, top, movie, backSelect)
def drawQ1andQ2andQ3(self, window, root, Survey, Movie, movie):
"""
Creates the survey questions and buttons that allow the user to specify
their preferred original language, dubbed language, and genre(s).
Preconditions: the parameters are passed in correctly.
"""
window.pack()
Welcometxt = window.create_text(60,25,fill="black",text="Pick an Original" + "\n" + "Language:")
button1 = Button(root,text = "French",command=lambda: Survey.choose1Lang(self, "French"))
button1.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button1_window = window.create_window(20, 50, anchor=NW, window=button1)
button1.update()
button1 = Button(root,text = "French",command=lambda: Survey.choose1Lang(self, "French"))
button1.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button1_window = window.create_window(20, 50, anchor=NW, window=button1)
button1.update()
button2 = Button(root,text = "English",command=lambda: Survey.choose1Lang(self, "English"))
button2.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button2_window = window.create_window(20, 100, anchor=NW, window=button2)
button2.update()
button3 = Button(root,text = "Spanish",command=lambda: Survey.choose1Lang(self, "Spanish"))
button3.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button3_window = window.create_window(20, 150, anchor=NW, window=button3)
button3.update()
Welcometxt = window.create_text(200,25,fill="black",text="Pick a Dubbed" + "\n" + "Language:")
button1 = Button(root,text = "French",command=lambda: Survey.choose2Lang(self,"French"))
button1.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button1_window = window.create_window(162, 50, anchor=NW, window=button1)
button1.update()
button2 = Button(root,text = "English",command=lambda: Survey.choose2Lang(self,"English"))
button2.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button2_window = window.create_window(162, 100, anchor=NW, window=button2)
button2.update()
button3 = Button(root,text = "Spanish",command=lambda: Survey.choose2Lang(self,"Spanish"))
button3.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button3_window = window.create_window(162, 150, anchor=NW, window=button3)
button3.update()
button4 = Button(root,text = "Submit",command=lambda: Survey.submit(window, self, Survey, Movie, root, movie, varList))
button4.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button4_window = window.create_window(91, 175, anchor=NW, window=button4)
button4.update()
#Initializing a variable list to track the state of check-boxes (0 = F ; 1 = T)
varList = [0,0,0,0,0,0,0,0,0]
#Intializing checkboxes
Label(root, text="What Kind of Movie?").pack()
Checkbutton(root, text="Adventure", variable=IntVar(), command=lambda: Survey.recordKey(varList, 0)).pack()
Checkbutton(root, text="Drama", variable=IntVar(), command=lambda: Survey.recordKey(varList, 1)).pack()
Checkbutton(root, text="Family", variable=IntVar(), command=lambda: Survey.recordKey(varList, 2)).pack()
Checkbutton(root, text="Animation", variable=IntVar(), command=lambda: Survey.recordKey(varList, 3)).pack()
Checkbutton(root, text="Comedy", variable=IntVar(), command=lambda: Survey.recordKey(varList, 4)).pack()
Checkbutton(root, text="Fantasy", variable=IntVar(), command=lambda: Survey.recordKey(varList, 5)).pack()
Checkbutton(root, text="Romance", variable=IntVar(), command=lambda: Survey.recordKey(varList, 6)).pack()
Checkbutton(root, text="Horror", variable=IntVar(), command=lambda: Survey.recordKey(varList, 7)).pack()
Checkbutton(root, text="Science Fiction", variable=IntVar(), command=lambda: Survey.recordKey(varList, 8)).pack()
root.mainloop()
class Movie:
""" This class creates an object Movie that reads and returns all
of the necessary data about the movie for this program.
The instance variables include ogLanguage, ogDubLang, ogKeyWords,
and movieList.
ogLanguage is initiated as an empty string but is later edited to equal
the specific movie's original language.
ogDubLang is initialized as an empty string but is later edited to
equal the dubbed language(s) of the specific movie.
ogKeyWords is initialized as an empty list but is later appended to
include all genres that the user selects for their movie.
movieList is initialized as an empty list, but is later appended to
include all of the movies that fit the user's specifications.
"""
def __init__(self):
self.ogLanguage = ""
self.ogDubLang = ""
self.ogKeyWords = []
self.movieList = []
ogLang = self.ogLanguage
ogDubLang = self.ogDubLang
ogKeyWords = self.ogKeyWords
def getOgLang(self):
"""
Returns: the original language of the movie as a string.
"""
return self.ogLanguage
def getOgDubLang(self):
"""
Returns: the dubbed languages of the movie as a list of strings.
"""
return self.ogDubLang
def getOgKeyWords(self):
"""
Returns: the keywords of the movie as a list of strings.
"""
return self.ogKeyWords
def getMovieList(self):
"""
Returns: the list of matched movies.
"""
return self.movieList
def findOgLang():
"""
Opens up the movies_metadata csv file and isolates the orinial languge
of the movie
Returns: original_language, a list that holds all of the original
languages of the movie list
"""
with open('movies_metadata.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile)
original_language = []
next(spamreader)
for row in spamreader:
path = row[7]
original_language.append(path)
return original_language
def findOgKey():
"""
Opens up the movies_metadata csv file and isolates the genres that
the movie falls under.
Returns: a list original_keys that contains a list of genres for
each of the movies in the dataset
"""
with open('movies_metadata.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile)
original_keys = []
next(spamreader)
for row in spamreader:
if len(row) > 3:
if len(row[3]) > 0:
path = eval(row[3])
small_list = []
for item in path:
key = item['name']
small_list.append(key)
original_keys.append(small_list)
return original_keys
def findDubLang():
"""
Opens up the movies_metadata csv file and isolates the spoken langauges
that each movie has (dubbed versions of the movie)
Returns: a list dubbed_language that contains a list of the dubbed languages
for each movie in the dataset
"""
with open('movies_metadata.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile)
dubbed_language = []
next(spamreader)
for row in spamreader:
if len(row) > 17:
if len(row[17]) > 0:
path = eval(row[17])
small_list = []
for item in path:
key = item['iso_639_1']
small_list.append(key)
dubbed_language.append(small_list)
else:
dubbed_language.append([])
return dubbed_language
def compareMovie(prefKeyList, prefDubLang, prefLang, Movie, Survey):
"""
Compares the results of the survey to the entire list of movies from the
movies_metadata csv file.
Returns: rec_movie, a list of all movies that fit all survery conditions
Preconditions: All parameters must be passed in correctly
"""
original_language = Movie.findOgLang()
dubbed_language = Movie.findDubLang()
original_keys = Movie.findOgKey()
rec_movie = []
i = 0
with open('movies_metadata.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile)
next(spamreader)
while i < len(original_language):
for row in spamreader:
if original_language[i] == prefLang and any(dubbed_language[i][n] == prefDubLang for n in range(len(dubbed_language[i]))):
for j in original_keys[i]:
if j in prefKeyList:
rec_movie.append(row[8])
i = i + 1
return rec_movie
def drawAllMovies(root, movie_list, counter, top, movie, backSelect):
"""
Creates a window of the first five movies once the survey has been
submitted. Includes a next, back and quit button that allow the
user to navigate between the list of movies.
Preconditions: All parameters are submitted correctly
"""
if counter > 0 or backSelect == 1:
top.destroy()
top = Toplevel()
top.title("Here Are Your Movies:")
else:
top = Toplevel()
top.title("Here Are Your Movies:")
backSelect = 0
if len(movie_list) == 0:
var = StringVar()
label = Message(top, textvariable=var, relief=FLAT )
var.set("End of Movie List")
label.pack()
else:
button1 = Button(top, text=movie_list[0], command=lambda: drawSum(movie_list[0]))
button1.pack()
button2 = Button(top, text=movie_list[1], command=lambda: drawSum(movie_list[1]))
button2.pack()
button3 = Button(top, text=movie_list[2], command=lambda: drawSum(movie_list[2]))
button3.pack()
button4 = Button(top, text=movie_list[3], command=lambda: drawSum(movie_list[3]))
button4.pack()
button5 = Button(top, text=movie_list[4], command=lambda: drawSum(movie_list[4]))
button5.pack()
buttonNext = Button(top, text="Next", command=lambda: drawAllMovies(root, movie_list[5:], counter+1, top, movie, backSelect))
buttonNext.pack()
buttonBack = Button(top, text="Back", command=lambda: drawAllMovies(root, movie.getMovieList()[(5*counter-5):], counter-1, top, movie, backSelect+1))
buttonBack.pack()
buttonQuit = Button(top, text="Quit", command=lambda: exit())
buttonQuit.pack()
mainloop()
def drawSum(movie_name):
"""
A helper function that is used in drawAllMovies to open the summary once the user
clicks on a button with the title of the movie
Preconditions: movie_name is passed correctly in
"""
sumMov = Toplevel()
sumMov.title("Summary")
with open('movies_metadata.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile)
original_language = []
next(spamreader)
for row in spamreader:
if row[8] == movie_name:
path = row[9]
var = StringVar()
label = Message(sumMov, textvariable=var, relief=FLAT )
var.set(path)
label.pack()
mainloop
def main():
#Initializing a window, specifically a tkinter window that allows for buttons
root=Tk()
window = Canvas(root)
root.title('Dubbed Movie Finder')
#Initializing an instance of our two primary classes: Survey and Movie.
userPrefs = Survey()
sugMovie = Movie()
Survey.drawQ1andQ2andQ3(userPrefs, window, root, Survey, Movie, sugMovie)
main()