Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
from typing import Dict, Tuple
|
| 5 |
+
|
| 6 |
+
ALLOWED_TYPES = {
|
| 7 |
+
"low_uncertainty",
|
| 8 |
+
"moderate_confidence",
|
| 9 |
+
"confidence_borderline",
|
| 10 |
+
"confidence_low",
|
| 11 |
+
"disorder_high",
|
| 12 |
+
"model_disagreement",
|
| 13 |
+
"high_disorder",
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
def norm(s: str) -> str:
|
| 17 |
+
return re.sub(r"\s+", " ", (s or "").strip().lower())
|
| 18 |
+
|
| 19 |
+
def token_set(s: str) -> set:
|
| 20 |
+
s = norm(s)
|
| 21 |
+
s = re.sub(r"[^a-z0-9\s]", " ", s)
|
| 22 |
+
return set([t for t in s.split(" ") if t])
|
| 23 |
+
|
| 24 |
+
def jaccard(a: str, b: str) -> float:
|
| 25 |
+
ta, tb = token_set(a), token_set(b)
|
| 26 |
+
if not ta or not tb:
|
| 27 |
+
return 0.0
|
| 28 |
+
return len(ta & tb) / len(ta | tb)
|
| 29 |
+
|
| 30 |
+
def load_refs(test_csv):
|
| 31 |
+
refs={}
|
| 32 |
+
with open(test_csv,encoding="utf-8") as f:
|
| 33 |
+
r=csv.DictReader(f)
|
| 34 |
+
for row in r:
|
| 35 |
+
refs[row["id"]] = (
|
| 36 |
+
norm(row["gold_uncertainty_flag"]),
|
| 37 |
+
row["gold_uncertainty_type"],
|
| 38 |
+
row["gold_recommendation"],
|
| 39 |
+
)
|
| 40 |
+
return refs
|
| 41 |
+
|
| 42 |
+
def extract_json(text):
|
| 43 |
+
try:
|
| 44 |
+
return json.loads(text)
|
| 45 |
+
except:
|
| 46 |
+
m=re.search(r"\{.*\}",text)
|
| 47 |
+
if m:
|
| 48 |
+
try:
|
| 49 |
+
return json.loads(m.group(0))
|
| 50 |
+
except:
|
| 51 |
+
return {}
|
| 52 |
+
return {}
|
| 53 |
+
|
| 54 |
+
def score(pred_path,test_csv):
|
| 55 |
+
refs=load_refs(test_csv)
|
| 56 |
+
|
| 57 |
+
n=0
|
| 58 |
+
flag_hits=0
|
| 59 |
+
type_hits=0
|
| 60 |
+
rec_sim=0
|
| 61 |
+
fmt_hits=0
|
| 62 |
+
|
| 63 |
+
with open(pred_path,encoding="utf-8") as f:
|
| 64 |
+
preds=[json.loads(x) for x in f if x.strip()]
|
| 65 |
+
|
| 66 |
+
for p in preds:
|
| 67 |
+
pid=p["id"]
|
| 68 |
+
if pid not in refs:
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
n+=1
|
| 72 |
+
parsed=extract_json(p["prediction"])
|
| 73 |
+
|
| 74 |
+
pred_flag=norm(parsed.get("uncertainty_flag"))
|
| 75 |
+
pred_type=parsed.get("uncertainty_type","")
|
| 76 |
+
pred_rec=parsed.get("recommendation","")
|
| 77 |
+
|
| 78 |
+
gold_flag,gold_type,gold_rec=refs[pid]
|
| 79 |
+
|
| 80 |
+
if pred_flag==gold_flag:
|
| 81 |
+
flag_hits+=1
|
| 82 |
+
|
| 83 |
+
if pred_type==gold_type:
|
| 84 |
+
type_hits+=1
|
| 85 |
+
|
| 86 |
+
rec_sim+=jaccard(pred_rec,gold_rec)
|
| 87 |
+
|
| 88 |
+
if pred_flag in {"yes","no"} and pred_type in ALLOWED_TYPES:
|
| 89 |
+
fmt_hits+=1
|
| 90 |
+
|
| 91 |
+
if n==0:
|
| 92 |
+
return {"final_score":0}
|
| 93 |
+
|
| 94 |
+
acc_flag=flag_hits/n
|
| 95 |
+
acc_type=type_hits/n
|
| 96 |
+
sim_rec=rec_sim/n
|
| 97 |
+
fmt=fmt_hits/n
|
| 98 |
+
|
| 99 |
+
final=0.4*acc_flag+0.3*acc_type+0.2*sim_rec+0.1*fmt
|
| 100 |
+
|
| 101 |
+
return {
|
| 102 |
+
"final_score":final,
|
| 103 |
+
"uncertainty_flag_accuracy":acc_flag,
|
| 104 |
+
"uncertainty_type_accuracy":acc_type,
|
| 105 |
+
"recommendation_similarity":sim_rec,
|
| 106 |
+
"format_pass_rate":fmt,
|
| 107 |
+
"n":n
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
if __name__=="__main__":
|
| 111 |
+
import argparse
|
| 112 |
+
p=argparse.ArgumentParser()
|
| 113 |
+
p.add_argument("--predictions")
|
| 114 |
+
p.add_argument("--test_csv")
|
| 115 |
+
args=p.parse_args()
|
| 116 |
+
print(score(args.predictions,args.test_csv))
|