-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_attr_ext.py
More file actions
304 lines (243 loc) · 9.36 KB
/
Copy patheval_attr_ext.py
File metadata and controls
304 lines (243 loc) · 9.36 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import argparse
import json
import logging
import sys
from collections import Counter, defaultdict
from tuw_nlp.common.eval import print_cat_stats
from brise_plandok.annotation.attributes import ATTR_TO_CAT
from brise_plandok.asail.extractor import get_extractor
from brise_plandok.constants import ATTRIBUTE_NORM_MAP, SenFields, DocumentFields
ATTR_IGNORE = {
"?",
"N/A",
"Weitere_Bestimmung_Prüfung_erforderlich",
"WeitereBestimmungPruefungErforderlich",
"AusnahmePruefungErforderlich",
"ZuVorherigemSatzGehoerig",
"Plangebiet",
"PlanzeichenBBID",
"WidmungID",
"obligation",
"prohibition",
"permission",
}
ATTR_CATS = {}
# ATTR_CATS = {label: '*Flaeche*' for label in {
# 'BBBebaubareFlaecheAbgegrenzt',
# 'BBBebaubareFlaecheJeBauplatz',
# 'BBBebaubareFlaecheGesamterBauplatz',
# 'BBAusnuetzbarkeitFlaecheGrundflaechenbezugRelativ',
# 'BBAusnuetzbarkeitFlaecheGrundflaechenbezug',
# 'BBAusnuetzbarkeitFlaecheWohnnutzflaeche'}}
def preprocess_attrs(attrs):
pp_attrs = []
for attr in attrs:
if type(attr) is str:
name = attr
attr = {
"name": name,
"value": None,
"type": None,
}
name = attr["name"]
if name.startswith('"'):
continue
new_name = name.replace("(?)", "").strip("? ").replace("ä", "ae")
if new_name in ATTRIBUTE_NORM_MAP:
new = ATTRIBUTE_NORM_MAP[new_name]
logging.warning(f"converting old attr ({new_name}) to new ({new})")
new_name = new
if new_name in ATTR_IGNORE:
continue
if new_name not in ATTR_TO_CAT:
logging.warning(f"{new_name} attribute is not recognized - will be ignored")
continue
if attr["value"]:
attr["value"] = attr["value"].replace("ä", "ae")
pp_attrs.append({"name": new_name, "value": attr["value"], "type": attr["type"]})
return pp_attrs
def load_sample(stream, flat):
sections = []
for line in stream:
doc = json.loads(line)
gold = False
if DocumentFields.LABELS_GOLD in doc:
gold = doc[DocumentFields.LABELS_GOLD]
annotated = False
if DocumentFields.ANNOTATORS in doc:
annotated = len(doc[DocumentFields.ANNOTATORS]) > 0
if flat:
for sen in doc["sens"].values():
if len(sections) == 0:
sections.append({"sens": []})
_load_sen(sen, sections, gold, annotated)
else:
for section in doc["sections"]:
sections.append({"sens": []})
for sen in section["sens"]:
_load_sen(sen, sections, gold, annotated)
return sections
def _load_sen(sen, sections, gold, annotated):
if gold:
add_attribute(sections, sen, sen[SenFields.GOLD_ATTRIBUTES].keys())
elif annotated:
add_attribute(sections, sen, sen[SenFields.ANNOTATED_ATTRIBUTES].keys())
elif sen["attributes"]:
add_attribute(sections, sen, sen["attributes"])
def add_attribute(sections, sen, attributes):
sen["attributes"] = preprocess_attrs(attributes)
sections[-1]["sens"].append(sen)
def get_err_ids(label, results):
return [
i for i, (text, attrs, preds) in enumerate(results) if (label in attrs) ^ (label in preds)
]
def get_err_ids_cat(cat, results):
def cats(s):
return {ATTR_CATS.get(a, a) for a in s}
return [
i
for i, (text, attrs, preds) in enumerate(results)
if (cat in cats(attrs)) ^ (cat in cats(preds))
]
def print_output(results, fn):
with open(fn, "w") as f_obj:
for _, rec in results.items():
json.dump(rec, f_obj)
def count_attr_stats(sample, label_cats=None, print_errs=False):
cats = defaultdict(Counter)
for sen_id, orig_attrs, orig_preds in sample:
attrs = {label_cats.get(a, a) for a in orig_attrs}
preds = {label_cats.get(a, a) for a in orig_preds}
for attr in attrs & preds:
cats[attr]["TP"] += 1
for attr in attrs - preds:
cats[attr]["FN"] += 1
if print_errs:
print(f"FN: {attr}: {sen_id}")
for attr in preds - attrs:
cats[attr]["FP"] += 1
if print_errs:
print(f"FP: {attr}: {sen_id}")
cats["total"] = {stat: sum(s[stat] for s in cats.values()) for stat in ("TP", "FN", "FP")}
return cats
def eval_attrs(results, print_errs=False):
print()
print("=" * 10)
print("Attribute extraction")
print("=" * 10)
attr_results = []
for sen_id, sen in results.items():
gold_attr_names = set(attr["name"] for attr in sen["attributes"])
gen_attr_names = set(attr["name"] for attr in sen["gen_attributes"])
attr_results.append((sen_id, gold_attr_names, gen_attr_names))
cats = count_attr_stats(attr_results, label_cats=ATTR_CATS, print_errs=print_errs)
print_cat_stats(cats, 500)
def eval_modality(results):
print()
print("=" * 10)
print("Modality")
print("=" * 10)
stats = Counter()
for sen_id, sen in results.items():
stats["all"] += 1
mod = sen["modality"][0]
gen_mod = sen["gen_mod"]
if mod == gen_mod:
stats["corr"] += 1
else:
print(f"{sen_id} incorrect modality: {gen_mod} (correct: {mod})")
acc = stats["corr"] / stats["all"]
print(f'modality accuracy: {acc:.2%} ({stats["corr"]} of {stats["all"]})')
def eval_types_values(results):
print()
print("=" * 10)
print("Attr. types and values")
print("=" * 10)
stats = Counter()
fp_errs = Counter()
fn_errs = Counter()
for sen_id, sen in results.items():
gold_attrs = defaultdict(lambda: defaultdict(set))
gen_attrs = defaultdict(lambda: defaultdict(set))
for attr in sen["attributes"]:
if attr["name"] not in ATTR_IGNORE:
gold_attrs[attr["name"]][attr["type"]].add(attr["value"])
for attr in sen["gen_attributes"]:
gen_attrs[attr["name"]][attr["type"]].add(attr["value"])
all_names = set()
for name in gen_attrs:
all_names.add(name)
all_types = set()
for atype, values in gen_attrs[name].items():
all_types.add(atype)
stats["gen_attr"] += len(values)
if name not in gold_attrs:
print(f"{sen_id} incorrect attribute: {name}")
stats["FP"] += len(values)
fp_errs["no such attr"] += len(values)
continue
if atype not in gold_attrs[name]:
print(
f"{sen_id} incorrect attr type {atype} of {name}"
f"(correct: {gold_attrs[name]})"
)
stats["FP"] += len(values)
fp_errs["wrong type"] += len(values)
continue
gold_vals = gold_attrs[name][atype]
stats["TP"] += len(gold_vals & values)
for fp in values - gold_vals:
fp_errs["wrong value"] += 1
stats["FP"] += 1
print(f"{sen_id} incorrect value {fp} of {name}")
for fn in gold_vals - values:
stats["FN"] += 1
fn_errs["value"] += 1
print(f"{sen_id} missing value {fn} of {name}")
for atype, gold_vals in gold_attrs[name].items():
if atype not in all_types:
print(f"{sen_id} missing attr type: {atype} of {name}")
stats["FN"] += len(gold_vals)
fn_errs["type"] += len(gold_vals)
for name in gold_attrs:
if name not in all_names:
print(f"{sen_id} missing attribute: " f"{name} ({gold_attrs[name]})")
total = sum(len(g_values) for g_values in gold_attrs[name].values())
fn_errs["missing attr"] += total
stats["FN"] += total
print_cat_stats({"attrs": stats})
print()
print("FN errs:")
print(fn_errs.most_common())
print()
print("FP errs:")
print(fp_errs.most_common())
def eval_results(results, args):
eval_attrs(results, print_errs=args.print_errs)
if args.rule_ext:
eval_modality(results)
eval_types_values(results)
def eval_rule_ext(args):
sections = load_sample(sys.stdin, args.flat)
with get_extractor(args) as extractor:
sections, results = extractor.run_on_sections(sections)
if args.output_file:
print_output(results, args.output_file)
eval_results(results, args)
def get_args():
parser = argparse.ArgumentParser(description="")
parser.add_argument("-o", "--output-file", type=str)
parser.add_argument("-p", "--print-errs", default=False, action="store_true")
parser.add_argument("-cd", "--cache-dir", default="cache", type=str)
parser.add_argument("-r", "--rule-ext", default=False, action="store_true")
parser.add_argument("-f", "--flat", default=False, action="store_true")
return parser.parse_args()
def main():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s : " + "%(module)s (%(lineno)s) - %(levelname)s - %(message)s",
)
args = get_args()
eval_rule_ext(args)
if __name__ == "__main__":
main()