-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
69 lines (53 loc) · 2.59 KB
/
helper.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
import math
from dataservice import DataService
import operator
import math
class Helper(object):
@classmethod
def cosine_similarity(cls, app_list1, app_list2):
match_count = cls.__count_match(app_list1, app_list2)
return float(match_count) / math.sqrt(len(app_list1) * len(app_list2))
@classmethod
def __count_match(cls, list1, list2):
count = 0
for element in list1:
if element in list2:
count+= 1
return count
def calculate_user_top_5(user_id, user_download_history, all_download_history):
app_similarity = {}
for apps in all_download_history:
similarity = Helper.cosine_similarity(user_download_history, apps)
for other_app in apps:
if app_similarity.has_key(other_app):
app_similarity[other_app] += similarity
else:
app_similarity[other_app] = similarity
for app in user_download_history:
app_similarity.pop(app)
sorted_tups = sorted(app_similarity.items(), key=operator.itemgetter(1), reverse=True)
top_5_app = [sorted_tups[0][0], sorted_tups[1][0], sorted_tups[2][0], sorted_tups[3][0], sorted_tups[4][0]]
print("top_5_app for " + str(user_id) + ":\t" + str(top_5_app))
DataService.update_user_info({'user_id' : user_id}, {'$set' : {'top_5_app': top_5_app}})
# all_download_history is the entire list of history {(downloaded app list)}
def calculate_app_top_5(app, all_download_history):
# create a dict to store each other app and its similarity to this app_list2
app_similarity = {} # {app_id: similarity}
for apps in all_download_history:
# calculate the similarity of the app and the user download history
similarity = Helper.cosine_similarity([app], apps)
for other_app in apps:
if app_similarity.has_key(other_app):
app_similarity[other_app] = app_similarity[other_app] + similarity
else:
app_similarity[other_app] = similarity
# there could be app without related app (not in any download history)
if not app_similarity.has_key(app):
return
# sort app_similarity dict by value and get the top 5 as recommendation
app_similarity.pop(app)
#sort by similarity
sorted_tups = sorted(app_similarity.items(), key=operator.itemgetter(1), reverse=True)
top_5_app = [sorted_tups[0][0], sorted_tups[1][0], sorted_tups[2][0], sorted_tups[3][0], sorted_tups[4][0]]
# print("top_5_app for " + str(app) + ":\t" + str(top_5_app))
DataService.update_app_info({'app_id' : app}, {'$set' : {'top_5_app': top_5_app}})