from dataclasses import dataclass from typing import Dict, Any, List import re REQ = [ "loop_coherence_index", "resonance_stability_band", "control_lag_profile", "correction_efficiency_score", "baseline_deviation", ] BANDS = ["stable","fragile","unstable"] @dataclass class ScoreResult: score: float details: Dict[str, Any] def _f(p: str, key: str): m = re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", p) return float(m.group(1)) if m else None def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: p = (prediction or "").lower() words_ok = len(p.split()) <= 800 hits = sum(1 for k in REQ if k in p) idx = _f(p,"loop_coherence_index") eff = _f(p,"correction_efficiency_score") dev = _f(p,"baseline_deviation") numeric_ok = int( idx is not None and 0<=idx<=1 and eff is not None and 0<=eff<=1 and dev is not None and 0<=dev<=1 ) band_ok = int("resonance_stability_band" in p and any(b in p for b in BANDS)) lag_ok = int("control_lag_profile" in p) raw = ( 0.2 * int(words_ok) + 0.4 * (hits/len(REQ)) + 0.25 * numeric_ok + 0.1 * band_ok + 0.05 * lag_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)}