| from dataclasses import dataclass |
| from typing import Dict, Any, List |
| import re |
|
|
| REQ = [ |
| "selected_policy_id", |
| "policy_mode", |
| "predicted_coherence_trajectory", |
| "intervention_intensity", |
| "communication_strategy", |
| "policy_switch_trigger", |
| ] |
|
|
| POLICY = ["p1", "p2", "p3"] |
| INTENSITY = ["low", "medium", "high"] |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def _traj_ok(p: str): |
| return "->" in p and re.search(r"\b0\.\d+\s*->\s*0\.\d+", p) is not None |
|
|
| def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: |
| p = (prediction or "").lower() |
| words_ok = len(p.split()) <= 900 |
|
|
| hits = sum(1 for k in REQ if k in p) |
|
|
| policy_ok = int("selected_policy_id" in p and any(x in p for x in POLICY)) |
| mode_ok = int("policy_mode" in p) |
| traj_ok = int("predicted_coherence_trajectory" in p and _traj_ok(p)) |
| inten_ok = int("intervention_intensity" in p and any(i in p for i in INTENSITY)) |
| comm_ok = int("communication_strategy" in p and len(p) > 80) |
| trig_ok = int("policy_switch_trigger" in p and len(p) > 100) |
|
|
| raw = ( |
| 0.15 * int(words_ok) + |
| 0.35 * (hits / len(REQ)) + |
| 0.15 * policy_ok + |
| 0.10 * traj_ok + |
| 0.10 * inten_ok + |
| 0.075 * comm_ok + |
| 0.075 * trig_ok |
| ) |
|
|
| return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits}) |
|
|
| def aggregate(results: List[ScoreResult]) -> Dict[str, Any]: |
| if not results: |
| return {"mean": 0.0, "n": 0} |
| return {"mean": sum(r.score for r in results)/len(results), "n": len(results)} |
|
|