File size: 2,106 Bytes
016dd6c | 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 | 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))
|