-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_all_recommendations.py
215 lines (199 loc) · 6.31 KB
/
get_all_recommendations.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
from getRecommendation import get_recommendation
from collections import Counter
from collections import defaultdict
def get_books():
with open('books.txt') as f:
books = f.read().splitlines()
for i, book in enumerate(books):
temp = book.split("(")
if(len(temp) > 1):
books[i] = temp[0][:-1]
return books
def get_movies():
with open('movies.txt') as f:
movies = f.read().splitlines()
for i, movie in enumerate(movies):
temp = movie.split("(")
if(len(temp) > 1):
movies[i] = temp[0][:-1]
return movies
def get_shows():
with open('tvshows.txt') as f:
shows = f.read().splitlines()
for i, show in enumerate(shows):
temp = show.split("(")
if(len(temp) > 1):
shows[i] = temp[0][:-1]
return shows
def get_games():
with open('games.txt') as f:
games = f.read().splitlines()
for i, game in enumerate(games):
temp = game.split("(")
if(len(temp) > 1):
games[i] = temp[0][:-1]
return games
def get_book_recommendation():
books = get_books()
count = 0
book_recom = []
book_recom_based = defaultdict()
for i in range(len(books)):
count += 1
if count%10 == 1:
print(count)
else:
print(count)
recommendations = get_recommendation("book", books[i])
book_recom.extend(recommendations)
book_recom_based[books[i]] = recommendations
print("books done")
book_recom = [x for x in book_recom if x not in books]
book_recom_part2 = []
for i in range(len(book_recom)):
temp = book_recom[i].split("(")
if len(temp) > 1:
book_recom[i] = temp[0][:-1]
book_recom_part2.append([temp[0][:-1], temp[1]])
book_recom = [{k:v} for k, v in sorted(Counter(book_recom).items(), key=lambda item: item[1], reverse=True)]
for i in range(len(book_recom)):
for j in range(len(book_recom_part2)):
for key in book_recom[i]:
if key == book_recom_part2[j][0]:
book_recom[i][key+" ("+book_recom_part2[i][1]] = book_recom[i].pop(key)
recom_based = []
for i in range(len(book_recom)):
temp = []
for k,v in book_recom_based.items():
for key in book_recom[i]:
if key in v:
temp.append(k)
recom_based.append(temp)
for i in range(len(book_recom)):
book_recom[i].update({'similar':recom_based[i]})
with open('book_recommmendation.txt', 'w') as f:
for item in book_recom:
f.write("%s\n" % item)
def get_movie_recommendation():
movies = get_movies()
count = 0
movie_recom = []
movie_recom_based = defaultdict()
for i in range(len(movies)):
count += 1
if count%10 == 1:
print(count)
recommendations = get_recommendation("movie", movies[i])
movie_recom.extend(recommendations)
movie_recom_based[movies[i]] = recommendations
print("movis done")
movie_recom = [x for x in movie_recom if x not in movies]
movie_recom_part2 = []
for i in range(len(movie_recom)):
temp = movie_recom[i].split("(")
if len(temp) > 1:
movie_recom[i] = temp[0][:-1]
movie_recom_part2.append([temp[0][:-1], temp[1]])
movie_recom = [{k:v} for k, v in sorted(Counter(movie_recom).items(), key=lambda item: item[1], reverse=True)]
for i in range(len(movie_recom)):
for j in range(len(movie_recom_part2)):
for key in movie_recom[i]:
if key == movie_recom_part2[j][0]:
movie_recom[i][key+" ("+movie_recom_part2[i][1]] = show_recom[i].pop(key)
recom_based = []
for i in range(len(movie_recom)):
temp = []
for k,v in movie_recom_based.items():
for key in movie_recom[i]:
if key in v:
temp.append(k)
recom_based.append(temp)
for i in range(len(movie_recom)):
movie_recom[i].update({'similar':recom_based[i]})
with open('movie_recommmendation.txt', 'w') as f:
for item in movie_recom:
f.write("%s\n" % item)
def get_show_recommendation():
shows = get_shows()
count = 0
show_recom = []
show_recom_based = defaultdict()
for i in range(len(shows)):
count += 1
if count%10 == 1:
print(count)
recommendations = get_recommendation("show", shows[i])
show_recom.extend(recommendations)
show_recom_based[shows[i]] = recommendations
print("shows done")
show_recom = [x for x in show_recom if x not in shows]
show_recom_part2 = []
for i in range(len(show_recom)):
temp = show_recom[i].split("(")
if len(temp) > 1:
show_recom[i] = temp[0][:-1]
show_recom_part2.append([temp[0][:-1], temp[1]])
show_recom = [{k:v} for k, v in sorted(Counter(show_recom).items(), key=lambda item: item[1], reverse=True)]
for i in range(len(show_recom)):
for j in range(len(show_recom_part2)):
for key in show_recom[i]:
if key == show_recom_part2[j][0]:
show_recom[i][key+" ("+show_recom_part2[i][1]] = show_recom[i].pop(key)
recom_based = []
for i in range(len(show_recom)):
temp = []
for k,v in show_recom_based.items():
for key in show_recom[i]:
if key in v:
temp.append(k)
recom_based.append(temp)
for i in range(len(show_recom)):
show_recom[i].update({'similar':recom_based[i]})
with open('show_recommmendation.txt', 'w') as f:
for item in show_recom:
f.write("%s\n" % item)
def get_game_recommendation():
games = get_games()
count = 0
game_recom = []
game_recom_based = defaultdict()
for i in range(len(games)):
count += 1
if count%10 == 1:
print(count)
recommendations = get_recommendation("game", games[i])
game_recom.extend(recommendations)
game_recom_based[games[i]] = recommendations
print("games done")
game_recom = [x for x in game_recom if x not in games]
game_recom_part2 = []
for i in range(len(game_recom)):
temp = game_recom[i].split("(")
if len(temp) > 1:
game_recom[i] = temp[0][:-1]
game_recom_part2.append([temp[0][:-1], temp[1]])
game_recom = [{k:v} for k, v in sorted(Counter(game_recom).items(), key=lambda item: item[1], reverse=True)]
for i in range(len(game_recom)):
for j in range(len(game_recom_part2)):
for key in game_recom[i]:
if key == game_recom_part2[j][0]:
game_recom[i][key+" ("+game_recom_part2[i][1]] = game_recom[i].pop(key)
recom_based = []
for i in range(len(game_recom)):
temp = []
for k,v in game_recom_based.items():
for key in game_recom[i]:
if key in v:
temp.append(k)
recom_based.append(temp)
for i in range(len(game_recom)):
game_recom[i].update({'similar':recom_based[i]})
with open('game_recommmendation.txt', 'w', encoding="utf-8") as f:
for item in game_recom:
f.write("%s\n" % item)
def get_all_recommendations():
get_game_recommendation()
get_show_recommendation()
get_movie_recommendation()
get_book_recommendation()
get_all_recommendations()