-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDataID.py
More file actions
84 lines (69 loc) · 2.25 KB
/
getDataID.py
File metadata and controls
84 lines (69 loc) · 2.25 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
from conn import connElasticSearch
from getDataS import getdata
from teachdata import teach_data
def getdataisbnUid(keyword, userId, activate_nn=False):
#book avg
def isbnRatingAvg(book_code):
# match isbn and boost
query = {
"bool": {
"must": [
{
"match": {
"isbn": book_code
}
}
],
"should": [
{
"match": {
"uid": userId
}
}
]
}
}
# execute query
rslt = cn.search(index='bratings', query=query, size=10000)
if not rslt['hits']['hits']:
return 0, 0
sum = 0
counter = 0
# get user's grade
userRating = int(rslt['hits']['hits'][0]['_source']['rating'])
for ht in rslt['hits']['hits']:
# grade > 0
somebodies_rates = int(ht['_source']['rating'])
if somebodies_rates:
sum += somebodies_rates
counter += 1
# checking for division by zero
if counter != 0:
return (sum / counter), userRating
else:
return sum, userRating
cn = connElasticSearch()
# get all the books matching keyword
books = getdata('books', keyword)
avgs = []
ratings = []
# separate lists for user's book ratings and avg's
for isbn in books['isbn']:
mean, rate = isbnRatingAvg(isbn)
avgs += [mean]
ratings += [rate]
books['rating'] = ratings
if activate_nn:
# books with no rating by user
fill = (books['rating'] == 0)
# check if there are unrated books
if fill.value_counts()[0]:
books.loc[fill, 'rating'] = teach_data(userId, books[fill])
score = []
# formulating and calculating the scores
for i in range(len(books)):
score += [(0.65 * books.loc[i, 'score']) + (0.25 * books.loc[i, 'rating']) + (0.1 * avgs[i])]
# replace with the new custom scores
books['score'] = score
# sort by 'score'
return books.sort_values(by=['score'], ascending=False)