| from dataclasses import dataclass |
| from typing import Dict, Any, List |
| import re |
|
|
| REQ = [ |
| "optimal_endpoint_mix", |
| "rwe_generation_plan", |
| "pricing_stability_zone", |
| "access_probability", |
| "restriction_burden_forecast", |
| "coverage_stability_horizon", |
| "confidence_score", |
| ] |
|
|
| ZONES = ["wide", "moderate", "narrow"] |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def _float01(p: str, key: str) -> bool: |
| return bool(re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", p)) |
|
|
| def _zone_ok(p: str) -> bool: |
| m = re.search(r"pricing_stability_zone\s*[:=]\s*([a-z\-]+)", p) |
| return bool(m and m.group(1) in ZONES) |
|
|
| def _horizon_ok(p: str) -> bool: |
| return bool(re.search(r"coverage_stability_horizon\s*[:=]\s*\d+(\.\d+)?\s*(m|mo|mos|y|yr|yrs)\b", p)) |
|
|
| 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) |
| ap_ok = int(_float01(p, "access_probability")) |
| conf_ok = int(_float01(p, "confidence_score")) |
| zone_ok = int(_zone_ok(p)) |
| hz_ok = int(_horizon_ok(p)) |
|
|
| raw = ( |
| 0.22 * int(words_ok) + |
| 0.40 * (hits / len(REQ)) + |
| 0.14 * ap_ok + |
| 0.08 * conf_ok + |
| 0.08 * zone_ok + |
| 0.08 * hz_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)} |
|
|