-
Notifications
You must be signed in to change notification settings - Fork 0
/
conll.py
216 lines (158 loc) · 6 KB
/
conll.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
import re
"""
Modified version of https://pypi.org/project/conlleval/
"""
def stats():
return {'cor': 0, 'hyp': 0, 'ref': 0}
def evaluate(ref, hyp, otag='O'):
# evaluation for NLTK
aligned = align_hyp(ref, hyp)
return conlleval(aligned, otag=otag)
def align_hyp(ref, hyp):
# align references and hypothese for evaluation
# add last element of token tuple in hyp to ref
if len(ref) != len(hyp):
raise ValueError("Size Mismatch: ref: {} & hyp: {}".format(len(ref), len(hyp)))
out = []
for i in range(len(ref)):
if len(ref[i]) != len(hyp[i]):
raise ValueError("Size Mismatch: ref: {} & hyp: {}".format(len(ref), len(hyp)))
out.append([(*ref[i][j], hyp[i][j][-1]) for j in range(len(ref[i]))])
return out
def conlleval(data, otag='O'):
# token, segment & class level counts for TP, TP+FP, TP+FN
tok = stats()
seg = stats()
cls = {}
for sent in data:
prev_ref = otag # previous reference label
prev_hyp = otag # previous hypothesis label
prev_ref_iob = None # previous reference label IOB
prev_hyp_iob = None # previous hypothesis label IOB
in_correct = False # currently processed chunks is correct until now
for token in sent:
hyp_iob, hyp = parse_iob(token[-1])
ref_iob, ref = parse_iob(token[-2])
ref_e = is_eoc(ref, ref_iob, prev_ref, prev_ref_iob, otag)
hyp_e = is_eoc(hyp, hyp_iob, prev_hyp, prev_hyp_iob, otag)
ref_b = is_boc(ref, ref_iob, prev_ref, prev_ref_iob, otag)
hyp_b = is_boc(hyp, hyp_iob, prev_hyp, prev_hyp_iob, otag)
if not cls.get(ref) and ref:
cls[ref] = stats()
if not cls.get(hyp) and hyp:
cls[hyp] = stats()
# segment-level counts
if in_correct:
if ref_e and hyp_e and prev_hyp == prev_ref:
in_correct = False
seg['cor'] += 1
cls[prev_ref]['cor'] += 1
elif ref_e != hyp_e or hyp != ref:
in_correct = False
if ref_b and hyp_b and hyp == ref:
in_correct = True
if ref_b:
seg['ref'] += 1
cls[ref]['ref'] += 1
if hyp_b:
seg['hyp'] += 1
cls[hyp]['hyp'] += 1
# token-level counts
if ref == hyp and ref_iob == hyp_iob:
tok['cor'] += 1
tok['ref'] += 1
prev_ref = ref
prev_hyp = hyp
prev_ref_iob = ref_iob
prev_hyp_iob = hyp_iob
if in_correct:
seg['cor'] += 1
cls[prev_ref]['cor'] += 1
return summarize(seg, cls)
def parse_iob(t):
m = re.match(r'^([^-]*)-(.*)$', t)
return m.groups() if m else (t, None)
def is_boc(lbl, iob, prev_lbl, prev_iob, otag='O'):
"""
is beginning of a chunk
supports: IOB, IOBE, BILOU schemes
- {E,L} --> last
- {S,U} --> unit
:param lbl: current label
:param iob: current iob
:param prev_lbl: previous label
:param prev_iob: previous iob
:param otag: out-of-chunk label
:return:
"""
boc = False
boc = True if iob in ['B', 'S', 'U'] else boc
boc = True if iob in ['E', 'L'] and prev_iob in ['E', 'L', 'S', otag] else boc
boc = True if iob == 'I' and prev_iob in ['S', 'L', 'E', otag] else boc
boc = True if lbl != prev_lbl and iob != otag and iob != '.' else boc
# these chunks are assumed to have length 1
boc = True if iob in ['[', ']'] else boc
return boc
def is_eoc(lbl, iob, prev_lbl, prev_iob, otag='O'):
"""
is end of a chunk
supports: IOB, IOBE, BILOU schemes
- {E,L} --> last
- {S,U} --> unit
:param lbl: current label
:param iob: current iob
:param prev_lbl: previous label
:param prev_iob: previous iob
:param otag: out-of-chunk label
:return:
"""
eoc = False
eoc = True if iob in ['E', 'L', 'S', 'U'] else eoc
eoc = True if iob == 'B' and prev_iob in ['B', 'I'] else eoc
eoc = True if iob in ['S', 'U'] and prev_iob in ['B', 'I'] else eoc
eoc = True if iob == otag and prev_iob in ['B', 'I'] else eoc
eoc = True if lbl != prev_lbl and iob != otag and prev_iob != '.' else eoc
# these chunks are assumed to have length 1
eoc = True if iob in ['[', ']'] else eoc
return eoc
def score(cor_cnt, hyp_cnt, ref_cnt):
# precision
p = 1 if hyp_cnt == 0 else cor_cnt / hyp_cnt
# recall
r = 0 if ref_cnt == 0 else cor_cnt / ref_cnt
# f-measure (f1)
f = 0 if p+r == 0 else (2*p*r)/(p+r)
return {"p": p, "r": r, "f": f, "s": ref_cnt}
def summarize(seg, cls):
# class-level
res = {lbl: score(cls[lbl]['cor'], cls[lbl]['hyp'], cls[lbl]['ref']) for lbl in set(cls.keys())}
# micro
res.update({"total": score(seg.get('cor', 0), seg.get('hyp', 0), seg.get('ref', 0))})
return res
def read_corpus_conll(corpus_file, fs="\t"):
"""
read corpus in CoNLL format
:param corpus_file: corpus in conll format
:param fs: field separator
:return: corpus
"""
featn = None # number of features for consistency check
sents = [] # list to hold words list sequences
words = [] # list to hold feature tuples
for line in open(corpus_file):
line = line.strip()
if len(line.strip()) > 0:
feats = tuple(line.strip().split(fs))
if not featn:
featn = len(feats)
elif featn != len(feats) and len(feats) != 0:
raise ValueError("Unexpected number of columns {} ({})".format(len(feats), featn))
words.append(feats)
else:
if len(words) > 0:
sents.append(words)
words = []
return sents
def get_chunks(corpus_file, fs="\t", otag="O"):
sents = read_corpus_conll(corpus_file, fs=fs)
return set([parse_iob(token[-1])[1] for sent in sents for token in sent if token[-1] != otag])