ClarusC64 commited on
Commit
28089cd
·
verified ·
1 Parent(s): 6e9cb33

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +48 -0
scorer.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from dataclasses import dataclass
3
+ from typing import Dict, Any, List
4
+
5
+ BASINS = {
6
+ "basin_a_inflammatory_autonomic",
7
+ "basin_b_mito_metabolic_fatigue",
8
+ "basin_c_neuroimmune_cognitive",
9
+ "basin_d_mast_cell_histamine_like",
10
+ "basin_e_autoimmune_multisystem",
11
+ }
12
+
13
+ @dataclass
14
+ class ScoreResult:
15
+ score: float
16
+ details: Dict[str, Any]
17
+
18
+ def _has(t: str, pats: List[str]) -> bool:
19
+ t = (t or "").lower()
20
+ return any(re.search(p, t) for p in pats)
21
+
22
+ def _extract_ints(text: str) -> List[int]:
23
+ return [int(x) for x in re.findall(r"\b\d{1,3}\b", text) if 0 <= int(x) <= 100]
24
+
25
+ def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
26
+ p = (prediction or "").lower().strip()
27
+ words_ok = len(p.split()) <= 360
28
+
29
+ basin_ok = any(b in p for b in BASINS)
30
+ nums = _extract_ints(p)
31
+ has_score = any(0 <= n <= 100 for n in nums)
32
+
33
+ features_ref = _has(p, [r"top5", r"features", r"pem", r"pots", r"il6", r"ana", r"tryptase", r"esr"])
34
+ evidence_ref = _has(p, [r"because", r"domin", r"pattern", r"defines", r"stable"])
35
+
36
+ raw = (
37
+ 0.30 * int(basin_ok) +
38
+ 0.20 * int(has_score) +
39
+ 0.20 * int(features_ref) +
40
+ 0.20 * int(evidence_ref) +
41
+ 0.10 * int(words_ok)
42
+ )
43
+ return ScoreResult(score=min(1.0, raw), details={"basin_ok": basin_ok, "id": sample.get("id")})
44
+
45
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
46
+ if not results:
47
+ return {"mean": 0.0, "n": 0}
48
+ return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}