-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_pro.py
358 lines (292 loc) · 11.1 KB
/
data_pro.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# -*- coding: utf-8 -*-
import numpy as np
import logging
from collections import Counter
import statistics
from collections import OrderedDict
def relationID2Name(id):
if id == 0:
return "Component-Whole(e2,e1)"
elif id == 1:
return "Other"
elif id == 2:
return "Instrument-Agency(e2,e1)"
elif id == 3:
return "Member-Collection(e1,e2)"
elif id == 4:
return "Cause-Effect(e2,e1)"
elif id == 5:
return "Entity-Destination(e1,e2)"
elif id == 6:
return "Content-Container(e1,e2)"
elif id == 7:
return "Message-Topic(e1,e2)"
elif id == 8:
return "Product-Producer(e2,e1)"
elif id == 9:
return "Member-Collection(e2,e1)"
elif id == 10:
return "Entity-Origin(e1,e2)"
elif id == 11:
return "Cause-Effect(e1,e2)"
elif id == 12:
return "Component-Whole(e1,e2)"
elif id == 13:
return "Message-Topic(e2,e1)"
elif id == 14:
return "Product-Producer(e1,e2)"
elif id == 15:
return "Entity-Origin(e2,e1)"
elif id == 16:
return "Content-Container(e2,e1)"
elif id == 17:
return "Instrument-Agency(e1,e2)"
elif id == 18:
return "Entity-Destination(e2,e1)"
else:
logger.debug('unknown relation id {} !!!!!!!!'.format(id))
return None;
# when_not_use_other, use this function to map other to the last id
def rawID2innerID(id):
if id == 0:
return 0
elif id == 1:
return 18
else:
return id-1
def innerID2rawID(id):
if id == 0:
return 0
elif id == 18:
return 1
else:
return id+1
def outputToSem10rc(ids, path, include_other):
startSentID = 8001
with open(path, 'w') as f:
for id in ids:
if include_other:
f.write('{}\t{}\n'.format(startSentID, relationID2Name(id)))
else:
f.write('{}\t{}\n'.format(startSentID, relationID2Name(innerID2rawID(id))))
startSentID += 1
ENG_PUNC = set(['`','~','!','@','#','$','%','&','*','(',')','-','_','+','=','{',
'}','|','[',']','\\',':',';','\'','"','<','>',',','.','?','/'])
DIGIT = set(['0','1','2','3','4','5','6','7','8','9'])
def normalizeWordList(list, cased):
'''
Normalize a list of words.
alphabet - lower case
digit - 0
punctuation - #
'''
newlist = []
for word in list:
newword = ''
for ch in word:
# if ch in DIGIT:
# newword = newword + '0'
# elif ch in ENG_PUNC:
# newword = newword + '#'
# else:
newword = newword + ch
if not cased:
newword = newword.lower()
newlist.append(newword)
return newlist
def normalizeWord(word, cased):
newword = ''
for ch in word:
# if ch in DIGIT:
# newword = newword + '0'
# elif ch in ENG_PUNC:
# newword = newword + '#'
# else:
newword = newword + ch
if not cased:
newword = newword.lower()
return newword
def load_data(file, cased, use_word_between, include_other):
sentences = []
relations = []
e1_pos = []
e2_pos = []
max_len = 0
len_counter = Counter()
with open(file, 'r', encoding='utf-8', errors='ignore') as f:
for line in f.readlines():
line = line.strip().split()
if include_other:
relations.append(int(line[0]))
else:
relations.append(rawID2innerID(int(line[0])))
word_list = line[5:]
if use_word_between:
begin = int(line[1])
end = int(line[4])
e1_pos.append((0, int(line[2])-begin)) # (start_pos, end_pos)
e2_pos.append((int(line[3])-begin, end-begin)) # (start_pos, end_pos)
sentences.append(normalizeWordList(word_list[begin:end+1], cased))
sentence_length = end+1-begin
else:
e1_pos.append((int(line[1]), int(line[2]))) # (start_pos, end_pos)
e2_pos.append((int(line[3]), int(line[4]))) # (start_pos, end_pos)
sentences.append(normalizeWordList(word_list, cased))
sentence_length = len(word_list)
if sentence_length > max_len:
max_len = sentence_length
if sentence_length not in len_counter:
len_counter[sentence_length] = 1
else:
len_counter[sentence_length] += 1
print("max sentence length %d" % (max_len))
# print(len_counter.most_common())
return sentences, relations, e1_pos, e2_pos, max_len
# def build_dict(sentences):
# word_count = Counter()
# for sent in sentences:
# for w in sent:
# if w not in word_count:
# word_count[w] = 1
# else:
# word_count[w] += 1
# # 按照词频降序返回 [('棒', 2), ('的', 1), ('青', 1), ('年', 1)]
# ls = word_count.most_common()
#
# word_dict = {w[0]: index + 1 for (index, w) in enumerate(ls)}
# # leave 0 to PAD
# return word_dict
def create_alphabet(sentences):
word_alpha = set()
for sent in sentences:
for w in sent:
word_alpha.add(w)
return word_alpha
def build_fixed_dict(alpha):
word_dict = OrderedDict()
index = 1 # leave 0 to PAD
for w in alpha:
word_dict[w] = index
index += 1
return word_dict
# def build_fixed_dict(sentences):
# word_dict = OrderedDict()
# index = 1 # leave 0 to PAD
# for sent in sentences:
# for w in sent:
# if w not in word_dict:
# word_dict[w] = index
# index += 1
#
# return word_dict
def build_position_dict(max_len):
'''
If max_len is 40, the position will be between [-39, 39].
And we use 40 as the position of PAD.
'''
position_dict = {w: index for (index, w) in enumerate(range(-(max_len-1), max_len+1))}
return position_dict
def numpyNormalize(Z):
Zmax,Zmin = Z.max(),Z.min()
Z = (Z - Zmin)/(Zmax - Zmin)
return Z
def load_embedding_from_glove(emb_file, word_dict, cased):
vocab = {}
with open(emb_file, 'r') as f:
while 1:
line = f.readline()
if not line:
break;
templist = line.strip().split()
# norm the value into [0,1] and map into [-0.01, 0.01]
# vector = np.array(list(map(float, templist[1:])))
# vector = (numpyNormalize(vector)*0.02-0.01)
# vector = vector/np.linalg.norm(vector)
# vocab[templist[0]] = vector
try:
vocab[normalizeWord(templist[0], cased)] = list(map(float, templist[1:]))
except ValueError:
continue
# print('##############', vocab['has'])
dim = len(vocab['the']) # assume 'the' exists
# num_words = len(word_dict) + 1
num_words = len(word_dict)
embeddings = np.random.uniform(-0.01, 0.01, size=(num_words, dim))
# embeddings = np.random.normal(0, 1, size=(num_words, dim))
pre_trained = 0
for w in vocab.keys():
if w in word_dict:
embeddings[word_dict[w]-1] = np.array(vocab[w])
pre_trained += 1
# embeddings[0] = np.zeros(dim)
logging.info(
'embedding dimension %d' % (dim))
logging.info(
'embeddings: %.2f%%(pre_trained) total: %d' % (pre_trained / num_words * 100, num_words))
return embeddings.astype(np.float32)
def pos(x, max):
'''
map the relative distance between [0, max)
max should be odd
'''
half = max//2 - 1
if x < -half:
return 0
if x >= -half and x <= half:
return x + half + 1
if x > half:
return max-1
def vectorize(data, word_dict, max_len, position_dict):
sentences, relations, e1_pos, e2_pos, _ = data
# replace word with word-id
e1_vec = []
e2_vec = []
# compute relative distance
dist1 = []
dist2 = []
POSTION_PAD_ID = len(position_dict)-1 # see build_position_dict
num_data = len(sentences)
sents_vec = np.zeros((num_data, max_len), dtype=int)
logging.debug('data shape: (%d, %d)' % (num_data, max_len))
for idx, (sent, pos1, pos2) in enumerate(zip(sentences, e1_pos, e2_pos)):
vec = [word_dict[w] if w in word_dict else 0 for w in sent]
position_e1 = [ idx-pos1[1] for idx, _ in enumerate(sent)]
vec_position_e1 = [position_dict[pos] for pos in position_e1]
pad_vec_position_e1 = [POSTION_PAD_ID]*max_len # see build_position_dict
position_e2 = [ idx-pos2[1] for idx, _ in enumerate(sent)]
vec_position_e2 = [position_dict[pos] for pos in position_e2]
pad_vec_position_e2 = [POSTION_PAD_ID]*max_len
# pad in the rear, sent in the front
if max_len >= len(vec):
sents_vec[idx, 0:len(vec)] = vec
pad_vec_position_e1[0:len(vec)] = vec_position_e1
pad_vec_position_e2[0:len(vec)] = vec_position_e2
else:
sents_vec[idx, 0:max_len] = vec[0:max_len]
pad_vec_position_e1[0:max_len] = vec_position_e1[0:max_len]
pad_vec_position_e2[0:max_len] = vec_position_e2[0:max_len]
# pad in the front, sent in the rear
# if max_len >= len(vec):
# sents_vec[idx, max_len-len(vec):] = vec
# pad_vec_position_e1[max_len-len(vec):] = vec_position_e1
# pad_vec_position_e2[max_len-len(vec):] = vec_position_e2
# else:
# sents_vec[idx, 0:max_len] = vec[0:max_len]
# pad_vec_position_e1[0:max_len] = vec_position_e1[0:max_len]
# pad_vec_position_e2[0:max_len] = vec_position_e2[0:max_len]
# last word of e1 and e2
e1_vec.append(vec[pos1[1]])
e2_vec.append(vec[pos2[1]])
dist1.append(pad_vec_position_e1)
dist2.append(pad_vec_position_e2)
# for sent, p1, p2 in zip(sents_vec, e1_pos, e2_pos):
# # current word position - last word position of e1 or e2
# dist1.append([pos(p1[1] - idx, max_len) for idx, _ in enumerate(sent)])
# dist2.append([pos(p2[1] - idx, max_len) for idx, _ in enumerate(sent)])
# sents_vec - map each word of sentence to their word id
# relations - relation class of sentence
# e1_vec - map last word of entity 1 to its word id
# e2_vec - map last word of entity 1 to its word id
# dist1 - map each word position of entity 1 to it pos id
# dist2 - map each word position of entity 2 to it pos id
return sents_vec, relations, e1_vec, e2_vec, dist1, dist2