-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_data.py
294 lines (255 loc) · 11.4 KB
/
preprocess_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
import random
import xml.etree.ElementTree as ET
import pickle
import os
###################3
#调试用
##################3
# convert xml file to txt file
# read data from xml file
def read_ppi(filename):
data = []
parser = ET.XMLParser(encoding="UTF-8") # etree.XMLParser(recover=True)
tree = ET.parse(filename, parser=parser)
root = tree.getroot()
for document in root:
for sentence in document:
sentence_context = sentence.attrib['text']
sentence_id = sentence.attrib['id']
entity_dict = {}
pos_interaction_list = []
neg_interaction_list = []
e_count = 0
e_pair_flag = {}
e_prefix = sentence_id + '.e'
for item in sentence:
if item.tag == 'entity':
e_type = item.attrib['type']
e_id = item.attrib['id']
e_ch_offset = item.attrib['charOffset']
e_text = item.attrib['text']
entity_dict[e_id] = [e_text, e_type, e_ch_offset]
e_pair_flag[e_count] = []
e_count += 1
elif item.tag == 'interaction':
e1 = item.attrib['e1']
entity1 = entity_dict[e1]
e2 = item.attrib['e2']
entity2 = entity_dict[e2]
i_type = item.attrib['type']
pos_interaction_list.append([entity1, entity2, i_type])
e1_int = int(e1[e1.rfind('e') + 1:])
e2_int = int(e2[e2.rfind('e') + 1:])
e_pair_flag[e1_int].append(e2_int)
e_pair_flag[e2_int].append(e1_int)
# add negative instance into list
for i in range(e_count):
for j in range(e_count):
if j in e_pair_flag[i]:
entity1 = entity_dict[e_prefix + str(i)]
entity2 = entity_dict[e_prefix + str(j)]
i_type = "false"
neg_interaction_list.append([entity1, entity2, i_type])
# if len(pos_interaction_list):
# print("neg_interaction_list", neg_interaction_list[0])
# print("pos_interaction_list", pos_interaction_list[0])
data.append([sentence_id, sentence_context, pos_interaction_list, neg_interaction_list])
return data
# data: [sent_id, sent_text, pair_list]
# pair_list: [entity1, entity2, ddi]
# ddi: true/false
def read_ddi(dir):
data = []
num_pair = 0
print(dir)
file_list = os.listdir(dir)
for fname in file_list:
parser = ET.XMLParser(encoding="UTF-8") # etree.XMLParser(recover=True)
tree = ET.parse(dir + '/' + fname, parser=parser)
root = tree.getroot()
for sent in root:
sent_id = sent.attrib['id']
sent_text = sent.attrib['text'].strip()
ent_dict = {}
pair_list = []
for c in sent:
# obtain entities' information
if c.tag == 'entity':
d_type = c.attrib['type']
d_id = c.attrib['id']
d_ch_of = c.attrib['charOffset']
d_text = c.attrib['text']
ent_dict[d_id] = [d_text, d_type, d_ch_of]
# obtain entity pairs' information
elif c.tag == 'pair':
p_id = c.attrib['id']
e1 = c.attrib['e1']
entity1 = ent_dict[e1]
e2 = c.attrib['e2']
entity2 = ent_dict[e2]
ddi = c.attrib['ddi']
# if ddi == 'true':
# if 'type' in c.attrib:
# ddi = c.attrib['type']
# else:
# ddi = 'int'
pair_list.append([entity1, entity2, ddi])
num_pair = num_pair + 1
data.append([sent_id, sent_text, pair_list])
print("num_pair:", num_pair)
return data
def write(filename, data):
with open(filename, mode='wb') as fw:
pickle.dump(data, fw, 0)
def replace_ddi(data):
count = 0
replaced_data = []
for s in data:
sent = s[1]
pair = s[2]
e_dict = []
for p in pair:
d1, d1_type, d1_offset = p[0]
d2, d2_type, d2_offset = p[1]
if d1 not in e_dict:
e_dict.append(d1_offset)
if d2 not in e_dict:
e_dict.append(d2_offset)
for p in pair:
d1, d1_type, d1_offset = p[0]
d2, d2_type, d2_offset = p[1]
ddi = p[2]
if d1 == d2:
continue
count += 1
if d1_offset.find(';') == -1 and d2_offset.find(';') == -1:
d1_start, d1_end = d1_offset.split('-')
d2_start, d2_end = d2_offset.split('-')
d1_start, d1_end = int(d1_start), int(d1_end)
d2_start, d2_end = int(d2_start), int(d2_end)
elif d1_offset.find(';') > -1 and d2_offset.find(';') > -1:
d1_start, d1_end = d1_offset.split(';')[0].split('-')
d2_start, d2_end = d2_offset.split(';')[0].split('-')
d1_start, d1_end = int(d1_start), int(d1_end)
d2_start, d2_end = int(d2_start), int(d2_end)
elif d1_offset.find(';') > -1 and d2_offset.find(';') == -1:
d1_1, d1_2 = d1_offset.split(';')
d1_1_start, d1_1_end = d1_1.split('-')
d1_2_start, d1_2_end = d1_2.split('-')
d1_1_start, d1_1_end = int(d1_1_start), int(d1_1_end)
d1_2_start, d1_2_end = int(d1_2_start), int(d1_2_end)
d2_start, d2_end = d2_offset.split('-')
d2_start, d2_end = int(d2_start), int(d2_end)
if len(set(range(d1_1_start, d1_1_end)) & set(range(d2_start, d2_end))):
d1_start, d1_end = d1_2_start, d1_2_end
else:
d1_start, d1_end = d1_1_start, d1_1_end
else:
d2_1, d2_2 = d2_offset.split(';')
d2_1_start, d2_1_end = d2_1.split('-')
d2_2_start, d2_2_end = d2_2.split('-')
d2_1_start, d2_1_end = int(d2_1_start), int(d2_1_end)
d2_2_start, d2_2_end = int(d2_2_start), int(d2_2_end)
d1_start, d1_end = d1_offset.split('-')
d1_start, d1_end = int(d1_start), int(d1_end)
if len(set(range(d2_1_start, d2_1_end)) & set(range(d1_start, d1_end))):
d2_start, d2_end = d2_2_start, d2_2_end
else:
d2_start, d2_end = d2_1_start, d2_1_end
other = set(e_dict) - set([d1_offset, d2_offset])
replaced_sent = sent.replace(sent[d1_start:d1_end+1], 'DRUGA').replace(sent[d2_start:d2_end+1], 'DRUGB')
for n in other:
if n.find(';') > -1:
n = n.split(';')[0]
n_start, n_end = n.split('-')
n_start, n_end = int(n_start), int(n_end)
replaced_sent = replaced_sent.replace(sent[n_start:n_end+1], 'DRUGN')
# print(replaced_sent)
# print(sent)
replaced_data.append([replaced_sent, d1, d1_type, d2, d2_type, ddi])
print(replaced_data)
return replaced_data
def replace_ddi_triplet(data):
count = 0
replaced_data = []
for s in data:
sent = s[1]
pair = s[2]
e_dict = []
homo_negs = []
for p in pair:
d1, d1_type, d1_offset = p[0]
d2, d2_type, d2_offset = p[1]
if d1 not in e_dict:
e_dict.append(d1_offset)
if d2 not in e_dict:
e_dict.append(d2_offset)
if ddi == 'false':
homo_negs.append(p)
for p in pair:
d1, d1_type, d1_offset = p[0]
d2, d2_type, d2_offset = p[1]
ddi = p[2]
if d1 == d2:
continue
count += 1
if ddi == 'true' and len(homo_negs)>0:
index = random.randint(0, len(homo_negs) - 1)
homo_neg = homo_negs[index]
if d1_offset.find(';') == -1 and d2_offset.find(';') == -1:
d1_start, d1_end = d1_offset.split('-')
d2_start, d2_end = d2_offset.split('-')
d1_start, d1_end = int(d1_start), int(d1_end)
d2_start, d2_end = int(d2_start), int(d2_end)
elif d1_offset.find(';') > -1 and d2_offset.find(';') > -1:
d1_start, d1_end = d1_offset.split(';')[0].split('-')
d2_start, d2_end = d2_offset.split(';')[0].split('-')
d1_start, d1_end = int(d1_start), int(d1_end)
d2_start, d2_end = int(d2_start), int(d2_end)
elif d1_offset.find(';') > -1 and d2_offset.find(';') == -1:
d1_1, d1_2 = d1_offset.split(';')
d1_1_start, d1_1_end = d1_1.split('-')
d1_2_start, d1_2_end = d1_2.split('-')
d1_1_start, d1_1_end = int(d1_1_start), int(d1_1_end)
d1_2_start, d1_2_end = int(d1_2_start), int(d1_2_end)
d2_start, d2_end = d2_offset.split('-')
d2_start, d2_end = int(d2_start), int(d2_end)
if len(set(range(d1_1_start, d1_1_end)) & set(range(d2_start, d2_end))):
d1_start, d1_end = d1_2_start, d1_2_end
else:
d1_start, d1_end = d1_1_start, d1_1_end
else:
d2_1, d2_2 = d2_offset.split(';')
d2_1_start, d2_1_end = d2_1.split('-')
d2_2_start, d2_2_end = d2_2.split('-')
d2_1_start, d2_1_end = int(d2_1_start), int(d2_1_end)
d2_2_start, d2_2_end = int(d2_2_start), int(d2_2_end)
d1_start, d1_end = d1_offset.split('-')
d1_start, d1_end = int(d1_start), int(d1_end)
if len(set(range(d2_1_start, d2_1_end)) & set(range(d1_start, d1_end))):
d2_start, d2_end = d2_2_start, d2_2_end
else:
d2_start, d2_end = d2_1_start, d2_1_end
other = set(e_dict) - set([d1_offset, d2_offset])
replaced_sent = sent.replace(sent[d1_start:d1_end + 1], 'DRUGA').replace(sent[d2_start:d2_end + 1],
'DRUGB')
for n in other:
if n.find(';') > -1:
n = n.split(';')[0]
n_start, n_end = n.split('-')
n_start, n_end = int(n_start), int(n_end)
replaced_sent = replaced_sent.replace(sent[n_start:n_end + 1], 'DRUGN')
# print(replaced_sent)
# print(sent)
replaced_data.append([replaced_sent, d1, d1_type, d2, d2_type, ddi])
print(replaced_data)
return replaced_data
# data_xml = "data/xml/bioinfer-1.2.0b-unified-format.xml"
# step1_train_data = read(data_xml)
# # write("data/step1/train_data.txt", step1_train_data)
# pickle.dump(step1_train_data, open('data/step1/train_data.txt', 'wb'))
# data = pickle.load(open('data/step1/train_data.txt', 'rb'))
# print(data)
corpora = "ddi_data/xml/Train"
tr_data = read_ddi(corpora)
step2_tr_data = replace_ddi(tr_data)