| from dataclasses import dataclass |
| from typing import Dict, Any, List |
| import re |
|
|
| REQ = [ |
| "crowded_agent_type", |
| "capacity_utilization_score", |
| "reversal_risk_score", |
| "unwind_trigger_conditions", |
| "time_to_capacity_event_hours", |
| ] |
|
|
| AGENTS = ["cta_trend", "options_hedging", "distressed_seller", "market_maker", "fundamental_growth"] |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def _has_float_0_1(p: str) -> bool: |
| return bool(re.search(r"\b0\.\d+\b", p)) or "1.0" in p |
|
|
| def _has_agent(p: str) -> bool: |
| return any(a in p for a in AGENTS) |
|
|
| def _has_time(p: str) -> bool: |
| return "hour" in p or bool(re.search(r"\b\d+\b", p)) |
|
|
| 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_nums = _has_float_0_1(p) |
| has_agent = _has_agent(p) |
| has_time = _has_time(p) |
|
|
| raw = ( |
| 0.20 * int(words_ok) + |
| 0.60 * (hits / len(REQ)) + |
| 0.10 * int(has_nums) + |
| 0.05 * int(has_agent) + |
| 0.05 * int(has_time) |
| ) |
|
|
| 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)} |
|
|