-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
211 lines (198 loc) · 7.41 KB
/
preprocess.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
import pathlib
from pathlib import Path
import numpy as np
from pytorch_pretrained_bert import BertTokenizer
def read_data(directory):
ids = []
texts = []
labels = []
for f in directory.glob('*.txt'):
id = f.name.replace('article', '').replace('.txt','')
ids.append(id)
in_f = open(f, encoding='utf-8')
texts.append(in_f.read())
labels.append(parse_label(f.as_posix().replace('.txt', '.labels.tsv')))
in_f.close()
# labels can be empty
return ids, texts, labels
def parse_label(label_path):
labels = []
f= Path(label_path)
if not f.exists():
return labels
for line in open(label_path):
parts = line.strip().split('\t')
labels.append([int(parts[2]), int(parts[3]), parts[1], 0, 0])
labels = sorted(labels)
if labels:
length = max([label[1] for label in labels])
visit = np.zeros(length)
res = []
for label in labels:
if sum(visit[label[0]:label[1]]):
label[3] = 1
else:
visit[label[0]:label[1]] = 1
res.append(label)
return res
else:
return labels
def clean_text(articles, ids):
texts = []
for article, id in zip(articles, ids):
sentences = article.split('\n')
start = 0
end = -1
res = []
for sentence in sentences:
start = end + 1
end = start + len(sentence) # length of sequence
if sentence != "": # if not empty line
res.append([id, sentence, start, end])
texts.append(res)
return texts
def make_dataset(directory):
ids, texts, labels = read_data(directory)
texts = clean_text(texts, ids)
res = []
for text, label in zip(texts, labels):
# making positive examples
tmp = []
pos_ind = [0] * len(text)
for l in label:
for i, sen in enumerate(text):
if l[0] >= sen[2] and l[0] < sen[3] and l[1] > sen[3]:
l[4] = 1
tmp.append(sen + [l[0], sen[3], l[2], l[3], l[4]])
pos_ind[i] = 1
l[0] = sen[3] + 1
elif l[0] != l[1] and l[0] >= sen[2] and l[0] < sen[3] and l[1] <= sen[3]:
tmp.append(sen + l)
pos_ind[i] = 1
# making negative examples
dummy = [0, 0, 'O', 0, 0]
for k, sen in enumerate(text):
if pos_ind[k] != 1:
tmp.append(sen+dummy)
res.append(tmp)
return res
def make_bert_testset(dataset):
words, tags, ids= [], [], []
for article in dataset:
tmp_doc, tmp_label, tmp_id = [], [], []
tmp_sen = article[0][1]
tmp_i = article[0][0]
label = ['O'] * len(tmp_sen.split(' '))
for sentence in article:
tokens = sentence[1].split(' ')
token_len = [len(token) for token in tokens]
if len(sentence) == 9: # label exists
if tmp_sen != sentence[1]:
tmp_label.append(label)
tmp_doc.append(tmp_sen.split(' '))
tmp_id.append(tmp_i)
label = ['O'] * len(token_len)
start = sentence[4] - sentence[2]
end = sentence[5] - sentence[2]
if sentence[6] != 'O':
for i in range(1, len(token_len)):
token_len[i] += token_len[i-1] + 1
token_len[-1] += 1
token_len = np.asarray(token_len)
s_ind = np.min(np.where(token_len > start))
tmp = np.where(token_len >= end)
if len(tmp[0]) != 0:
e_ind = np.min(tmp)
else:
e_ind = s_ind
for i in range(s_ind, e_ind+1):
label[i] = sentence[6]
tmp_sen = sentence[1]
tmp_i = sentence[0]
else:
tmp_doc.append(tokens)
tmp_id.append(sentence[0])
if len(sentence) == 9:
tmp_label.append(label)
tmp_doc.append(tmp_sen.split(' '))
tmp_id.append(tmp_i)
words.append(tmp_doc)
tags.append(tmp_label)
ids.append(tmp_id)
return words, tags, ids
def make_bert_dataset(dataset):
words, tags, ids= [], [], []
for article in dataset:
tmp_doc, tmp_label, tmp_id = [], [], []
tmp_sen = article[0][1]
tmp_i = article[0][0]
label = ['O'] * len(tmp_sen.split(' '))
for sentence in article:
tokens = sentence[1].split(' ')
token_len = [len(token) for token in tokens]
if len(sentence) == 9: # label exists
if tmp_sen != sentence[1] or sentence[7]:
tmp_label.append(label)
tmp_doc.append(tmp_sen.split(' '))
tmp_id.append(tmp_i)
if tmp_sen != sentence[1]:
label = ['O'] * len(token_len)
start = sentence[4] - sentence[2]
end = sentence[5] - sentence[2]
if sentence[6] != 'O':
for i in range(1, len(token_len)):
token_len[i] += token_len[i-1] + 1
token_len[-1] += 1
token_len = np.asarray(token_len)
s_ind = np.min(np.where(token_len > start))
tmp = np.where(token_len >= end)
if len(tmp[0]) != 0:
e_ind = np.min(tmp)
else:
e_ind = s_ind
for i in range(s_ind, e_ind+1):
label[i] = sentence[6]
tmp_sen = sentence[1]
tmp_i = sentence[0]
else:
tmp_doc.append(tokens)
tmp_id.append(sentence[0])
if len(sentence) == 9:
tmp_label.append(label)
tmp_doc.append(tmp_sen.split(' '))
tmp_id.append(tmp_i)
words.append(tmp_doc)
tags.append(tmp_label)
ids.append(tmp_id)
return words, tags, ids
def mda(dataset):
words, tags, ids= [], [], []
for article in dataset:
tmp_doc, tmp_label, tmp_id = [], [], []
for sentence in article:
tokens = sentence[1].split(' ')
token_len = [len(token) for token in tokens]
if len(sentence) == 9: # label exists
start = sentence[4] - sentence[2]
end = sentence[5] - sentence[2]
label = ['O'] * len(token_len)
if sentence[6] != 'O':
for i in range(1, len(token_len)):
token_len[i] += token_len[i-1] + 1
token_len[-1] += 1
token_len = np.asarray(token_len)
s_ind = np.min(np.where(token_len > start))
tmp = np.where(token_len >= end)
if len(tmp[0]) != 0:
e_ind = np.min(tmp)
else:
e_ind = s_ind
for i in range(s_ind, e_ind+1):
label[i] = sentence[6]
tmp_label.append(label)
tmp_doc.append(tokens)
tmp_id.append(sentence[0])
words.append(tmp_doc)
tags.append(tmp_label)
ids.append(tmp_id)
return words, tags, ids