-
Notifications
You must be signed in to change notification settings - Fork 10
/
preprocessing.py
265 lines (241 loc) · 9.97 KB
/
preprocessing.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
import numpy as np
import random
import pickle
def makeTup(Path):
sentenceTuple = dict()
wordset = set()
wordsetTD = set()
charset = set()
charset.add('')
sentenceTuple['train_dev']=list()
for filename in Path:
key = filename[:filename.find('setPath')]
sentenceTuple[key] = list()
with open(Path[filename], 'rb') as file_:
templist = list()
for line in file_:
line = line.strip()
if line != '':
temp = line.split('\t')
if 'B-' in temp[1]:
label = 2
elif 'I-' in temp[1]:
label = 3
elif 'E-' in temp[1]:
label = 4
elif 'S-' in temp[1]:
label = 1
else:
label = 0
templist.append((temp[0],label))
wordset.add(temp[0])
if key != 'test':
wordsetTD.add(temp[0])
for c in temp[0]:
charset.add(c)
elif line == '':
sentenceTuple[key].append(templist)
if key != 'test':
sentenceTuple['train_dev'].append(templist)
templist = list()
return sentenceTuple, wordset, wordsetTD, charset
def makeIDTup(name, sentenceTuple, wordset, wordsetTD, charset, word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char):
idxcounter = len(word2ID)
idx = 0
for word in wordset:
if word not in word2ID:
word2ID[word] = idxcounter + idx
ID2word[idxcounter+idx] = word
idx+=1
idxcounter = len(word2IDTD)
idx = 0
for word in wordsetTD:
if word not in word2IDTD:
word2IDTD[word] = idxcounter + idx
ID2wordTD[idxcounter+idx] = word
idx+=1
idxcounter = len(char2ID)
idx = 0
for char in charset:
if char not in char2ID:
char2ID[char] = idxcounter + idx
ID2char[idxcounter+idx] = char
idx+=1
sentenceTupleID = dict()
sentenceTupleLen = dict()
for key in sentenceTuple:
sentenceTupleID[key] = list()
sentenceTupleLen[key] = list()
for sent in sentenceTuple[key]:
sentenceTupleLen[key].append(len(sent))
tupleID = list()
for tup in sent:
tempTup = (word2ID[tup[0]],[char2ID[_] for _ in tup[0]], tup[1])
tupleID.append(tempTup)
sentenceTupleID[key].append(tupleID)
pickle.dump(sentenceTupleID, open('data/'+name+'_sentenceTupleID.pickle','wb'))
pickle.dump(sentenceTupleLen, open('data/'+name+'_sentenceTupleLen.pickle','wb'))
return word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char
Path=dict()
word2ID = dict()
ID2word = dict()
word2IDTD = dict()
ID2wordTD = dict()
char2ID = dict()
ID2char = dict()
#NCBI
Path['devsetPath'] = '../MTL-Bioinformatics-2016/data/NCBI-disease-IOBES/devel.tsv'
Path['testsetPath'] = '../MTL-Bioinformatics-2016/data/NCBI-disease-IOBES/test.tsv'
Path['trainsetPath'] ='../MTL-Bioinformatics-2016/data/NCBI-disease-IOBES/train.tsv'
sentenceTuple, wordset, wordsetTD, charset = makeTup(Path)
word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char = makeIDTup('NCBI',sentenceTuple, wordset, wordsetTD, charset, word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char)
#BC2GM
Path['devsetPath'] = '../MTL-Bioinformatics-2016/data/BC2GM-IOBES/devel.tsv'
Path['testsetPath'] = '../MTL-Bioinformatics-2016/data/BC2GM-IOBES/test.tsv'
Path['trainsetPath'] ='../MTL-Bioinformatics-2016/data/BC2GM-IOBES/train.tsv'
sentenceTuple, wordset, wordsetTD, charset = makeTup(Path)
word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char = makeIDTup('BC2GM',sentenceTuple, wordset, wordsetTD, charset, word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char)
#BC4CHEMD
Path['devsetPath'] = '../MTL-Bioinformatics-2016/data/BC4CHEMD-IOBES/devel.tsv'
Path['testsetPath'] = '../MTL-Bioinformatics-2016/data/BC4CHEMD-IOBES/test.tsv'
Path['trainsetPath'] ='../MTL-Bioinformatics-2016/data/BC4CHEMD-IOBES/train.tsv'
sentenceTuple, wordset, wordsetTD, charset = makeTup(Path)
word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char = makeIDTup('BC4CHEMD',sentenceTuple, wordset, wordsetTD, charset, word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char)
#BC5CDR-disease
Path['devsetPath'] = '../MTL-Bioinformatics-2016/data/BC5CDR-disease-IOBES/devel.tsv'
Path['testsetPath'] = '../MTL-Bioinformatics-2016/data/BC5CDR-disease-IOBES/test.tsv'
Path['trainsetPath'] ='../MTL-Bioinformatics-2016/data/BC5CDR-disease-IOBES/train.tsv'
sentenceTuple, wordset, wordsetTD, charset = makeTup(Path)
word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char = makeIDTup('BC5CDR-disease',sentenceTuple, wordset, wordsetTD, charset, word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char)
#BC5CDR-chem
Path['devsetPath'] = '../MTL-Bioinformatics-2016/data/BC5CDR-chem-IOBES/devel.tsv'
Path['testsetPath'] = '../MTL-Bioinformatics-2016/data/BC5CDR-chem-IOBES/test.tsv'
Path['trainsetPath'] ='../MTL-Bioinformatics-2016/data/BC5CDR-chem-IOBES/train.tsv'
sentenceTuple, wordset, wordsetTD, charset = makeTup(Path)
word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char = makeIDTup('BC5CDR-chem',sentenceTuple, wordset, wordsetTD, charset, word2ID, ID2word, word2IDTD, ID2wordTD, char2ID, ID2char)
#JNLPBA
Path['testsetPath'] = '../MTL-Bioinformatics-2016/data/JNLPBA/original-data/test/Genia4EReval1.iob2'
Path['trainsetPath'] ='../MTL-Bioinformatics-2016/data/JNLPBA/original-data/train/Genia4ERtask1.iob2'
temp_sentenceTuple = dict()
sentenceTuple = dict()
wordset = set()
wordsetTD = set()
charset = set()
for filename in Path:
key = filename[:filename.find('setPath')]
temp_sentenceTuple[key] = list()
with open(Path[filename], 'rb') as file_:
templist = list()
for line in file_:
line = line.strip()
if line != '':
temp = line.split('\t')
if temp[0] == '':
print(line)
continue
if temp[1] == 'O':
tag = 0
elif ('B-' in temp[1]) or ('b-' in temp[1]):
if 'cell_' in temp[1]:
tag = 0
else:
tag = 1
elif ('I-' in temp[1]) or ('i-' in temp[1]):
if 'cell_' in temp[1]:
tag = 0
else:
tag = 2
templist.append((temp[0],tag))
wordset.add(temp[0])
if key != 'test':
wordsetTD.add(temp[0])
for c in temp[0]:
charset.add(c)
elif line == '':
temp_sentenceTuple[key].append(templist)
templist = list()
sentenceTuple[key] = list()
for sent in temp_sentenceTuple[key]:
temp = list()
for idx, label in enumerate(sent):
if idx == len(sent)-1:
if label[1] == 2 : #L
tag = 4
else:
tag = label[1]
elif label[1] == 1 and sent[idx+1][1] == 2: #B
tag = 2
elif label[1] == 2 and (sent[idx+1][1] == 0 or sent[idx+1][1]== 1): #L
tag = 4
elif label[1] == 2 and sent[idx+1][1] == 2: #I
tag = 3
else:
tag = label[1]
if tag == 2:
print(sent, idx)
temp.append((label[0],tag))
sentenceTuple[key].append(temp)
idxcounter = len(word2ID)
idx = 0
for word in wordset:
if word not in word2ID:
word2ID[word] = idxcounter+idx
ID2word[idxcounter+idx] = word
idx+=1
idxcounter = len(word2IDTD)
idx = 0
for word in wordsetTD:
if word not in word2IDTD:
word2IDTD[word] = idxcounter+idx
ID2wordTD[idxcounter+idx] = word
idx+=1
idxcounter = len(char2ID)
idx = 0
for char in charset:
if char not in char2ID:
char2ID[char] = idxcounter+idx
ID2char[idxcounter+idx] = char
idx+=1
sentenceTupleID = dict()
sentenceTupleLen = dict()
for key in sentenceTuple:
sentenceTupleID[key] = list()
sentenceTupleLen[key] = list()
for sent in sentenceTuple[key]:
sentenceTupleLen[key].append(len(sent))
tupleID = list()
for tup in sent:
charlist = list()
tempTup = (word2ID[tup[0]],[char2ID[_] for _ in tup[0]], tup[1])
tupleID.append(tempTup)
sentenceTupleID[key].append(tupleID)
dev_idx = random.sample(range(0,len(sentenceTupleLen['train'])),len(sentenceTupleLen['test']))
sentenceTupleID['dev'] = list()
sentenceTupleLen['dev'] = list()
for idx in dev_idx:
sentenceTupleID['dev'].append(sentenceTupleID['train'][idx])
sentenceTupleLen['dev'].append(sentenceTupleLen['train'][idx])
tempID = list()
tempLen = list()
for i in range(len(sentenceTupleLen['train'])):
if i in dev_idx:
pass
else:
tempID.append(sentenceTupleID['train'][i])
tempLen.append(sentenceTupleLen['train'][i])
sentenceTupleLen['train_dev'] = sentenceTupleLen['train'][:]
sentenceTupleID['train_dev'] = sentenceTupleID['train'][:]
sentenceTupleLen['train'] = tempLen[:]
sentenceTupleID['train'] = tempID[:]
pickle.dump(sentenceTupleID, open('data/JNLPBA_sentenceTupleID.pickle','wb'))
pickle.dump(sentenceTupleLen, open('data/JNLPBA_sentenceTupleLen.pickle','wb'))
word2ID['NUM'] = 107551
ID2word[107551] = 'NUM'
word2ID['<UNK>'] = 107552
ID2word[107552] = '<UNK>'
pickle.dump(char2ID, open('data/char2ID.pickle','wb'))
pickle.dump(word2ID, open('data/word2ID.pickle','wb'))
pickle.dump(ID2char, open('data/ID2char.pickle','wb'))
pickle.dump(ID2word, open('data/ID2word.pickle','wb'))
pickle.dump(word2IDTD, open('data/word2IDTD.pickle','wb'))
pickle.dump(ID2wordTD, open('data/ID2wordTD.pickle','wb'))