-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprocess_tkg_datasets.py
173 lines (142 loc) · 4.87 KB
/
process_tkg_datasets.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
import argparse
import os
import pdb
import string
import re
from process_interpolation_dataset import *
from collections import Counter, defaultdict
def create_ent_rel_to_idx():
entities = []
relations = []
train_triples = []
valid_triples = []
test_triples = []
times = []
with open(os.path.join(input_dir, "train.txt"), "r") as f:
for line in f:
line_split = line.strip().split('\t')
try:
start_time = int(line_split[3].split('-')[0])
times.append(start_time)
except:
pass
try:
end_time = int(line_split[4].split('-')[0])
times.append(end_time)
except:
pass
times = list(set(times))
min_time = min(times)
max_time = max(times)
years = list()
for data_split, triple_lst in zip(['train', 'valid', 'test'], [train_triples, valid_triples, test_triples]):
with open(os.path.join(input_dir, "{}.txt".format(data_split)), "r") as f:
for line in f:
line_split = line.strip().split('\t')
head = line_split[0]
tail = line_split[2]
rel = line_split[1]
entities.append(head)
entities.append(tail)
relations.append(rel)
start_time = line_split[3].split('-')[0]
end_time = line_split[4].split('-')[0]
if start_time == '####':
start_time = min_time
if end_time == '####':
end_time = max_time
start_time = int(start_time)
end_time = int(end_time)
if start_time > end_time:
end_time = max_time
years.extend(list(range(start_time, end_time + 1)))
for t in range(start_time, end_time + 1):
triple_lst.append((head, rel, tail, t))
year_freq = Counter(years)
years2id = defaultdict(list)
sum_freq = 0
id = 0
for year in sorted(list(year_freq.keys())):
pdb.set_trace()
sum_freq += year_freq[year]
if sum_freq >= 300:
sum_freq = 0
id += 1
else:
years2id[id].append(year)
return list(set(times)), list(set(entities)), list(set(relations)), train_triples, valid_triples, test_triples
def create_year2id(self, triple_time):
year2id = dict()
freq = ddict(int)
count = 0
year_list = []
for k, v in triple_time.items():
try:
start = v[0].split('-')[0]
end = v[1].split('-')[0]
except:
pdb.set_trace()
if start.find('#') == -1 and len(start) == 4:
year_list.append(int(start))
if end.find('#') == -1 and len(end) == 4:
year_list.append(int(end))
# for k,v in entity_time.items():
# start = v[0].split('-')[0]
# end = v[1].split('-')[0]
# if start.find('#') == -1 and len(start) == 4: year_list.append(int(start))
# if end.find('#') == -1 and len(end) ==4: year_list.append(int(end))
# # if int(start) > int(end):
# # pdb.set_trace()
year_list.sort()
for year in year_list:
freq[year] = freq[year] + 1
# pdb.set_trace()
year_class = []
count = 0
for key in sorted(freq.keys()):
count += freq[key]
if count > 300:
year_class.append(key)
count = 0
prev_year = 0
i = 0
# pdb.set_trace()
for i, yr in enumerate(year_class):
year2id[(prev_year, yr)] = i
prev_year = yr + 1
year2id[(prev_year, max(year_list))] = i + 1
self.year_list = year_list
# pdb.set_trace()
# for k,v in entity_time.items():
# if v[0] == '####-##-##' or v[1] == '####-##-##':
# continue
# if len(v[0].split('-')[0])!=4 or len(v[1].split('-')[0])!=4:
# continue
# start = v[0].split('-')[0]
# end = v[1].split('-')[0]
# for start in start_list:
# if start not in start_year2id:
# start_year2id[start] = count_start
# count_start+=1
# for end in end_list:
# if end not in end_year2id:
# end_year2id[end] = count_end
# count_end+=1
return year2id
if __name__ == '__main__':
args = get_args()
input_dir = os.path.join('raw', args.dataset_dir)
output_dir = os.path.join('interpolation', args.dataset_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
times, entities, relations, train_triples, valid_triples, test_triples = create_ent_rel_to_idx()
times.sort()
# pdb.set_trace()
num_times = len(times)
num_ents = len(entities)
num_rels = len(relations)
write_stats_idx()
time2id = {k: v for v, k in enumerate(times)}
ent2id = {k: v for v, k in enumerate(entities)}
rel2id = {k: v for v, k in enumerate(relations)}
write_processed_files()