-
Notifications
You must be signed in to change notification settings - Fork 9
/
load_data.py
313 lines (294 loc) · 10.8 KB
/
load_data.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
import random
import pickle
from time import time
import sys
from collections import defaultdict
import gensim
import numpy as np
from sklearn.externals import joblib
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score, precision_recall_fscore_support, accuracy_score
from sklearn.model_selection import train_test_split
def dataRead(fname, filter_ids=None):
print "Input File Reading"
fp = open(fname, 'r')
#samples = fp.read().strip().split('\r\n\r\n')
samples = fp.read().strip().split('\n\n')
sent_lengths = [] #1-d array
sent_contents = [] #2-d array [[w1,w2,....] ...]
sent_lables = [] #1-d array
entity1_list = [] #2-d array [[e1,e1_t] [e1,e1_t]...]
entity2_list = [] #2-d array [[e1,e1_t] [e1,e1_t]...]
doc_ids = []
idents = []
for sample in samples:
#sent, entities = sample.strip().split('\r\n')
sent, entities = sample.strip().split('\n')
doc_id, ident, e1, e2, relation = entities.split('\t')
if filter_ids is not None:
if ident not in filter_ids:
continue
sent_contents.append(sent.lower())
entity1_list.append([e1.lower(), ident])
entity2_list.append([e2.lower(), ident])
sent_lables.append(relation)
idents.append(ident)
doc_ids.append(doc_id)
return idents, sent_contents, entity1_list, entity2_list, sent_lables
class LoadData(object):
def __init__(self):
self.word_index = {}
self.pos_index = {}
self.num_words = 1
self.num_pos = 1
self.embs = [np.zeros((300,))]
self.pos = [np.zeros((32,))]
self.wv = gensim.models.Word2Vec.load('/home/amri228/i2b2_2016/ddi/word_vecs2/gensim_model_pubmed')
self.max_u = self.wv.syn0.max()
self.min_u = self.wv.syn0.min()
def fit(self, filename, skip):
all_data = dataRead(filename, skip)
word_cnts = {}
pos_cnts = {}
for ident, tr, tl, e1, e2 in zip(all_data[0], all_data[1],
all_data[-1], all_data[2], all_data[3]):
final_string = tr.split(' ')
e1_pos = None
e2_pos = None
cnt = 0
for w in final_string:
if w == 'proteina':
e1_pos = cnt
elif w == 'proteinb':
e2_pos = cnt
cnt += 1
is_flip = False
if e1_pos > e2_pos:
is_flip = True
tmp_pos = e1_pos
e1_pos = e2_pos
e2_pos = tmp_pos
if e1_pos is None or e2_pos is None:
continue
tmp = []
final_e1_pos = []
final_e2_pos = []
cnt = 0
error = False
for w in final_string:
if cnt-e1_pos in pos_cnts:
pos_cnts[cnt-e1_pos] += 1
else:
pos_cnts[cnt-e1_pos] = 1
if cnt-e2_pos in pos_cnts:
pos_cnts[cnt-e2_pos] += 1
else:
pos_cnts[cnt-e2_pos] = 1
cnt += 1
for w in final_string:
if w in word_cnts:
word_cnts[w] += 1
else:
word_cnts[w] = 1
for w in final_e1_pos:
if w in pos_cnts:
pos_cnts[w] += 1
else:
pos_cnts[w] = 1
for w in final_e2_pos:
if w in pos_cnts:
pos_cnts[w] += 1
else:
pos_cnts[w] = 1
for w, cnt in word_cnts.iteritems():
if cnt > 5:
if w in self.wv:
self.embs.append(self.wv[w])
self.word_index[w] = self.num_words
self.num_words += 1
else:
#self.embs.append(np.random.uniform(self.min_u, self.max_u, (300,)))
self.embs.append(np.random.uniform(-1., 1., (300,)))
self.word_index[w] = self.num_words
self.num_words += 1
for w, cnt in pos_cnts.iteritems():
if cnt > 5:
self.pos.append(np.random.uniform(-1., 1., (32,)))
self.pos_index[w] = self.num_pos
self.num_pos += 1
self.pos_index['NegUNK'] = self.num_pos
self.num_pos += 1
self.pos.append(np.random.uniform(-1., 1., (32,)))
self.pos_index['PosUNK'] = self.num_pos
self.num_pos += 1
self.pos.append(np.random.uniform(-1., 1., (32,)))
self.word_index['UNK'] = self.num_words
#self.embs.append(np.random.uniform(self.min_u, self.max_u, (300,)))
self.embs.append(np.random.uniform(-1., 1., (300,)))
self.num_words += 1
#del self.wv
self.embs = np.array(self.embs, dtype='float32')
self.pos = np.array(self.pos, dtype='float32')
return
def fit_iter(self, filename, skip):
all_data = dataRead(filename, skip)
word_cnts = {}
pos_cnts = {}
for ident, tr, tl, e1, e2 in zip(all_data[0], all_data[1],
all_data[-1], all_data[2], all_data[3]):
final_string = tr.split(' ')
e1_pos = None
e2_pos = None
cnt = 0
for w in final_string:
if w == 'proteina':
e1_pos = cnt
elif w == 'proteinb':
e2_pos = cnt
cnt += 1
is_flip = False
if e1_pos > e2_pos:
is_flip = True
tmp_pos = e1_pos
e1_pos = e2_pos
e2_pos = tmp_pos
tmp = []
final_e1_pos = []
final_e2_pos = []
cnt = 0
error = False
for w in final_string:
if cnt-e1_pos in pos_cnts:
pos_cnts[cnt-e1_pos] += 1
else:
pos_cnts[cnt-e1_pos] = 1
if cnt-e2_pos in pos_cnts:
pos_cnts[cnt-e2_pos] += 1
else:
pos_cnts[cnt-e2_pos] = 1
cnt += 1
for w in final_string:
if w in word_cnts:
word_cnts[w] += 1
else:
word_cnts[w] = 1
for w in final_e1_pos:
if w in pos_cnts:
pos_cnts[w] += 1
else:
pos_cnts[w] = 1
for w in final_e2_pos:
if w in pos_cnts:
pos_cnts[w] += 1
else:
pos_cnts[w] = 1
for w, cnt in word_cnts.iteritems():
if cnt > 5 and w not in self.word_index:
if w in self.wv:
self.embs = np.vstack([self.embs, self.wv[w]])
else:
self.embs = np.vstack([self.embs, np.random.uniform(self.min_u, self.max_u, (300,))])
self.word_index[w] = self.num_words
self.num_words += 1
for w, cnt in pos_cnts.iteritems():
if cnt > 5 and w not in self.pos_index:
self.pos = np.vstack([self.pos, np.random.uniform(-1., 1., (32,))])
self.pos_index[w] = self.num_pos
self.num_pos += 1
return
def transform(self, filename, skip):
all_data = dataRead(filename, skip)
pairs_idx = []
pairs_idx_rev = []
domain_labels = []
pos_e2_idx = []
pos_e1_idx = []
e1_ids = []
e2_ids = []
y = []
idents = []
for ident, tr, tl, e1, e2 in zip(all_data[0], all_data[1],
all_data[-1], all_data[2], all_data[3]):
final_string = tr.split(' ')
e1_pos = None
e2_pos = None
cnt = 0
for w in final_string:
if w == 'proteina':
e1_pos = cnt
elif w == 'proteinb':
e2_pos = cnt
cnt += 1
is_flip = False
if e1_pos is None or e2_pos is None:
continue
if e1_pos > e2_pos:
is_flip = True
tmp_pos = e1_pos
e1_pos = e2_pos
e2_pos = tmp_pos
tmp = []
final_e1_pos = []
final_e2_pos = []
cnt = 0
for w in final_string:
final_e1_pos.append(cnt - e1_pos)
final_e2_pos.append(cnt - e2_pos)
cnt += 1
idents.append(ident)
y.append(tl)
e1_ids.append(e1[0])
e2_ids.append(e2[0])
fstring = []
for w in final_string:
if w == 'proteina' and is_flip:
fstring.append('proteinb')
elif w == 'proteinb' and is_flip:
fstring.append('proteina')
else:
fstring.append(w)
final_string = fstring
str_idx = []
for w in final_string:
if w in self.word_index:
str_idx.append(self.word_index[w])
else:
str_idx.append(self.word_index['UNK'])
pairs_idx.append(str_idx)
e1_idx = []
for p in final_e1_pos:
if p in self.pos_index:
e1_idx.append(self.pos_index[p])
else:
if p < 0:
e1_idx.append(self.pos_index['NegUNK'])
else:
e1_idx.append(self.pos_index['PosUNK'])
pos_e1_idx.append(e1_idx)
e2_idx = []
for p in final_e2_pos:
if p in self.pos_index:
e2_idx.append(self.pos_index[p])
else:
if p < 0:
e2_idx.append(self.pos_index['NegUNK'])
else:
e2_idx.append(self.pos_index['PosUNK'])
pos_e2_idx.append(e2_idx)
lab_lookup = {'True':1, 'False':0}
lab_lookup_rev = {1:'True', 0:'False'}
final_y = np.array([np.int32(lab_lookup[x]) for x in y])
return pairs_idx, pos_e1_idx, pos_e2_idx, final_y, idents, e1_ids, e2_ids
def fit_transform(self, filename, skip):
self.fit(filename, skip)
return self.transform(filename, skip)
def fit_iter_transform(self, filename, skip):
self.fit_iter(filename, skip)
return self.transform(filename, skip)
def pad_data(self, data):
max_len = np.max([len(x) for x in data])
padded_dataset = []
for example in data:
zeros = [0]*(max_len-len(example))
padded_dataset.append(example+zeros)
return np.array(padded_dataset)