ClarusC64's picture
Create scorer.py
681c2fe verified
raw
history blame contribute delete
899 Bytes
from dataclasses import dataclass
from typing import Dict, Any, List
REQ = ["coherence", "risk", "pitting", "surface", "protect"]
@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()) <= 900
hits = sum(1 for k in REQ if k in p)
has_numeric = any(c.isdigit() for c in p)
raw = (
0.25 * int(words_ok) +
0.45 * (hits / len(REQ)) +
0.20 * int(has_numeric) +
0.10 * int("time" in p or "horizon" in p)
)
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)}