| from dataclasses import dataclass |
| from typing import Dict, Any, List |
| import re |
|
|
| REQ = [ |
| "coupling_strength_index", |
| "expected_coupling_band", |
| "confidence_score", |
| ] |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def _float01(text: str, key: str) -> bool: |
| return bool(re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", text)) |
|
|
| def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: |
| p = (prediction or "").lower() |
| words_ok = len(p.split()) <= 600 |
|
|
| hits = sum(1 for k in REQ if k in p) |
| floats_ok = sum(1 for k in REQ if _float01(p, k)) |
| has_band = "-" in p or "band" in p |
|
|
| raw = ( |
| 0.30 * int(words_ok) + |
| 0.40 * (hits / len(REQ)) + |
| 0.20 * (floats_ok / len(REQ)) + |
| 0.10 * int(has_band) |
| ) |
|
|
| return ScoreResult( |
| score=min(1.0, raw), |
| details={"id": sample.get("id"), "hits": hits, "floats_ok": floats_ok}, |
| ) |
|
|
| 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)} |
|
|