from dataclasses import dataclass from typing import Dict, Any, List import re REQ = [ "combination_coherence_score", "dominant_response_basin", "cross_system_alignment_vector", "early_incoherence_flags", "net_functional_shift", ] @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()) <= 950 hits = sum(1 for k in REQ if k in p) coh = _f(p, "combination_coherence_score") num_ok = int(coh is not None and 0 <= coh <= 1) basin_ok = int("dominant_response_basin" in p) vec_ok = int("cross_system_alignment_vector" in p and ":" in p) flags_ok = int("early_incoherence_flags" in p) shift_ok = int("net_functional_shift" in p) raw = ( 0.18 * int(words_ok) + 0.42 * (hits / len(REQ)) + 0.18 * num_ok + 0.08 * basin_ok + 0.07 * vec_ok + 0.03 * flags_ok + 0.04 * shift_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)}