Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ = [
|
| 6 |
+
"selected_policy_id",
|
| 7 |
+
"policy_mode",
|
| 8 |
+
"predicted_coherence_trajectory",
|
| 9 |
+
"intervention_intensity",
|
| 10 |
+
"communication_strategy",
|
| 11 |
+
"policy_switch_trigger",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
POLICY = ["p1", "p2", "p3"]
|
| 15 |
+
INTENSITY = ["low", "medium", "high"]
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class ScoreResult:
|
| 19 |
+
score: float
|
| 20 |
+
details: Dict[str, Any]
|
| 21 |
+
|
| 22 |
+
def _traj_ok(p: str):
|
| 23 |
+
return "->" in p and re.search(r"\b0\.\d+\s*->\s*0\.\d+", p) is not None
|
| 24 |
+
|
| 25 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 26 |
+
p = (prediction or "").lower()
|
| 27 |
+
words_ok = len(p.split()) <= 900
|
| 28 |
+
|
| 29 |
+
hits = sum(1 for k in REQ if k in p)
|
| 30 |
+
|
| 31 |
+
policy_ok = int("selected_policy_id" in p and any(x in p for x in POLICY))
|
| 32 |
+
mode_ok = int("policy_mode" in p)
|
| 33 |
+
traj_ok = int("predicted_coherence_trajectory" in p and _traj_ok(p))
|
| 34 |
+
inten_ok = int("intervention_intensity" in p and any(i in p for i in INTENSITY))
|
| 35 |
+
comm_ok = int("communication_strategy" in p and len(p) > 80)
|
| 36 |
+
trig_ok = int("policy_switch_trigger" in p and len(p) > 100)
|
| 37 |
+
|
| 38 |
+
raw = (
|
| 39 |
+
0.15 * int(words_ok) +
|
| 40 |
+
0.35 * (hits / len(REQ)) +
|
| 41 |
+
0.15 * policy_ok +
|
| 42 |
+
0.10 * traj_ok +
|
| 43 |
+
0.10 * inten_ok +
|
| 44 |
+
0.075 * comm_ok +
|
| 45 |
+
0.075 * trig_ok
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits})
|
| 49 |
+
|
| 50 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 51 |
+
if not results:
|
| 52 |
+
return {"mean": 0.0, "n": 0}
|
| 53 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|