-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_prepare.py
182 lines (160 loc) · 5.2 KB
/
data_prepare.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
import os
import time
from collections import defaultdict
import numpy as np
def get_date(ts):
timeArray = time.localtime(float(ts))
ntCtime_str = time.strftime("%m %d, %Y", timeArray)
# print(ntCtime_str)
return ntCtime_str
def write_pos_file(datastore_path, dataset_name):
def count_item(_path_to_data, _item_count):
f = open(_path_to_data, 'r')
for line in f:
try:
u, i, t = line.rstrip().split('\t')
try:
u = int(u)
i = int(i)
t = int(t)
except:
u = int(u)
i = int(i)
t = int(float(t))
except:
u, i, r, t = line.rstrip().split('\t')
try:
u = int(u)
i = int(i)
t = int(t)
except:
u = int(u)
i = int(i)
t = int(float(t))
_item_count[i] += 1
f.close()
def count_user(_path_to_data, _item_count, _user_count):
# should after count item
f = open(_path_to_data, 'r')
for line in f:
try:
u, i, t = line.rstrip().split('\t')
try:
u = int(u)
i = int(i)
t = int(t)
except:
u = int(u)
i = int(i)
t = int(float(t))
except:
u, i, r, t = line.rstrip().split('\t')
try:
u = int(u)
i = int(i)
t = int(t)
except:
u = int(u)
i = int(i)
t = int(float(t))
if _item_count[i] < 5:
continue
_user_count[u] += 1
f.close()
data_path = datastore_path + '/' + dataset_name + '/'
item_count = defaultdict(lambda: 0)
user_count = defaultdict(lambda: 0)
count_item(data_path + dataset_name + '.txt', item_count)
count_user(data_path + dataset_name + '.txt', item_count, user_count)
f = open(data_path + dataset_name + '.txt', 'r')
user_dict = defaultdict(list)
usermap = dict()
usernum = 0
itemmap = dict()
itemnum = 0
for line in f:
try:
u, i, t = line.rstrip().split('\t')
try:
u = int(u)
i = int(i)
t = int(t)
except:
u = int(u)
i = int(i)
t = int(float(t))
except:
u, i, r, t = line.rstrip().split('\t')
try:
u = int(u)
i = int(i)
t = int(t)
except:
u = int(u)
i = int(i)
t = int(float(t))
if item_count[i] < 5 or user_count[u] < 5:
continue
if u in usermap:
userid = usermap[u]
else:
usernum += 1
userid = usernum
usermap[u] = userid
if i in itemmap:
itemid = itemmap[i]
else:
itemnum += 1
itemid = itemnum
itemmap[i] = itemid
user_dict[userid].append([itemid, t])
f.close()
print(len(user_dict))
for userid in user_dict.keys():
user_dict[userid].sort(key=lambda x: x[1])
f = open(data_path + dataset_name + '_all.txt', 'w')
for user_id in user_dict.keys():
for i in user_dict[user_id]:
f.write(str(user_id) + '\t' + str(i[0]) + '\t' + str(i[1]) + '\t' + get_date(i[1]) + '\n')
f.close()
f = open(data_path + dataset_name + '_lite.txt', 'w')
for user_id in user_dict.keys():
for i in user_dict[user_id]:
f.write('%d %d\n' % (user_id, i[0]))
f.close()
def write_neg_file(datastore_path, dataset_name):
data_path = datastore_path + '/' + dataset_name + '/'
adj_list_original = defaultdict(list)
test_candidate = {}
# assume user/item index starting from 1
path_to_data = data_path + dataset_name + '_all.txt'
max_item = 0
f = open(path_to_data, 'r')
for line in f:
u, i, t, d = line.rstrip().split('\t')
u = int(u)
i = int(i)
if i > max_item:
max_item = i
adj_list_original[u].append(i)
user_list = list(adj_list_original.keys())
user_list.sort()
f.close()
write_list = []
for user_id in user_list:
temp_neg_list = []
while len(temp_neg_list) < 100:
neg_id = np.random.randint(1, max_item)
if neg_id not in adj_list_original[user_id]:
temp_neg_list.append(neg_id)
for neg_id in temp_neg_list:
write_list.append(str(user_id) + '\t' + str(neg_id) + '\n')
f = open(data_path + dataset_name + '_test_neg.txt', 'w')
f.writelines(write_list)
f.close()
if __name__ == "__main__":
# Data prepare for TGCN4SR & HyperRec, original data is from TiSASRec
datastore_path = 'tisasrec_data'
dataset_name = 'steam'
write_pos_file(datastore_path, dataset_name)
write_neg_file(datastore_path, dataset_name)