-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse_genia.py
executable file
·233 lines (187 loc) · 7.39 KB
/
parse_genia.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
#!/usr/bin/env python
from typing import Dict, Optional, Tuple, List
import os
# Download http://www.nactem.ac.uk/GENIA/current/GENIA-corpus/Part-of-speech/GENIAcorpus3.02p.tgz
CORPUS_FILE_PATH: str = "../GENIA/GENIAcorpus3.02.merged.xml"
class Stat:
def __init__(self) -> None:
self.total: int = 0
self.layer: List[int] = []
self.ignored: int = 0
self.num_labels: int = 0
TAG_SET: Dict[str, Stat] = {'G#DNA': Stat(),
'G#RNA': Stat(),
'G#protein': Stat(),
'G#cell_line': Stat(),
'G#cell_type': Stat()}
class Label:
def __init__(self) -> None:
self.start: Optional[int] = None
self.end: Optional[int] = None
self.tag: Optional[str] = None
def __eq__(self, other) -> bool:
return self.start == other.start and self.end == other.end and self.tag == other.tag
def __str__(self) -> str:
return str(self.start) + ',' + str(self.end) + ' ' + self.tag
def calc_stat(words: List[str], labels: List[Label]) -> None:
labels = sorted(labels, key=lambda x: (x.start, -x.end, x.tag))
for tag, stat in TAG_SET.items():
sequence_label = [0] * len(words)
prev_label = None
for label in labels:
if label.tag != tag:
continue
stat.total += 1
if prev_label is not None and label == prev_label:
depth = sequence_label[label.start] - 1
stat.layer[depth] += 1
continue
flag = True
depth = sequence_label[label.start]
for index in range(label.start + 1, label.end):
if sequence_label[index] != depth:
flag = False
break
if flag:
for index in range(label.start, label.end):
sequence_label[index] += 1
if len(stat.layer) == depth:
stat.layer.append(0)
stat.layer[depth] += 1
else:
stat.ignored += 1
prev_label = label
stat.num_labels += sum(sequence_label) + len(words)
SENTENCE_BEGIN_TAG = '<sentence>'
SENTENCE_END_TAG = '</sentence>'
MENTION_BEGIN_TAG = '<cons '
MENTION_END_TAG = '</cons>'
WORD_BEGIN_TAG = '<w '
WORD_END_TAG = '</w>'
LEX_ATTRIBUTE = ' lex="'
SEM_ATTRIBUTE = ' sem="'
def parse_line(line: str) -> Tuple[str, str]:
if line.find('HMG-I(Y)</cons>') > -1:
line = line.replace('HMG-I(Y)</cons>', '<w c="NN">HMG-I(Y)</w></cons>')
# words
word_tags_begin = []
words_begin = []
index = -1
while True:
index = line.find(WORD_BEGIN_TAG, index + 1)
if index < 0:
break
word_tags_begin.append(index)
index = line.find('>', index + 1)
words_begin.append(index + 1)
words_end = []
index = -1
while True:
index = line.find(WORD_END_TAG, index + 1)
if index < 0:
break
words_end.append(index)
assert (len(words_begin) == len(words_end))
words = list()
for bi, ei in zip(words_begin, words_end):
words.append(line[bi:ei].replace(' ', '\xa0'))
# labels
mention_tags_begin = []
mentions_begin = []
index = -1
while True:
index = line.find(MENTION_BEGIN_TAG, index + 1)
if index < 0:
break
mention_tags_begin.append(index)
index = line.find('>', index + 1)
mentions_begin.append(index)
mentions_end = []
index = -1
while True:
index = line.find(MENTION_END_TAG, index + 1)
if index < 0:
break
mentions_end.append(index)
assert (len(mentions_begin) == len(mentions_end))
tags = []
for bi, ei in zip(mention_tags_begin, mentions_begin):
bi2 = line.find(SEM_ATTRIBUTE, bi, ei)
if bi2 < 0:
tags.append(None)
continue
bi2 += len(SEM_ATTRIBUTE)
ei2 = line.index('"', bi2, ei)
tags.append(line[bi2:ei2])
stack = []
que = []
for index in range(len(line)):
if index in mentions_begin:
label = Label()
label.start = len([i for i in words_begin if i < index])
label.tag = tags.pop(0)
stack.append(label)
elif index in mentions_end:
label = stack.pop()
label.end = len([i for i in words_end if i <= index])
que.append(label)
labels = list()
for label in que:
for tag in TAG_SET:
if label.tag is not None and label.tag.find(tag) > -1:
label.tag = tag
labels.append(label)
break
calc_stat(words, labels)
return ' '.join(words), '|'.join([str(label) for label in labels])
def parse_genia() -> None:
output_dir_path = "data/genia/"
os.makedirs(output_dir_path, mode=0o755, exist_ok=True)
output_file_list = ["genia.train", "genia.dev", "genia.test"]
dataset_size_list = [15022, 1669, 1855]
with open(CORPUS_FILE_PATH, 'r') as f:
for output_file, dataset_size in zip(output_file_list, dataset_size_list):
output_lines = []
sent_count = 0
token_count = 0
for tag in TAG_SET:
TAG_SET[tag] = Stat()
for line in f:
line = line.strip()
if line.find(SENTENCE_BEGIN_TAG) > -1:
assert (line.find(SENTENCE_END_TAG) > -1)
words, labels = parse_line(line)
output_lines.append(words + '\n')
output_lines.append(labels + '\n')
output_lines.append('\n')
sent_count += 1
token_count += len(words.split(' '))
if sent_count == dataset_size:
with open(output_dir_path + output_file, 'w') as f2:
f2.writelines(output_lines)
print("")
print("--- {}".format(output_file))
print("# of sentences:\t{:6d}".format(sent_count))
print("# of tokens:\t{:6d}".format(token_count))
total = 0
total_layer = []
total_ignored = 0
for _, stat in TAG_SET.items():
total += stat.total
for depth, num in enumerate(stat.layer):
if len(total_layer) == depth:
total_layer.append(0)
total_layer[depth] += num
total_ignored += stat.ignored
print("total # of mentions:\t{}\t(layer:\t{},\tignored:\t{})"
.format(total, total_layer, total_ignored))
for tag, stat in TAG_SET.items():
print("\t{}:\t{:5d}\t(layer:\t{},\tignored:\t{})"
.format(tag, stat.total, stat.layer, stat.ignored))
ave_labels = 0
for _, stat in TAG_SET.items():
ave_labels += stat.num_labels
ave_labels /= token_count * len(TAG_SET)
print("average # of labels:\t{:.2f}".format(ave_labels))
break
parse_genia()