from dataclasses import dataclass from typing import Dict, Any, List GRADIENTS = {"flat", "shallow", "moderate", "moderate_to_steep", "steep", "flat_to_positive"} @dataclass class ScoreResult: score: float details: Dict[str, Any] def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: p = (prediction or "").lower() words_ok = len(p.split()) <= 800 has_traj = "trajectory" in p or "stressed" in p or "decoupling" in p or "collapse" in p has_gradient = any(g in p for g in GRADIENTS) or "gradient" in p has_levers = ">" in p or "rank" in p or "leverage" in p has_gain = "coherence" in p and ("gain" in p or "medium" in p or "high" in p or "low" in p) has_tradeoffs = "tradeoff" in p or "cost" in p or "risk" in p has_monitor = "monitor" in p or "indicator" in p or "metric" in p raw = ( 0.15 * int(words_ok) + 0.20 * int(has_traj) + 0.20 * int(has_gradient) + 0.20 * int(has_levers) + 0.10 * int(has_gain) + 0.10 * int(has_tradeoffs) + 0.05 * int(has_monitor) ) return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id")}) 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)}