Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ = [
|
| 6 |
+
"optimal_endpoint_mix",
|
| 7 |
+
"rwe_generation_plan",
|
| 8 |
+
"pricing_stability_zone",
|
| 9 |
+
"access_probability",
|
| 10 |
+
"restriction_burden_forecast",
|
| 11 |
+
"coverage_stability_horizon",
|
| 12 |
+
"confidence_score",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
ZONES = ["wide", "moderate", "narrow"]
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class ScoreResult:
|
| 19 |
+
score: float
|
| 20 |
+
details: Dict[str, Any]
|
| 21 |
+
|
| 22 |
+
def _float01(p: str, key: str) -> bool:
|
| 23 |
+
return bool(re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", p))
|
| 24 |
+
|
| 25 |
+
def _zone_ok(p: str) -> bool:
|
| 26 |
+
m = re.search(r"pricing_stability_zone\s*[:=]\s*([a-z\-]+)", p)
|
| 27 |
+
return bool(m and m.group(1) in ZONES)
|
| 28 |
+
|
| 29 |
+
def _horizon_ok(p: str) -> bool:
|
| 30 |
+
return bool(re.search(r"coverage_stability_horizon\s*[:=]\s*\d+(\.\d+)?\s*(m|mo|mos|y|yr|yrs)\b", p))
|
| 31 |
+
|
| 32 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 33 |
+
p = (prediction or "").lower()
|
| 34 |
+
words_ok = len(p.split()) <= 950
|
| 35 |
+
|
| 36 |
+
hits = sum(1 for k in REQ if k in p)
|
| 37 |
+
ap_ok = int(_float01(p, "access_probability"))
|
| 38 |
+
conf_ok = int(_float01(p, "confidence_score"))
|
| 39 |
+
zone_ok = int(_zone_ok(p))
|
| 40 |
+
hz_ok = int(_horizon_ok(p))
|
| 41 |
+
|
| 42 |
+
raw = (
|
| 43 |
+
0.22 * int(words_ok) +
|
| 44 |
+
0.40 * (hits / len(REQ)) +
|
| 45 |
+
0.14 * ap_ok +
|
| 46 |
+
0.08 * conf_ok +
|
| 47 |
+
0.08 * zone_ok +
|
| 48 |
+
0.08 * hz_ok
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits})
|
| 52 |
+
|
| 53 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 54 |
+
if not results:
|
| 55 |
+
return {"mean": 0.0, "n": 0}
|
| 56 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|