-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_SearchEngine_w_Clustering.py
196 lines (171 loc) · 5.85 KB
/
2_SearchEngine_w_Clustering.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
from elasticsearch import Elasticsearch,helpers
import pandas as pd
import csv
import os,sys,csv
def load_data(csv_name,index_name):
curdir = os.path.dirname(__file__)
csv_file = curdir+"/datasets/"+csv_name
f = open(csv_file,"r")
dictionar = csv.DictReader(f)
res = helpers.bulk(es,dictionar,index=index_name)
print("Data Loaded")
def load_csv(csv_name):
curdir = os.path.dirname(__file__)
csv_file = curdir+"./datasets/"+csv_name
csv = pd.read_csv(csv_file)
return csv
def get_movies(phrase,num = 10):
query_body = {
"query":{
"bool":{
"should":[{
"match":{
"title":{
"query":phrase,
"fuzziness": "AUTO"
}
}},
{
"match":{
"genres":{
"query":phrase,
"fuzziness": "AUTO"
}
}}
]
}
}
}
res = es.search(index="movies",body=query_body,size=num)
return res
#φορτώνει το αρχείο clusters.csv και βρίσκει τον cluster στον οποίο ανήκει ο χρήστης
#και στη συνέχεια υπολογίζει τον μέσο όρο των βαθμολογιών του cluster για την συγκεκριμένη
#ταινια.
#Αν κανένας απο τον cluster δεν έχει δει την ταινία επιστρέφει τιμή -1
def getClusterEval(userId,movieId):
clusters = load_csv("clusters.csv")
userCluster = clusters[clusters["userId"]==userId].iloc[0]["cluster"]
userList = clusters[clusters["cluster"]==userCluster]
clusterAverage = {"val":0,"n":0}
for index,row in userList.iterrows():
query_body={
"query": {
"bool": {
"must": [
{
"match": {
"userId": int(row["userId"])
}
},{
"match" : {
"movieId":int(movieId)
}
}
]
}
}
}
res = es.search(index="ratings",body=query_body,filter_path=["hits.hits._source.rating"])
if res:
clusterAverage["val"]+=float(res["hits"]["hits"][0]["_source"]["rating"])
clusterAverage["n"]+=1
if clusterAverage["n"]!=0:
return clusterAverage["val"]/clusterAverage["n"]
else:
return -1
def getUserEval(userId,movieId):
query_body={
"query": {
"bool": {
"must": [
{
"match": {
"userId": userId
}
},{
"match" : {
"movieId":movieId
}
}
]
}
}
}
res = es.search(index="ratings",body=query_body,filter_path=["hits.hits._source.rating"])
if res:
return float(res["hits"]["hits"][0]["_source"]["rating"])
else: return -1
def getAverageEval(movieId):
query_body={
"query": {
"bool": {
"must": [
{
"match" : {
"movieId":movieId
}
}
]
}
}
}
res= es.search(index="ratings",body=query_body,size=671)
n = len(res["hits"]["hits"])
if n==0: return 0
rating = 0
for x in res["hits"]["hits"]:
rating = rating + float(x["_source"]["rating"])
return rating/n
def print_movies(res):
if not res:
print("no results")
return
else:
for x in res:
print()
print("===========================")
print("ID: ",str(x["_id"]))
print("Title: ",x["_source"]["title"])
print("Genres: ",x["_source"]["genres"])
print('Normalized Elastic Rating: %.2f' % x["_oldScore"])
if x["_cluster"]:
print('Users Rating: N/A')
print('User Cluster Rating: ',x["_userEval"])
else:
print('Users Rating: ',x["_userEval"])
print('Average Rating: %.2f / 5' % x["_movieEval"])
print('New_Score: %.2f'% x["_score"],"/ 10")
#η Calculate score δεν αλλάζει, αν ένας χρήστης δεν έχει δει μια ταινία
#αλλα υπάρχει βαθμολογία απο τον cluster του τότε χρησιμοποιείται αυτή για userEval.
def calculate_score(old_score,userEval,movieEval):
if userEval>=0:
return 6*old_score + 2*movieEval + 2*2*(userEval-0.5)
else:
return 6*old_score + 4*movieEval
def search_movie(phrase,userId,num=10):
res = get_movies(phrase,num)
if res["hits"]["total"]["value"]==0: return
elastic_max_score = res["hits"]["max_score"]
for x in res["hits"]["hits"]:
userEval = getUserEval(movieId=x["_id"],userId=userId)
##αν το userEval=-1 δες τον cluster του user
if userEval==-1:
userEval = getClusterEval(userId=userId,movieId=x["_id"])
x["_cluster"] = True
movieEval = getAverageEval(x["_id"])
x["_movieEval"]= round(movieEval,1)
old_score = x["_score"]/elastic_max_score
x["_oldScore"] = old_score
if userEval>=0: x["_userEval"] = round(userEval,1)
else: x["_userEval"] = "N/A"
new_score = calculate_score(old_score,userEval/5,movieEval/5)
x["_score"] = new_score
sorted_movies = sorted(res["hits"]["hits"], key=lambda x: x["_score"],reverse=True)
return sorted_movies
if __name__=="__main__":
es = Elasticsearch(host="localhost",port=9200,timeout=500)
load_data("ratings.csv","ratings")
a = input("Search for a movie: ")
b = int(input("User ID: "))
res = search_movie(phrase=a,userId=b,num=10)
print_movies(res)