ClarusC64 commited on
Commit
a14d6ba
·
verified ·
1 Parent(s): a8bc069

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +61 -0
scorer.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+ from typing import Dict, Any, List
3
+ import re
4
+
5
+ REQ = [
6
+ "regime_coherence_score",
7
+ "dominant_driver_label",
8
+ "cross_asset_alignment_index",
9
+ "dispersion_vs_concentration_index",
10
+ "regime_stability_band",
11
+ ]
12
+
13
+ LABELS = [
14
+ "macro_rates",
15
+ "risk_on_liquidity",
16
+ "growth_beta",
17
+ "concentration_equity",
18
+ "shock_repricing",
19
+ "uncertain_transition",
20
+ ]
21
+
22
+ BANDS = ["stable", "moderate", "unstable", "fragile"]
23
+
24
+ @dataclass
25
+ class ScoreResult:
26
+ score: float
27
+ details: Dict[str, Any]
28
+
29
+ def _has_float(p: str) -> bool:
30
+ return bool(re.search(r"\b0\.\d+\b", p)) or "1.0" in p
31
+
32
+ def _has_driver(p: str) -> bool:
33
+ return any(l in p for l in LABELS)
34
+
35
+ def _has_band(p: str) -> bool:
36
+ return any(b in p for b in BANDS)
37
+
38
+ def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
39
+ p = (prediction or "").lower()
40
+ words_ok = len(p.split()) <= 900
41
+
42
+ hits = sum(1 for k in REQ if k.lower() in p)
43
+
44
+ has_numbers = _has_float(p)
45
+ has_driver = _has_driver(p)
46
+ has_band = _has_band(p)
47
+
48
+ raw = (
49
+ 0.20 * int(words_ok) +
50
+ 0.50 * (hits / len(REQ)) +
51
+ 0.10 * int(has_numbers) +
52
+ 0.10 * int(has_driver) +
53
+ 0.10 * int(has_band)
54
+ )
55
+
56
+ return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits})
57
+
58
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
59
+ if not results:
60
+ return {"mean": 0.0, "n": 0}
61
+ return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}