-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_lastScore.py
149 lines (123 loc) · 5.64 KB
/
get_lastScore.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
# -*- coding: utf-8 -*-
"""
Created on Mon May 02 13:46:36 2016
@author: Yudi Dong
"""
#Build a dictionary to store the scores of users.
USER_SCORE = 'RawData/trainIdx2.txt'
lib_userscore={}
lib_allscore = {}
with open(USER_SCORE) as userScore:
for line in userScore:
if "|" in line:
[user_id,number] = line.strip("\n").split("|")
lib_itemscore={}
else:
[item_id,score] = line.split()
lib_itemscore[item_id]=score
lib_userscore[user_id]=lib_itemscore
##Create the list to store the user id in the test set
TEST_SET = 'RawData/testIdx2.txt'
user_id_test = []
with open(TEST_SET) as test_set:
for line in test_set:
if "|" in line:
[user_id, number] = line.strip("\n").split("|")
user_id_test.append(user_id)
##process the trackData2.txt : create a dictionary {"track_id":["album_id","artist_id"]}
TRACK_HI = "RawData/trackData2.txt"
lib_track ={}
with open(TRACK_HI) as track_hi:
for line in track_hi:
track_split=line.strip("\n").split("|")
lib_track[track_split[0]] = [track_split[1], track_split[2]]
##create a dictionary to store the very test users' rating of tracks{"user_id":{"track_id":rating}}
TRACK_RATING = "Data/track_userrating.txt"
lib_user_trackRating ={}
nest_trackRating = {}
with open(TRACK_RATING) as track_rating:
for line in track_rating:
if "|" in line:
[user_id, other] = line.strip("\n").split("|")
nest_trackRating = {}
else:
[item_id,score] = line.strip("\n").split("\t")
nest_trackRating[item_id] = score
lib_user_trackRating[user_id] = nest_trackRating
##Create a dictionary {"user_id":{"track_id":[[album,artist],[albumtrack],[artisttrack]]} based on the test hierarchy
TEST_HI ="Data/test_hierarchy.txt"
LAST_SCORE = "Data/last_score.txt"
lib_test_hi = {} #{"user_id":{"track_id":[[album,artist],[albumtrack],[artisttrack]]}
index ={}
albumtrack = []
artisttrack = []
count = 0
with open(TEST_HI) as test_hi:
for line in test_hi:
if count == 6:
count = 0
index ={}
count = count +1
line_list = line.strip("\n").split("|")
albumartist = [line_list[2],line_list[3]]
for track_id in lib_user_trackRating[line_list[0]].keys():
if lib_track[track_id][0] == line_list[2]:
albumtrack.append(track_id)
if lib_track[track_id][1] == line_list[3]:
artisttrack.append(track_id)
index[line_list[1]] = [albumartist,albumtrack,artisttrack]
albumtrack = []
artisttrack = []
lib_test_hi[line_list[0]]=index
for user_id in user_id_test:
for key in lib_test_hi[user_id]:
if lib_userscore[user_id].has_key(lib_test_hi[user_id][key][0][0]):
lib_test_hi[user_id][key][0][0] = lib_userscore[user_id][lib_test_hi[user_id][key][0][0]]
else:
lib_test_hi[user_id][key][0][0] = "None"
if lib_userscore[user_id].has_key(lib_test_hi[user_id][key][0][1]):
lib_test_hi[user_id][key][0][1] = lib_userscore[user_id][lib_test_hi[user_id][key][0][1]]
else:
lib_test_hi[user_id][key][0][1] = "None"
if len(lib_test_hi[user_id][key][1]) !=0:
for i in range(len(lib_test_hi[user_id][key][1])):
if lib_userscore[user_id].has_key(lib_test_hi[user_id][key][1][i]):
lib_test_hi[user_id][key][1][i] = lib_userscore[user_id][lib_test_hi[user_id][key][1][i]]
else:
lib_test_hi[user_id][key][1][i] = "None"
if len(lib_test_hi[user_id][key][2]) !=0:
for i in range(len(lib_test_hi[user_id][key][2])):
if lib_userscore[user_id].has_key(lib_test_hi[user_id][key][2][i]):
lib_test_hi[user_id][key][2][i] = lib_userscore[user_id][lib_test_hi[user_id][key][2][i]]
else:
lib_test_hi[user_id][key][2][i] = "None"
lib_lastScore = {}
for user_id in user_id_test:
track_lastscore ={}
for key in lib_test_hi[user_id]:
score = 0
if lib_test_hi[user_id][key][0][0] !="None":
score = score + float(lib_test_hi[user_id][key][0][0])
if lib_test_hi[user_id][key][0][1] !="None":
score = score + float(lib_test_hi[user_id][key][0][1])*0.2
if len(lib_test_hi[user_id][key][1]) !=0:
for i in range(len(lib_test_hi[user_id][key][1])):
if lib_test_hi[user_id][key][1][i] == "None":
lib_test_hi[user_id][key][1][i] = 0
else:
lib_test_hi[user_id][key][1][i] = float(lib_test_hi[user_id][key][1][i])
score = score + (sum(lib_test_hi[user_id][key][1])/len(lib_test_hi[user_id][key][1]))*0.1
if len(lib_test_hi[user_id][key][2]) !=0:
for i in range(len(lib_test_hi[user_id][key][2])):
if lib_test_hi[user_id][key][2][i] == "None":
lib_test_hi[user_id][key][2][i] = 0
else:
lib_test_hi[user_id][key][2][i] = float(lib_test_hi[user_id][key][2][i])
score = score + (sum(lib_test_hi[user_id][key][2])/len(lib_test_hi[user_id][key][2]))*0.02
track_lastscore[key] = score
lib_lastScore[user_id] = track_lastscore
with open(LAST_SCORE,'w') as last_score:
with open(TEST_HI) as test_hi:
for line in test_hi:
[user_id, track_id, other]=line.strip("\n").split("|",2)
last_score.write(user_id+"|"+track_id+"|"+str(lib_lastScore[user_id][track_id])+"\n")