Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
|
| 5 |
+
ACTIONS = {
|
| 6 |
+
"stay_course",
|
| 7 |
+
"adjust_dose",
|
| 8 |
+
"switch_class",
|
| 9 |
+
"pause_observe",
|
| 10 |
+
"add_support_module",
|
| 11 |
+
"deescalate_exit_loop",
|
| 12 |
+
"urgent_escalation",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
LABELS = {"coherent_navigation","partially_coherent","incoherent_navigation"}
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class ScoreResult:
|
| 19 |
+
score: float
|
| 20 |
+
details: Dict[str, Any]
|
| 21 |
+
|
| 22 |
+
def _has(t: str, pats: List[str]) -> bool:
|
| 23 |
+
t = (t or "").lower()
|
| 24 |
+
return any(re.search(p, t) for p in pats)
|
| 25 |
+
|
| 26 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 27 |
+
p = (prediction or "").lower().strip()
|
| 28 |
+
words_ok = len(p.split()) <= 380
|
| 29 |
+
|
| 30 |
+
action_ok = any(a in p for a in ACTIONS)
|
| 31 |
+
label_ok = any(l in p for l in LABELS)
|
| 32 |
+
signature_ref = _has(p, [r"slope", r"tolerance", r"tradeoff", r"coupling", r"volatility", r"plateau", r"discord"])
|
| 33 |
+
constraint_ref = _has(p, [r"avoid", r"contra", r"risk", r"cannot", r"must not"])
|
| 34 |
+
|
| 35 |
+
raw = (
|
| 36 |
+
0.20 * int(words_ok) +
|
| 37 |
+
0.35 * int(action_ok) +
|
| 38 |
+
0.35 * int(label_ok) +
|
| 39 |
+
0.05 * int(signature_ref) +
|
| 40 |
+
0.05 * int(constraint_ref)
|
| 41 |
+
)
|
| 42 |
+
final = max(0.0, min(1.0, raw))
|
| 43 |
+
|
| 44 |
+
return ScoreResult(
|
| 45 |
+
score=final,
|
| 46 |
+
details={
|
| 47 |
+
"action_ok": action_ok,
|
| 48 |
+
"label_ok": label_ok,
|
| 49 |
+
"id": sample.get("id"),
|
| 50 |
+
},
|
| 51 |
+
)
|
| 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)}
|