forked from bzitkovic/intelligent-system-goodReads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
406 lines (345 loc) · 13.8 KB
/
gui.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
from tkinter import (
Entry,
Label,
Button,
Checkbutton,
IntVar,
StringVar,
Frame,
Tk,
Toplevel,
font,
)
from tkinter.constants import CENTER
from typing import List
from models import Book
from visualization import (
get_box_plot,
get_heat_map,
get_histogram_genres,
get_tabloo_table,
get_decision_tree,
FEATURE_COLUMNS,
)
from pandas import DataFrame
from decision_tree_model import make_decision_tree, make_prediction_total_rating
from sklearn.tree import DecisionTreeClassifier
from numpy import round
WINDOW_TITLE = "Good Reads"
WINDOW_X_OFFSET = int(1080 / 3 - 100)
WINDOW_Y_OFFSET = int(1920 / 2 - 400)
WINDOW_IS_RESIZABLE = False
FONT_STYLE = "Ariel"
FONT_SIZE = 11
COLOR_PURPLE = "#7878ab"
COLOR_GREEN = "#78ab7c"
class GUI:
def __init__(self, window: Tk, books: List[Book], dataframe: DataFrame, decision_tree: DecisionTreeClassifier):
self.window = window
self.books = books
self.dataframe = dataframe
self.decision_tree = decision_tree
self.configure_window()
self.create_variables()
self.create_UI()
def configure_window(self):
self.window.title(WINDOW_TITLE)
self.window.geometry(f"800x420+{WINDOW_Y_OFFSET}+{WINDOW_X_OFFSET}")
self.window.configure(bg=COLOR_PURPLE)
self.window.resizable(WINDOW_IS_RESIZABLE, WINDOW_IS_RESIZABLE)
default_font = font.nametofont("TkDefaultFont")
default_font.configure(family=FONT_STYLE, size=FONT_SIZE)
def create_variables(self):
self.chk1_variable = IntVar()
self.chk2_variable = IntVar()
self.chk3_variable = IntVar()
self.chk4_variable = IntVar()
self.chk5_variable = IntVar()
self.chk6_variable = IntVar()
self.ent_number_of_pages_variable = StringVar()
self.ent_minimum_rating_variable = StringVar()
self.ent_minimum_reviews_variable = StringVar()
self.chk_options = [
self.chk1_variable,
self.chk2_variable,
self.chk3_variable,
self.chk4_variable,
self.chk5_variable,
self.chk6_variable,
]
self.ent_values = [
self.ent_number_of_pages_variable,
self.ent_minimum_rating_variable,
self.ent_minimum_reviews_variable,
]
def create_UI(self):
self.user_entry_frame = Frame(self.window, bg=COLOR_PURPLE)
self.user_entry_frame.grid(row=0, column=1, pady=10, sticky="NEWS")
self.visualization_frame = Frame(self.window, bg=COLOR_PURPLE)
self.visualization_frame.grid(row=0, rowspan=2, column=0, pady=10, sticky="NEWS")
self.checkbox_frame = Frame(self.window, bg=COLOR_PURPLE)
self.checkbox_frame.grid(row=1, column=1, sticky="NEWS")
self.recommend_frame = Frame(self.window, bg=COLOR_PURPLE)
self.recommend_frame.grid(row=2, column=0, columnspan=2, pady=35, sticky="NEWS")
self.create_visualization()
self.create_user_entry()
self.create_checkboxes()
self.create_recommend_button()
def create_user_entry(self):
self.lbl_number = Label(self.user_entry_frame, bg=COLOR_PURPLE, text="Minimun number of pages:")
self.lbl_number.grid(row=0, column=0, padx=20, sticky="W")
self.ent_number_of_pages = Entry(
self.user_entry_frame,
width=30,
textvariable=self.ent_number_of_pages_variable,
)
self.ent_number_of_pages.grid(row=1, column=0, padx=25, pady=10, sticky="W")
self.lbl_rating = Label(self.user_entry_frame, bg=COLOR_PURPLE, text="Minimum rating:")
self.lbl_rating.grid(row=2, column=0, padx=20, sticky="W")
self.ent_rating = Entry(
self.user_entry_frame,
width=30,
textvariable=self.ent_minimum_rating_variable,
)
self.ent_rating.grid(row=3, column=0, padx=25, pady=10, sticky="W")
self.lbl_reviews = Label(
self.user_entry_frame,
bg=COLOR_PURPLE,
text="Minimum number od reviews:",
)
self.lbl_reviews.grid(row=0, column=1, padx=20, sticky="W")
self.ent_reviews = Entry(
self.user_entry_frame,
width=30,
textvariable=self.ent_minimum_reviews_variable,
)
self.ent_reviews.grid(row=1, column=1, padx=25, pady=10, sticky="W")
def create_checkboxes(self):
self.lbl_genre = Label(self.checkbox_frame, bg=COLOR_PURPLE, text="Select prefered genre:")
self.lbl_genre.grid(row=0, column=0, padx=20, sticky="W")
self.chk1 = Checkbutton(
self.checkbox_frame,
bg=COLOR_PURPLE,
text="Romance",
variable=self.chk1_variable,
)
self.chk1.grid(row=1, column=0, padx=25, pady=5, sticky="W")
self.chk2 = Checkbutton(
self.checkbox_frame,
bg=COLOR_PURPLE,
text="Fiction",
variable=self.chk2_variable,
)
self.chk2.grid(row=1, column=1, pady=5, sticky="W")
self.chk3 = Checkbutton(
self.checkbox_frame,
bg=COLOR_PURPLE,
text="Fantasy",
variable=self.chk3_variable,
)
self.chk3.grid(row=2, column=0, padx=25, pady=5, sticky="W")
self.chk4 = Checkbutton(
self.checkbox_frame,
bg=COLOR_PURPLE,
text="Nonfiction",
variable=self.chk4_variable,
)
self.chk4.grid(row=2, column=1, pady=5, sticky="W")
self.chk5 = Checkbutton(
self.checkbox_frame,
bg=COLOR_PURPLE,
text="History",
variable=self.chk5_variable,
)
self.chk5.grid(row=3, column=0, padx=25, pady=5, sticky="W")
self.chk6 = Checkbutton(
self.checkbox_frame,
bg=COLOR_PURPLE,
text="Childrens",
variable=self.chk6_variable,
)
self.chk6.grid(row=3, column=1, pady=5, sticky="W")
def create_visualization(self):
self.lbl_get_heat_map = Label(self.visualization_frame, bg=COLOR_PURPLE, text="Get heat map")
self.lbl_get_heat_map.grid(row=0, column=0, padx=20, pady=10, sticky="W")
self.btn_get_heat_map = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_heat_map(self.dataframe),
)
self.btn_get_heat_map.grid(row=0, column=1)
self.lbl_get_boxplot_rating = Label(self.visualization_frame, bg=COLOR_PURPLE, text="Get boxplot for rating")
self.lbl_get_boxplot_rating.grid(row=1, column=0, padx=20, pady=10, sticky="W")
self.btn_get_boxplot_rating = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_box_plot(self.dataframe, "rating"),
)
self.btn_get_boxplot_rating.grid(row=1, column=1)
self.lbl_get_boxplot_reviews = Label(
self.visualization_frame,
bg=COLOR_PURPLE,
text="Get boxplot for reviews",
)
self.lbl_get_boxplot_reviews.grid(row=2, column=0, padx=20, pady=10, sticky="W")
self.btn_get_boxplot_reviews = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_box_plot(self.dataframe, "reviews"),
)
self.btn_get_boxplot_reviews.grid(row=2, column=1)
self.lbl_get_boxplot_total_ratings = Label(
self.visualization_frame,
bg=COLOR_PURPLE,
text="Get boxplot for total ratings",
)
self.lbl_get_boxplot_total_ratings.grid(row=3, column=0, padx=20, pady=10, sticky="W")
self.btn_get_boxplot_total_ratings = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_box_plot(self.dataframe, "totalratings"),
)
self.btn_get_boxplot_total_ratings.grid(row=3, column=1)
self.lbl_get_top_genres = Label(self.visualization_frame, bg=COLOR_PURPLE, text="Get top 8 genres")
self.lbl_get_top_genres.grid(row=4, column=0, padx=20, pady=10, sticky="W")
self.btn_get_top_genres = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_histogram_genres(self.books),
)
self.btn_get_top_genres.grid(row=4, column=1)
self.lbl_get_tabloo_table = Label(self.visualization_frame, bg=COLOR_PURPLE, text="Get table for data")
self.lbl_get_tabloo_table.grid(row=5, column=0, padx=20, pady=10, sticky="W")
self.btn_get_tabloo_table = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_tabloo_table(self.dataframe),
)
self.btn_get_tabloo_table.grid(row=5, column=1, padx=20, pady=10)
self.lbl_get_tree_image = Label(self.visualization_frame, bg=COLOR_PURPLE, text="Get tree model")
self.lbl_get_tree_image.grid(row=6, column=0, padx=20, pady=10, sticky="W")
self.btn_get_tree_image = Button(
self.visualization_frame,
text="➔",
width=6,
bd=3,
bg=COLOR_GREEN,
command=lambda: self.generate_decision_tree(),
)
self.btn_get_tree_image.grid(row=6, column=1, padx=20, pady=10)
def create_recommend_button(self):
self.btn_get_user_entry = Button(
self.recommend_frame,
text="Recommend me a book",
width=25,
bd=3,
bg=COLOR_GREEN,
command=lambda: self.recommend_books(),
)
self.btn_get_user_entry.pack()
def recommend_books(self):
predicted_books = []
total_rating_books = 0
checked_options = self.get_checked_options()
entries = self.get_all_entries()
user_input = {"pages_new": [entries[0]], "rating_new": [entries[2]], "reviews_new": [entries[1]]}
dataframe = DataFrame(data=user_input)
total_rating_prediction = make_prediction_total_rating(self.decision_tree, dataframe)
if total_rating_prediction == 0:
total_rating_books = 1500
if total_rating_prediction == 1:
total_rating_books = 8000
else:
total_rating_books = 3820000
for book in self.books:
if book.total_ratings <= total_rating_books:
for genre in book.genres:
if (
genre in checked_options
and book.pages >= int(entries[0])
and book.rating >= int(entries[2])
and book.reviews >= int(entries[1])
):
predicted_books.append(book)
dataframe = DataFrame(data=predicted_books)
self.create_popup(
dataframe,
f"Your entries were:\
\nMinimun number of pages: {entries[0]}\
\nMinimun rating: {entries[2]}\
\nMinimun number of reviews: {entries[1]}\
\nGenres: {checked_options}\
\n\nBased on your entries total rating was predicted and books with your genres were chosen!",
)
def create_popup(self, dataframe: DataFrame, message: str):
self.popup_window = Toplevel(self.window, bg=COLOR_PURPLE)
self.popup_window.title("Recommended books")
self.popup_window.geometry(f"600x250+{WINDOW_Y_OFFSET+100}+{WINDOW_X_OFFSET+75}")
self.popup_frame = Frame(self.popup_window, bg=COLOR_PURPLE)
self.popup_frame.grid(row=0, column=0, pady=30, padx=55)
self.recommended_message = Label(
self.popup_frame,
text=message,
justify=CENTER,
bg=COLOR_PURPLE,
wraplength=500,
)
self.recommended_message.pack()
self.popup_button = Button(
self.popup_window,
text="Show recommendations",
width=25,
bd=3,
bg=COLOR_GREEN,
command=lambda: get_tabloo_table(dataframe),
)
self.popup_button.grid(row=1, column=0)
def get_checked_options(self):
chk_checked_values = []
if self.chk_options[0].get() == 1:
chk_checked_values.append("Romance")
if self.chk_options[1].get() == 1:
chk_checked_values.append("Fiction")
if self.chk_options[2].get() == 1:
chk_checked_values.append("Fantasy")
if self.chk_options[3].get() == 1:
chk_checked_values.append("Nonfiction")
if self.chk_options[4].get() == 1:
chk_checked_values.append("History")
if self.chk_options[5].get() == 1:
chk_checked_values.append("Childrens")
return chk_checked_values
def get_all_entries(self) -> List[str]:
entries = []
entries.append(self.ent_number_of_pages_variable.get())
entries.append(self.ent_minimum_reviews_variable.get())
entries.append(self.ent_minimum_rating_variable.get())
for i in range(len(entries)):
if entries[i] == '':
entries[i] = '0'
return entries
def generate_decision_tree(self):
# extract importance
print("\n")
importance = DataFrame({'feature': FEATURE_COLUMNS, 'importance' : round(self.decision_tree.feature_importances_, 3)})
importance.sort_values('importance', ascending=False, inplace=True)
print(importance)
get_decision_tree(self.decision_tree, FEATURE_COLUMNS)