import csv import math ALLOWED = {"aligned", "partial", "divergent"} def parse_prediction(text): out = {} if not text: return out parts = [p.strip() for p in text.split(";") if p.strip()] for p in parts: if "=" in p: k, v = p.split("=", 1) out[k.strip()] = v.strip() return out def clamp01(x): return max(0.0, min(1.0, x)) def score(pred_file, gold_file): preds = {} with open(pred_file, newline="") as f: r = csv.DictReader(f) for row in r: preds[row["id"]] = parse_prediction(row.get("prediction","")) gold = {} with open(gold_file, newline="") as f: r = csv.DictReader(f) for row in r: gold[row["id"]] = row n = 0 acc = 0 mse = 0.0 div_hit = 0 for k, g in gold.items(): if k not in preds: continue p = preds[k] n += 1 gl = (g.get("alignment_label","") or "").strip().lower() pl = (p.get("alignment_label","") or "").strip().lower() if pl == gl and gl in ALLOWED: acc += 1 try: gs = float(g.get("alignment_score","")) ps = float(p.get("alignment_score","")) ps = clamp01(ps) mse += (gs - ps) ** 2 except: pass # simple divergence_points presence check (should not be empty for divergent/partial) dp = (p.get("divergence_points","") or "").strip() if gl in {"divergent", "partial"} and len(dp) >= 8: div_hit += 1 if gl == "aligned" and (dp.lower() in {"none", ""}): div_hit += 1 label_acc = acc / n if n else 0.0 rmse = math.sqrt(mse / n) if n else 0.0 div_score = div_hit / n if n else 0.0 # weighted total score total = 0.55 * label_acc + 0.30 * (1.0 - rmse) + 0.15 * div_score total = clamp01(total) print("label_accuracy:", round(label_acc, 3)) print("alignment_score_RMSE:", round(rmse, 3)) print("divergence_points_quality:", round(div_score, 3)) print("total_score:", round(total, 3))