Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ = [
|
| 6 |
+
"loop_coherence_index",
|
| 7 |
+
"resonance_stability_band",
|
| 8 |
+
"control_lag_profile",
|
| 9 |
+
"correction_efficiency_score",
|
| 10 |
+
"baseline_deviation",
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
BANDS = ["stable","fragile","unstable"]
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class ScoreResult:
|
| 17 |
+
score: float
|
| 18 |
+
details: Dict[str, Any]
|
| 19 |
+
|
| 20 |
+
def _f(p: str, key: str):
|
| 21 |
+
m = re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", p)
|
| 22 |
+
return float(m.group(1)) if m else None
|
| 23 |
+
|
| 24 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 25 |
+
p = (prediction or "").lower()
|
| 26 |
+
words_ok = len(p.split()) <= 800
|
| 27 |
+
|
| 28 |
+
hits = sum(1 for k in REQ if k in p)
|
| 29 |
+
|
| 30 |
+
idx = _f(p,"loop_coherence_index")
|
| 31 |
+
eff = _f(p,"correction_efficiency_score")
|
| 32 |
+
dev = _f(p,"baseline_deviation")
|
| 33 |
+
|
| 34 |
+
numeric_ok = int(
|
| 35 |
+
idx is not None and 0<=idx<=1 and
|
| 36 |
+
eff is not None and 0<=eff<=1 and
|
| 37 |
+
dev is not None and 0<=dev<=1
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
band_ok = int("resonance_stability_band" in p and any(b in p for b in BANDS))
|
| 41 |
+
lag_ok = int("control_lag_profile" in p)
|
| 42 |
+
|
| 43 |
+
raw = (
|
| 44 |
+
0.2 * int(words_ok) +
|
| 45 |
+
0.4 * (hits/len(REQ)) +
|
| 46 |
+
0.25 * numeric_ok +
|
| 47 |
+
0.1 * band_ok +
|
| 48 |
+
0.05 * lag_ok
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
return ScoreResult(score=min(1.0,raw), details={"id": sample.get("id"),"hits":hits})
|
| 52 |
+
|
| 53 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 54 |
+
if not results:
|
| 55 |
+
return {"mean":0.0,"n":0}
|
| 56 |
+
return {"mean":sum(r.score for r in results)/len(results),"n":len(results)}
|