forked from skhakim/Tome-A-Book-Recommender-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbutils.py
496 lines (456 loc) · 20.8 KB
/
dbutils.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import cx_Oracle as cx
import time
import pandas as pd
from matrix_factorization import *
""""
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
"""
def db_log_print(text, **kwargs):
with open('db_log.txt', 'a', encoding='UTF-8') as file:
print("\n", file=file)
print(time.asctime(), file=file)
print(text, kwargs, file=file)
def username_password_check(username, password):
flag = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
print(password, hash(password))
for row in cur.execute("SELECT READER_ID FROM READER WHERE USERNAME = :1 AND PASSWORD_HASH IN (:2, :3)",
[username, password, str(hash(password))]):
print(password, hash(password))
flag = row[0]
except Exception as e:
print("An error in username_password_check", e)
return flag
def info_for_username(username):
"""
for a given username,
find READER_ID, FULL_NAME, DATE_OF_BIRTH, GENDER, HOMETOWN, IMAGE_URL, JOIN_DATE
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT READER_ID, FULL_NAME, TO_CHAR(DATE_OF_BIRTH, 'YYYY, DD Month'), "
"GENDER, HOMETOWN, IMAGE_URL, TO_CHAR(JOIN_DATE, 'Mon-YYYY') "
"FROM PERSON INNER JOIN READER R on PERSON.PERSON_ID = R.READER_ID "
"WHERE Upper(USERNAME) = Upper(:1)", [username]):
ret['reader_id'] = row[0]
ret['name'] = row[1]
ret['dob'] = row[2]
ret['gender'] = row[3]
ret['hometown'] = row[4]
ret['image'] = row[5]
ret['join'] = row[6]
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM READS WHERE READER_ID = :1", [ret['reader_id']]):
ret['book'].append(row[0])
ret['eval'] = list()
for row in cur.execute("SELECT EVAL_ID FROM EVALUATION WHERE READER_ID = :1", [ret['reader_id']]):
ret['eval'].append(row[0])
except Exception as e:
print("An error in info_for_username", e)
db_log_print(ret)
return ret
def info_for_full_name(username):
"""
for a given full_name,
find READER_ID, FULL_NAME, DATE_OF_BIRTH, GENDER, HOMETOWN, IMAGE_URL, JOIN_DATE
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT READER_ID, FULL_NAME, TO_CHAR(DATE_OF_BIRTH, 'YYYY, DD Month'), "
"GENDER, HOMETOWN, IMAGE_URL, TO_CHAR(JOIN_DATE, 'Mon-YYYY') "
"FROM PERSON INNER JOIN READER R on PERSON.PERSON_ID = R.READER_ID "
"WHERE Upper(full_name) = Upper(:1)", [username]):
ret['reader_id'] = row[0]
ret['name'] = row[1]
ret['dob'] = row[2]
ret['gender'] = row[3]
ret['hometown'] = row[4]
ret['image'] = row[5]
ret['join'] = row[6]
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM READS WHERE READER_ID = :1", [ret['reader_id']]):
ret['book'].append(row[0])
ret['eval'] = list()
for row in cur.execute("SELECT EVAL_ID FROM EVALUATION WHERE READER_ID = :1", [ret['reader_id']]):
ret['eval'].append(row[0])
except Exception as e:
print("An error in info_for_username", e)
db_log_print(ret)
return ret
def info_for_reader_id(id):
"""
for a given username,
find READER_ID, FULL_NAME, DATE_OF_BIRTH, GENDER, HOMETOWN, IMAGE_URL, JOIN_DATE
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT READER_ID, FULL_NAME, TO_CHAR(DATE_OF_BIRTH, 'YYYY, DD Month'), "
"GENDER, HOMETOWN, IMAGE_URL, TO_CHAR(JOIN_DATE, 'Mon-YYYY') "
"FROM PERSON INNER JOIN READER R on PERSON.PERSON_ID = R.READER_ID "
"WHERE READER_ID = :1", [id]):
ret['reader_id'] = row[0]
ret['name'] = row[1]
ret['dob'] = row[2]
ret['gender'] = row[3]
ret['hometown'] = row[4]
ret['image'] = row[5]
ret['join'] = row[6]
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM READS WHERE READER_ID = :1 ORDER BY BOOK_ID DESC", [ret['reader_id']]):
ret['book'].append(row[0])
ret['eval'] = list()
for row in cur.execute("SELECT EVAL_ID FROM EVALUATION WHERE READER_ID = :1", [ret['reader_id']]):
ret['eval'].append(row[0])
except Exception as e:
print("An error in info_for_reader_id", e)
ret = None
db_log_print(ret)
return ret
def info_for_author_name(full_name):
"""
for a given author's full name,
find PERSON_ID, FULL_NAME, DoB, GENDER, HOMETOWN, IMAGE_URL, ABOUT, WEBPAGE
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT PERSON_ID, FULL_NAME, TO_CHAR(DATE_OF_BIRTH, 'YYYY, DD Month'), "
"GENDER, HOMETOWN, IMAGE_URL, ABOUT, WEBPAGE "
"FROM PERSON P INNER JOIN AUTHOR A2 on P.PERSON_ID = A2.AUTHOR_ID "
"WHERE Upper(FULL_NAME) = :1", [str(full_name).upper()]):
ret['author_id'] = row[0]
ret['name'] = row[1]
ret['dob'] = row[2]
ret['gender'] = row[3]
ret['hometown'] = row[4]
ret['image'] = row[5]
ret['about'] = row[6]
ret['webpage'] = row[7]
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM WRITES WHERE AUTHOR_ID = :1 "
"ORDER BY BOOK_ID DESC", [ret['author_id']]):
ret['book'].append(row[0])
ret['no_books'] = len(ret['book'])
for row in cur.execute("SELECT COUNT(FOLLOWER) FROM FOLLOWS "
"WHERE FOLLOWED=:1", [ret['author_id']]):
ret['fol'] = row[0]
except Exception as e:
print("An error in info_for_author_name", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def info_for_author_id(id):
"""
for a given author's brs id,
find PERSON_ID, FULL_NAME, DoB, GENDER, HOMETOWN, IMAGE_URL, ABOUT, WEBPAGE
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT PERSON_ID, FULL_NAME, TO_CHAR(DATE_OF_BIRTH, 'YYYY, DD Month'), "
"GENDER, HOMETOWN, IMAGE_URL, ABOUT, WEBPAGE "
"FROM PERSON P INNER JOIN AUTHOR A2 on P.PERSON_ID = A2.AUTHOR_ID "
"WHERE PERSON_ID = :1", [id]):
ret['author_id'] = row[0]
ret['name'] = row[1]
ret['dob'] = row[2]
ret['gender'] = row[3]
ret['hometown'] = row[4]
ret['image'] = row[5]
ret['about'] = row[6]
ret['webpage'] = row[7]
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM WRITES WHERE AUTHOR_ID = :1 ORDER BY BOOK_ID DESC", [ret['author_id']]):
ret['book'].append(row[0])
ret['no_books'] = len(ret['book'])
for row in cur.execute("SELECT COUNT(FOLLOWER) FROM FOLLOWS "
"WHERE FOLLOWED=:1", [ret['author_id']]):
ret['fol'] = row[0]
except Exception as e:
print("An error in info_for_author_id", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def info_for_book_title(title):
"""
:param title:title_of_a_book
:return: BOOK_ID, PUBLISHER_NAME, TITLE, PUBLICATION_DATE, ISBN, LANGUAGE, NUM_PAGES, IMAGE_URL, DESCRIPTION "
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT BOOK_ID, PUBLISHER_NAME, TITLE, "
"TO_CHAR(PUBLICATION_DATE, 'YYYY, DD Month'), "
"ISBN, LANGUAGE, NUM_PAGES, IMAGE_URL, DESCRIPTION "
"FROM BOOK LEFT JOIN PUBLISHER P on P.PUBLISHER_ID = BOOK.PUBLISHER_ID "
"WHERE Upper(TITLE) = :1", [str(title).upper()]):
ret['book_id'] = row[0]
ret['pub_name'] = row[1]
ret['title'] = row[2]
ret['pub_date'] = row[3]
ret['isbn'] = row[4]
ret['language'] = row[5]
ret['num_pages'] = row[6]
ret['image'] = row[7]
ret['description'] = row[8]
ret['genre'] = list()
for row in cur.execute("SELECT GENRE_NAME FROM GENRE, OF_GENRE WHERE OF_GENRE.GENRE_ID = GENRE.GENRE_ID "
"AND OF_GENRE.BOOK_ID = :1", [ret['book_id']]):
ret['genre'].append(row[0])
ret['author'] = list()
for row in cur.execute("SELECT WRITES.AUTHOR_ID FROM WRITES WHERE BOOK_ID = :1", [ret['book_id']]):
ret['author'].append(row[0])
ret['eval'] = list()
for row in cur.execute("SELECT EVAL_ID FROM EVALUATION WHERE BOOK_ID = :1", [ret['book_id']]):
ret['eval'].append(row[0])
ret['recom'] = list()
for row in cur.execute('''
SELECT TITLE, IMAGE_URL, BOOK_ID
FROM (SELECT BOOK, SCORE
FROM (
(SELECT BOOK_2 AS BOOK, SCORE FROM BOOK_SIMILAR WHERE BOOK_1 = :1)
UNION
(SELECT BOOK_1 AS BOOK, SCORE FROM BOOK_SIMILAR WHERE BOOK_2 = :2))
ORDER BY SCORE DESC) C
INNER JOIN BOOK ON BOOK_ID = C.BOOK
WHERE ROWNUM <= 10''', [ret['book_id'], ret['book_id']]):
ret['recom'] += [row]
if len(ret['recom']) == 0:
ret['recom'] = False
for row in cur.execute('SELECT COUNT(DISTINCT READER_ID) FROM READS WHERE BOOK_ID=:1'
, [ret['book_id']]):
ret['times_read'] = row[0]
for row in cur.execute('SELECT ROUND(AVG(RATING), 2) FROM EVALUATION WHERE BOOK_ID=:1'
, [ret['book_id']]):
ret['avg_rating'] = row[0]
except Exception as e:
print("An error in info_for_book_title", title, e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def info_for_book_id(id):
"""
:param id: goodreads_id_of_a_book
:return: BOOK_ID, PUBLISHER_NAME, TITLE, PUBLICATION_DATE, ISBN, LANGUAGE, NUM_PAGES, IMAGE_URL, DESCRIPTION "
"""
ret = None
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT BOOK_ID, PUBLISHER_NAME, TITLE, "
"TO_CHAR(PUBLICATION_DATE, 'YYYY, DD Month'), "
"ISBN, LANGUAGE, NUM_PAGES, IMAGE_URL, DESCRIPTION "
"FROM BOOK LeFT JOIN PUBLISHER P on P.PUBLISHER_ID = BOOK.PUBLISHER_ID "
"WHERE BOOK_ID = :1", [id]):
ret['book_id'] = row[0]
ret['pub_name'] = row[1]
ret['title'] = row[2]
ret['pub_date'] = row[3]
ret['isbn'] = row[4]
ret['language'] = row[5]
ret['num_pages'] = row[6]
ret['image'] = row[7]
ret['description'] = row[8]
ret['genre'] = list()
for row in cur.execute("SELECT GENRE_NAME FROM GENRE, OF_GENRE WHERE OF_GENRE.GENRE_ID = GENRE.GENRE_ID "
"AND OF_GENRE.BOOK_ID = :1", [ret['book_id']]):
ret['genre'].append(row[0])
ret['author'] = list()
for row in cur.execute("SELECT WRITES.AUTHOR_ID FROM WRITES WHERE BOOK_ID = :1", [ret['book_id']]):
ret['author'].append(row[0])
ret['eval'] = list()
for row in cur.execute("SELECT EVAL_ID FROM EVALUATION WHERE BOOK_ID = :1", [ret['book_id']]):
ret['eval'].append(row[0])
ret['recom'] = list()
for row in cur.execute('''
SELECT TITLE, IMAGE_URL, BOOK_ID
FROM (SELECT BOOK, SCORE
FROM (
(SELECT BOOK_2 AS BOOK, SCORE FROM BOOK_SIMILAR WHERE BOOK_1 = :1)
UNION
(SELECT BOOK_1 AS BOOK, SCORE FROM BOOK_SIMILAR WHERE BOOK_2 = :2))
ORDER BY SCORE DESC) C
INNER JOIN BOOK ON BOOK_ID = C.BOOK
WHERE ROWNUM <= 10''', [ret['book_id'], ret['book_id']]):
ret['recom'] += [row]
if len(ret['recom']) == 0:
ret['recom'] = False
for row in cur.execute('SELECT COUNT(DISTINCT READER_ID) FROM READS WHERE BOOK_ID=:1'
, [ret['book_id']]):
ret['times_read'] = row[0]
for row in cur.execute('SELECT ROUND(AVG(RATING), 2) FROM EVALUATION WHERE BOOK_ID=:1'
, [ret['book_id']]):
ret['avg_rating'] = row[0]
except Exception as e:
print(ret)
print("An error in info_for_book_id", id, e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def books_of_a_genre(genre_name):
"""
:param genre_name:
:return: all book ids of that genre
"""
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM GENRE, OF_GENRE "
"WHERE Upper(GENRE_NAME) = :1"
"AND GENRE.GENRE_ID = OF_GENRE.GENRE_ID ", [genre_name.upper()]):
ret['book'].append(row[0])
except Exception as e:
print("An error in books_of_a_genre", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def books_of_a_publisher(publisher_name):
"""
:param publisher_name:
:return: all book ids of that genre
"""
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM BOOK INNER JOIN PUBLISHER P on P.PUBLISHER_ID = BOOK.PUBLISHER_ID "
"WHERE upper(PUBLISHER_NAME) = :1", [publisher_name.upper()]):
ret['book'].append(row[0])
except Exception as e:
print("An error in books_of_a_publisher", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def books_of_a_language(language_name):
"""
:param language_name:
:return: all book ids of that genre
"""
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
ret['book'] = list()
for row in cur.execute("SELECT BOOK_ID FROM BOOK WHERE upper(LANGUAGE) = :1", [language_name.upper()]):
ret['book'].append(row[0])
except Exception as e:
print("An error in books_of_a_language", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def info_for_eval_id(eval_id):
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT RATING, REVIEW, DATE_UPDATED, B.BOOK_ID, TITLE, IMAGE_URL "
"FROM EVALUATION INNER JOIN BOOK B on B.BOOK_ID = EVALUATION.BOOK_ID "
"WHERE EVAL_ID = :1", [eval_id]):
ret['rating'] = row[0]
ret['review'] = row[1]
ret['date'] = row[2]
ret['book_id'] = row[3]
ret['title'] = row[4]
ret['image'] = row[5]
except Exception as e:
print("An error in info_for_eval_id", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
def info_reader_for_eval_id(eval_id):
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
ret = dict()
for row in cur.execute("SELECT RATING, REVIEW, to_char(DATE_UPDATED, 'YYYY, DD MONTH'), BOOK_ID,"
" READER_ID, PERSON_ID, FULL_NAME, IMAGE_URL "
" FROM EVALUATION INNER JOIN PERSON ON READER_ID = PERSON_ID "
" WHERE EVAL_ID = :1", [eval_id]):
ret['rating'] = row[0]
ret['review'] = row[1]
ret['date'] = row[2]
ret['book_id'] = row[3]
ret['reader_id'] = row[5]
ret['full_name'] = row[6]
ret['image'] = row[7]
except Exception as e:
print("An error in info_for_eval_id", e)
db_log_print(e)
ret = None
db_log_print(ret)
return ret
class MyRecommender:
def __init__(self):
try:
with cx.connect('ZOIN', 'ZOIN', dsn=cx.makedsn('localhost', 1521, None, 'ORCL'),
encoding="UTF-8") as con:
cur = con.cursor()
frame = pd.read_sql('''SELECT BOOK_ID, READER_ID, MAX(RATING) as M
FROM EVALUATION
GROUP BY BOOK_ID, READER_ID''', con=con)
self.frame = frame.rename(columns={
'BOOK_ID': 'item_id', 'READER_ID': 'user_id', 'M': 'rating'
})
(
self.X_train_initial,
self.y_train_initial,
self.X_train_update,
self.y_train_update,
self.X_test_update,
self.y_test_update,
) = train_update_test_split(self.frame, 0.01)
self.matrix_fact = KernelMF(n_epochs=20, n_factors=100, verbose=1, lr=0.001, reg=0.005)
self.matrix_fact.fit(self.X_train_initial, self.y_train_initial)
print('Fitting done.')
except Exception as e:
print("An error in constructing the recommender")
pass
def list_recommended_books(self, reader_id):
items_known = self.X_train_initial.query("user_id == @reader_id")["item_id"]
ret = self.matrix_fact.recommend(user=reader_id, items_known=items_known, amount=10)
return ret['item_id']
if __name__ == '__main__':
print("ewfr")