Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
|
| 5 |
+
@dataclass
|
| 6 |
+
class ScoreResult:
|
| 7 |
+
score: float
|
| 8 |
+
details: Dict[str, Any]
|
| 9 |
+
|
| 10 |
+
def _has(text: str, pats: List[str]) -> bool:
|
| 11 |
+
t = (text or "").lower()
|
| 12 |
+
return any(re.search(p, t) for p in pats)
|
| 13 |
+
|
| 14 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 15 |
+
pred = (prediction or "").strip()
|
| 16 |
+
words = len([w for w in re.split(r"\s+", pred) if w])
|
| 17 |
+
|
| 18 |
+
max_words = 180
|
| 19 |
+
m = re.search(r"under\s+(\d+)\s+words", (sample.get("constraints") or "").lower())
|
| 20 |
+
if m:
|
| 21 |
+
max_words = int(m.group(1))
|
| 22 |
+
length_ok = 1 if words <= max_words else 0
|
| 23 |
+
|
| 24 |
+
# Requires explicit correspondence classification + mapping
|
| 25 |
+
classify = 1 if _has(pred, [r"\bunsupported\b", r"\bpartial\b", r"\bsupported\b"]) else 0
|
| 26 |
+
|
| 27 |
+
mapping_terms = [
|
| 28 |
+
r"\binclusion\b", r"\bexcluded\b", r"\bonly\b", r"\bnot studied\b",
|
| 29 |
+
r"\bage\b", r"\bseverity\b", r"\bsetting\b", r"\bsubgroup\b",
|
| 30 |
+
r"\bbiomarker\b", r"\bnyha\b", r"\blvef\b", r"\bsecond[- ]line\b",
|
| 31 |
+
r"\bnon[- ]inferior\b"
|
| 32 |
+
]
|
| 33 |
+
mapping = 1 if _has(pred, mapping_terms) else 0
|
| 34 |
+
|
| 35 |
+
# Penalize approval-like language without caveats
|
| 36 |
+
overclaim_bad = [r"\bshould be indicated\b", r"\bapprove\b", r"\beffective in all\b"]
|
| 37 |
+
overclaim = 1 if _has(pred, overclaim_bad) else 0
|
| 38 |
+
|
| 39 |
+
# Suggested correction
|
| 40 |
+
action = 1 if _has(pred, [r"\bshould state\b", r"\brevise\b", r"\brestrict\b", r"\bwording\b"]) else 0
|
| 41 |
+
|
| 42 |
+
raw = (
|
| 43 |
+
0.30 * length_ok +
|
| 44 |
+
0.30 * classify +
|
| 45 |
+
0.25 * mapping +
|
| 46 |
+
0.10 * action +
|
| 47 |
+
0.05 * (0 if overclaim else 1)
|
| 48 |
+
)
|
| 49 |
+
final = max(0.0, min(1.0, raw))
|
| 50 |
+
|
| 51 |
+
return ScoreResult(
|
| 52 |
+
score=final,
|
| 53 |
+
details={
|
| 54 |
+
"word_count": words,
|
| 55 |
+
"max_words": max_words,
|
| 56 |
+
"length_ok": length_ok,
|
| 57 |
+
"classification": classify,
|
| 58 |
+
"mapping": mapping,
|
| 59 |
+
"action": action,
|
| 60 |
+
"overclaim_penalty": overclaim,
|
| 61 |
+
"correspondence_pressure": sample.get("correspondence_pressure"),
|
| 62 |
+
"domain": sample.get("domain"),
|
| 63 |
+
},
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 67 |
+
if not results:
|
| 68 |
+
return {"mean": 0.0, "n": 0}
|
| 69 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|