Datasets:
Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
|
| 4 |
+
@dataclass
|
| 5 |
+
class ScoreResult:
|
| 6 |
+
score: float
|
| 7 |
+
details: Dict[str, Any]
|
| 8 |
+
|
| 9 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 10 |
+
# Expect a single float coherence_score in [0,1]
|
| 11 |
+
try:
|
| 12 |
+
pred = float((prediction or "").strip())
|
| 13 |
+
except Exception:
|
| 14 |
+
pred = -1.0
|
| 15 |
+
|
| 16 |
+
true_raw = sample.get("coherence_score", "")
|
| 17 |
+
try:
|
| 18 |
+
true = float(true_raw) if true_raw not in ("", None) else None
|
| 19 |
+
except Exception:
|
| 20 |
+
true = None
|
| 21 |
+
|
| 22 |
+
if true is None:
|
| 23 |
+
ok = 0.0 <= pred <= 1.0
|
| 24 |
+
return ScoreResult(1.0 if ok else 0.0, {"mode": "format_only", "id": sample.get("id"), "pred": pred})
|
| 25 |
+
|
| 26 |
+
err = abs(true - pred)
|
| 27 |
+
return ScoreResult(max(0.0, 1.0 - err), {"id": sample.get("id"), "pred": pred, "true": true, "abs_error": err})
|
| 28 |
+
|
| 29 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 30 |
+
if not results:
|
| 31 |
+
return {"mean": 0.0, "n": 0}
|
| 32 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|