/
recommend.py
126 lines (110 loc) · 3.75 KB
/
recommend.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
# coding: UTF-8
from pprint import pprint
import csv
# generate dictionary
def generate_each_rank_dict_by_user_id(row, user_id, sim_dict):
"""
{
'1':[
{...},
{...},
],
'2':[
{...},
{...},
]
}
"""
if row[0] != user_id:
return False
rank = row[4]
if rank in sim_dict:
tmp_dict = {
'type': row[3],
'user_clothes': row[1],
'rank_clothes': row[2],
'similarity': round(float(row[5]), 5)
}
sim_dict[rank].append(tmp_dict)
else:
sim_dict[row[4]] = [{
'type': row[3],
'user_clothes': row[1],
'rank_clothes': row[2],
'similarity': round(float(row[5]), 5)
}]
def get_best_clothes_by_rank(rank_list, self_rank):
"""
{
'tops':[{
'rank_clothes':'',
'user_clothes':'',
'similarity':0}],
'bottoms':[{
'rank_clothes':'',
'user_clothes':'',
'similarity':0}],
'similarity':0
}
"""
rank = {
'Tops': [],
'Bottoms': [],
'similarity': 1
}
# ここらの実装は要検討
# 多分組み合わせ問題を解く必要が出てくる
# tops 及び bottoms のランキング画像複数あるときどうしようね
for row in rank_list:
if rank[row['type']] == [] and row['similarity'] != 0:
rank[row['type']] = [{
'rank_clothes': row['rank_clothes'],
'user_clothes': row['user_clothes'],
'similarity': row['similarity']
}]
if any(d['rank_clothes'] != row['rank_clothes'] for d in rank[row['type']]) and row['similarity'] != 0:
rank[row['type']].append({
'rank_clothes': row['rank_clothes'],
'user_clothes': row['user_clothes'],
'similarity': row['similarity']
})
else:
for item in rank[row['type']]:
if item['rank_clothes'] == row['rank_clothes'] \
and item['user_clothes'] != row['user_clothes'] \
and item['similarity'] < row['similarity']:
item['user_clothes'] = row['user_clothes']
item['similarity'] = row['similarity']
for row in rank['Tops']:
rank['similarity'] = rank['similarity'] * row['similarity']
for row in rank['Bottoms']:
rank['similarity'] = rank['similarity'] * row['similarity']
return {self_rank: rank}
def main():
with open('all_pattern_of_Similarity.csv', 'r') as f:
reader = csv.reader(f)
header = next(reader)
user_id = 'U4fce6cc2cc3530ae2f4b7ca0609edd40'
sim_dict = {}
most_sim = []
for row in reader:
generate_each_rank_dict_by_user_id(row)
for row in sim_dict:
most_sim.append(get_best_clothes_by_rank(sim_dict[row], row))
similarity_ranking = {}
most_sim_recommend_key = []
RECOMMEND_MAX = 5
for k in most_sim:
for row in k:
similarity_ranking[str(row)] = k[str(row)]['similarity']
for k, v in sorted(similarity_ranking.items(), key=lambda x: x[1], reverse=True):
most_sim_recommend_key.append(k)
print(most_sim_recommend_key)
for k in most_sim:
for index in k:
for i in range(0, RECOMMEND_MAX):
if index == most_sim_recommend_key[i]:
# kに推薦したいオブジェクトが入ってるのでBot側で頑張って
return k
if __name__ == '__main__':
main()