-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
25 lines (25 loc) · 1 KB
/
eval.py
File metadata and controls
25 lines (25 loc) · 1 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
import argparse, csv, json
def load_schema(p): return json.load(open(p,'r',encoding='utf-8'))
def tag_text(text, schema):
text_l=text.lower(); preds=set()
for tag,kws in schema.items():
for kw in kws:
if kw in text_l: preds.add(tag); break
return preds
def main():
ap=argparse.ArgumentParser()
ap.add_argument('--notes', required=True)
ap.add_argument('--schema', default='tag_schema.json')
args=ap.parse_args()
schema=load_schema(args.schema)
rows=list(csv.DictReader(open(args.notes,encoding='utf-8')))
tp=fp=fn=0
for r in rows:
gold=set([t.strip() for t in r['gold_tags'].split(';') if t.strip()])
pred=tag_text(r['note'], schema)
tp+=len(gold&pred); fp+=len(pred-gold); fn+=len(gold-pred)
prec = tp/(tp+fp) if (tp+fp)>0 else 0.0
rec = tp/(tp+fn) if (tp+fn)>0 else 0.0
f1 = 2*prec*rec/(prec+rec) if (prec+rec)>0 else 0.0
print(f"Precision: {prec:.3f}\nRecall: {rec:.3f}\nF1: {f1:.3f}")
if __name__=='__main__': main()