-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformater.py
199 lines (173 loc) · 8.56 KB
/
formater.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
import os
import re
import random
quoted = re.compile('"[^"]*"')
class MultiheadConllConvertor(object):
def __init__(self):
self.tok_3d = [] # (doc x sent x token)
self.ner_3d = [] # (doc x sent x ner)
self.ast_3d = []
self.head_3d = []
self.rel_3d = []
self.doc_info = []
def filter_by_length(self, len_thres):
filted_history = []
for doc_id in range(len(self.tok_3d)):
for s_id in range(len(self.tok_3d[doc_id])):
if len(self.tok_3d[doc_id][s_id]) >= len_thres:
filted_history.append((doc_id, s_id))
filted_history.reverse()
for f_d_id, f_s_id in filted_history:
del self.tok_3d[f_d_id][f_s_id]
del self.ner_3d[f_d_id][f_s_id]
del self.ast_3d[f_d_id][f_s_id]
del self.head_3d[f_d_id][f_s_id]
del self.rel_3d[f_d_id][f_s_id]
def filter_by_empty(self):
filted_history = []
for doc_id in range(len(self.ner_3d)):
for s_id in range(len(self.ner_3d[doc_id])):
if set(self.ner_3d[doc_id][s_id]) == {'O'}:
filted_history.append((doc_id, s_id))
filted_history.reverse()
for f_d_id, f_s_id in filted_history:
del self.tok_3d[f_d_id][f_s_id]
del self.ner_3d[f_d_id][f_s_id]
del self.ast_3d[f_d_id][f_s_id]
del self.head_3d[f_d_id][f_s_id]
del self.rel_3d[f_d_id][f_s_id]
def output_conll(self, out_file):
with open(out_file, 'w', encoding='utf8') as fo:
for tok_2d, ner_2d, ast_2d, head_2d, rel_2d, docinfo in zip(
self.tok_3d,
self.ner_3d,
self.ast_3d,
self.head_3d,
self.rel_3d,
self.doc_info
):
for sent_id, (tok_1d, ner_1, ast_1d, head_1d, rel_1d) in enumerate(zip(tok_2d, ner_2d, ast_2d, head_2d, rel_2d)):
fo.write("#doc_{}_sent_{}\n".format(docinfo, sent_id))
for tok_id, (tok, ner, ast, head, rel) in enumerate(zip(tok_1d, ner_1, ast_1d, head_1d, rel_1d)):
fo.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(tok_id, tok, ner, ast, rel, head))
def split_train_dev(self, train_file, dev_file, dev_ratio=0.1):
with open(train_file, 'w', encoding='utf8') as tfo, open(dev_file, 'w', encoding='utf8') as dfo:
for tok_2d, ner_2d, ast_2d, head_2d, rel_2d, docinfo in zip(
self.tok_3d,
self.ner_3d,
self.ast_3d,
self.head_3d,
self.rel_3d,
self.doc_info
):
out_writter = tfo if random.random() > dev_ratio else dfo
for sent_id, (tok_1d, ner_1, ast_1d, head_1d, rel_1d) in enumerate(zip(tok_2d, ner_2d, ast_2d, head_2d, rel_2d)):
out_writter.write("#doc_{}_sent_{}\n".format(docinfo, sent_id))
for tok_id, (tok, ner, ast, head, rel) in enumerate(zip(tok_1d, ner_1, ast_1d, head_1d, rel_1d)):
out_writter.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(tok_id, tok, ner, ast, rel, head))
@staticmethod
def read_con(con_line):
items = con_line.split()
cg = ' '.join(items[:-2]).lstrip("c=")
sent_id, tid_begin = [int(s) for s in items[-2].split(':')]
sent_id, tid_end = [int(s) for s in items[-1].split(':')]
return cg[1:-1], sent_id - 1, tid_begin, tid_end
def load_batch_from_i2b2(self, data_dir, disease):
txt_dir = os.listdir(os.path.join(data_dir, disease, 'txt'))
for file_name in sorted(txt_dir):
if file_name.endswith(".txt"):
self.load_single_from_i2b2(data_dir, disease, os.path.splitext(file_name)[0])
def load_single_from_i2b2(self, data_dir, disease, file_name):
in_txt = os.path.join(data_dir, disease, "txt", "{}.txt".format(file_name))
in_con = os.path.join(data_dir, disease, "concept", "{}.con".format(file_name))
in_ast = os.path.join(data_dir, disease, "ast", "{}.ast".format(file_name))
in_rel = os.path.join(data_dir, disease, "rel", "{}.rel".format(file_name))
tok_2d, ner_2d, ast_2d, head_2d, rel_2d = [], [], [], [], []
with open(in_txt, 'r') as fi:
for line in fi:
toks = line.rstrip().split()
tok_2d.append(toks)
ner_2d.append(['O'] * len(toks))
ast_2d.append(['_'] * len(toks))
head_2d.append([[i] for i in list(range(len(toks)))])
rel_2d.append([['N']] * len(toks))
with open(in_con, 'r') as fi:
for line in fi:
try:
tl, cl = line.rstrip().split('||')
tg, sent_id, tid_begin, tid_end = self.read_con(tl)
tp = ' '.join(tok_2d[sent_id][tid_begin: tid_end + 1])
assert tg == tp.lower()
con = quoted.findall(cl)[0].strip('"')
ner_2d[sent_id][tid_begin] = "B-{}".format(con)
if tid_end > tid_begin:
for i in range(tid_begin + 1, tid_end + 1):
ner_2d[sent_id][i] = "I-{}".format(con)
except AssertionError as ex:
print('[ner]')
print(disease, file_name)
print(line)
print(tp, '||', tg)
print()
with open(in_ast, 'r') as fi:
for line in fi:
try:
tl, cl, al = line.rstrip().split('||')
tg, sent_id, tid_begin, tid_end = self.read_con(tl)
tp = ' '.join(tok_2d[sent_id][tid_begin: tid_end + 1])
assert tg == tp.lower()
ast = quoted.findall(al)[0].strip('"')
ast_2d[sent_id][tid_end] = ast
except AssertionError as ex:
print('[ast]')
print(disease, file_name)
print(line)
print(tp, '||', tg)
print()
with open(in_rel, 'r') as fi:
for line in fi:
try:
tl, rl, hl = line.rstrip().split('||')
tg, t_sent_id, t_tid_begin, t_tid_end = self.read_con(tl)
tp = ' '.join(tok_2d[t_sent_id][t_tid_begin: t_tid_end + 1])
assert tg == tp.lower()
hg, h_sent_id, h_tid_begin, h_tid_end = self.read_con(hl)
hp = ' '.join(tok_2d[h_sent_id][h_tid_begin: h_tid_end + 1])
assert hg == hp.lower()
rel = quoted.findall(rl)[0].strip('"')
if (head_2d[t_sent_id][t_tid_end] == [t_tid_end]) or (rel_2d[t_sent_id][t_tid_end] == ['N']):
head_2d[t_sent_id][t_tid_end] = [h_tid_end]
rel_2d[t_sent_id][t_tid_end] = [rel]
else:
head_2d[t_sent_id][t_tid_end].append(h_tid_end)
rel_2d[t_sent_id][t_tid_end].append(rel)
except AssertionError as ex:
print('[rel]')
print(disease, file_name)
print(line)
print(tp, '||', tg)
print(hp, '||', hg)
print()
self.tok_3d.append(tok_2d)
self.ner_3d.append(ner_2d)
self.ast_3d.append(ast_2d)
self.head_3d.append(head_2d)
self.rel_3d.append(rel_2d)
self.doc_info.append("{}-{}".format(disease, file_name))
mhsc_training = MultiheadConllConvertor()
# mhsc.load_single_from_i2b2("/home/feicheng/Resources/concept_assertion_relation_training_data/", "beth", "record-13")
mhsc_training.load_batch_from_i2b2("/home/feicheng/Resources/i2b2va_2010/training_data", "partners")
mhsc_training.load_batch_from_i2b2("/home/feicheng/Resources/i2b2va_2010/training_data", "beth")
print(len(mhsc_training.doc_info))
mhsc_test = MultiheadConllConvertor()
mhsc_test.load_batch_from_i2b2("/home/feicheng/Resources/i2b2va_2010/test_data", "test")
len_thres = 100
mhsc_training.filter_by_length(len_thres)
# mhsc_training.filter_by_empty()
mhsc_test.filter_by_length(len_thres)
mhsc_training.split_train_dev("data/i2b2/i2b2_training.conll", "data/i2b2/i2b2_dev.conll", dev_ratio=0.09)
mhsc_test.output_conll("data/i2b2/i2b2_test.conll")
for d in mhsc_training.tok_3d:
for s in d:
if len(s) > len_thres:
print(' '.join(s))