forked from tyliupku/soft-label-RE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
307 lines (266 loc) · 8.41 KB
/
util.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import re
import numpy as np
import time, os, sys, shutil
# =================== word vector =================== #
def get_lower_sentence(sentence):
sentence = sentence.strip()
sentence = sentence.lower()
return sentence
# load the same pre-trained vector from https://github.com/thunlp/NRE/tree/master/data
def word2vec():
fvec=open("data/vector1.txt", "r")
vec={}
for line in fvec:
line=line.strip()
line=line.split('\t')
vec[line[0]]=line[1:51]
fvec.close()
return vec
def get_word_vec(vectors, one_or_att="att"):
"""
:param vectors:
:return: word_vocab: word -> id
word_vectors: numpy
"""
sorted_vectors = sorted(vectors.items(), key=lambda d:d[0])
word_vocab = {}
word_vectors = []
for it in sorted_vectors:
word_vocab[it[0]] = len(word_vocab)
word_vectors.append(it[1])
assert len(word_vocab)==len(word_vectors)
word_vocab['UNK'] = len(word_vocab)
# word_vocab['BLANK'] = len(word_vocab)
word_vectors.append(np.random.normal(size=50, loc=0, scale=0.05))
if one_or_att == "att":
word_vocab['BLANK'] = len(word_vocab)
word_vectors.append(np.random.normal(size=50, loc=0, scale=0.05))
# word_vectors.append(np.random.normal(size=50, loc=0, scale=0.05))
word_vectors = np.array(word_vectors, dtype=float)
return word_vocab, word_vectors
def get_sentence_seq(sentence, word_vocab):
vec = []
words = sentence.split()
for word in words:
try:
id = word_vocab[word]
except:
id = word_vocab["UNK"]
vec.append(id)
return vec
# =================== load data =================== #
def get_data(istrain=True, word_vocab=None):
frel = open("data/RE/relation2id.txt")
rel2id = {}
for line in frel:
line = line.strip()
it = line.split()
rel2id[it[0]] = it[1]
if istrain:
file = open("data/RE/train.txt")
else:
file = open("data/RE/test.txt")
sen_id = []
real_sen = []
lpos = []
rpos = []
namepos = []
bag_id = {}
midrel = {}
cnt = 0
for line in file:
line = line.strip()
line = line[:-10].strip()
it = line.split('\t')
mid1, mid2, rel, sen = it[0], it[1], it[-2], get_lower_sentence(it[-1])
if rel not in rel2id:
continue
rel = rel2id[rel]
if len(sen.split()) > 200:
continue
name1, name2 = it[2], it[3]
en1, en2, wps_left, wps_right = get_position(sen, name1, name2)
if en1 == 0 or en2 == 0:
cnt += 1
continue
name_set = key_position(sen, en1, en2)
if istrain:
key = mid1 + '\t' + mid2 + '\t' + rel
else:
key = mid1 + '\t' + mid2
key1 = mid1 + '\t' + mid2 + '\t' + rel
if rel != "0":
midrel[key1] = 1
if key not in bag_id:
bag_id[key] = []
bag_id[key].append(len(sen_id))
lpos.append(wps_left.tolist())
rpos.append(wps_right.tolist())
sen_id.append(get_sentence_seq(sen, word_vocab))
real_sen.append(sen)
namepos.append(name_set)
if istrain:
return bag_id, sen_id, lpos, rpos, real_sen, namepos
else:
return bag_id, sen_id, midrel, lpos, rpos, real_sen, namepos
def key_position(sen, en1, en2):
sen_len = len(sen.split())
en = [en1, en2]
ean = []
for eni in en:
eans = 0
if eni == 1:
eans = 0
elif eni == sen_len:
eans = eni - 3
else:
eans = eni - 2
ean.append(eans)
return ean
def get_position(sen, name1, name2):
sentence = sen.split()
llen = len(sentence)
wps_left = np.array(range(1, llen + 1))
wps_right = np.array(range(1, llen + 1))
en1, en2 = 0, 0
for i, it in enumerate(sentence):
if it == name1:
en1 = i + 1
if it == name2:
en2 = i + 1
wps_left -= int(en1)
wps_right -= int(en2)
wps_left = np.minimum(np.maximum(wps_left, -30), 30) + 30
wps_right = np.minimum(np.maximum(wps_right, -30), 30) + 30
return en1, en2, wps_left, wps_right
def batch_iter(data, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a dataset.
"""
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int((len(data)-1)/batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
# =================== evaluation =================== #
def evaluate(path, triple_list, midrel, epoch):
frel = open("data/RE/relation2id.txt")
fmid = open("data/RE/entity2name.txt")
rel2id = {}
id2rel = {}
mid2name = {}
triple = []
for line in frel:
line = line.strip()
it = line.split()
rel2id[it[0]] = it[1]
id2rel[int(it[1])] = it[0]
for line in fmid:
line = line.strip()
it = line.split('\t')
mid2name[it[0]] = it[1]
for item in triple_list:
mid = item["mid"].split('\t')
rel = item["rel"]
score = item["score"]
ent1, ent2 = mid2name[mid[0]], mid2name[mid[1]]
rname = id2rel[rel]
key = ent1 + '\t' + ent2 + '\t' + rname
mid_key = mid[0] + '\t' + mid[1] + '\t' + str(rel)
crt = "0"
if mid_key in midrel:
crt = "1"
triple.append({"triple":key, "val":score, "crt":crt})
sorted_triple = sorted(triple, key=lambda x: x["val"])
prfile = open(path, "w")
correct = 0
tot_recall = len(midrel.keys())
for i, item in enumerate(sorted_triple[::-1]):
if str(item["crt"]) == "1":
correct += 1
prfile.write("{0:.5f}\t{1:.5f}\t{2:.5f}\t".format(float(correct)/(i+1), float(correct)/tot_recall, float(item["val"])))
prfile.write(str(item["triple"])+'\n')
if i+1 > 2000:
break
prfile.close()
# Progress bar
TOTAL_BAR_LENGTH = 100.
last_time = time.time()
begin_time = last_time
# print os.popen('stty size', 'r').read()
_, term_width = os.popen('stty size', 'r').read().split()
term_width = int(term_width)
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def format_time(seconds):
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f