-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoadData.py
95 lines (70 loc) · 2.74 KB
/
LoadData.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
from models.DataSet import Trajectory, SeqDataSet
import pickle
def loadMsnbc(minLength=3,maxLength=None):
msnbc_path = 'data/msnbc/msnbc990928.seq'
with open(msnbc_path) as fp:
lines = fp.readlines()
line_num = len(lines)
dataset = SeqDataSet(list(range(17)))
for i in range(7,line_num):
line = lines[i]
line_split = line.split()
for i in range(len(line_split)):
line_split[i] = int(line_split[i]) - 1 # locations starts form 0
if minLength is not None and len(line_split) < minLength:
continue
if maxLength is not None and len(line_split) > maxLength:
continue
traj = Trajectory(line_split)
dataset.add_line(traj)
with open("./data/msnbc.pickle",'wb') as fp:
pickle.dump(dataset,fp)
return dataset
def loadMsnbc_trimmed(requiredlocations,minLength=3,maxLength=None):
msnbc_path = 'data/msnbc/msnbc990928.seq'
with open(msnbc_path) as fp:
lines = fp.readlines()
line_num = len(lines)
dataset = SeqDataSet(list(range(requiredlocations)))
location_count = {}
for i in range(7,line_num):
line = lines[i]
line_split = line.split()
for i in range(len(line_split)):
line_split[i] = int(line_split[i]) # locations starts form 0
if minLength is not None and len(line_split) < minLength:
continue
if maxLength is not None and len(line_split) > maxLength:
continue
for k in line_split:
if k not in location_count.keys():
location_count[k] = 0
location_count[k] += 1
locationsort = sorted(location_count.items(),key=lambda x: x[1],reverse=True)
locationdict = {}
for i in range(requiredlocations):
locationdict[locationsort[i][0]] = i
print(locationdict)
for i in range(7,line_num):
line = lines[i]
line_split = line.split()
for i in range(len(line_split)):
line_split[i] = int(line_split[i]) # locations starts form 0
if minLength is not None and len(line_split) < minLength:
continue
if maxLength is not None and len(line_split) > maxLength:
continue
trimmedtraj = []
for k in line_split:
if k not in locationdict.keys():
continue
trimmedtraj.append(locationdict[k])
traj = Trajectory(trimmedtraj)
dataset.add_line(traj)
filepath = "./data/msnbc" + str(requiredlocations) + '.pickle'
with open(filepath,'wb') as fp:
pickle.dump(dataset,fp)
return dataset
if __name__ == '__main__':
#ds = loadMsnbc()
loadMsnbc_trimmed(requiredlocations=14)