File size: 1,601 Bytes
e8689cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)}