Spaces:
Sleeping
Sleeping
Update analytics/recommendation_engine.py
Browse files
analytics/recommendation_engine.py
CHANGED
|
@@ -9,6 +9,70 @@ from models.fair_odds import probability_to_american
|
|
| 9 |
from models.live_fair_simulator_v3 import build_upcoming_simulated_rows
|
| 10 |
from models.opportunity_model import estimate_plate_appearance_probability
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def _lineup_distance_from_slot(slot: str) -> int:
|
| 13 |
s = str(slot or "").strip().lower()
|
| 14 |
|
|
|
|
| 9 |
from models.live_fair_simulator_v3 import build_upcoming_simulated_rows
|
| 10 |
from models.opportunity_model import estimate_plate_appearance_probability
|
| 11 |
|
| 12 |
+
def _apply_opportunity_badges(recommendations: list[dict]) -> list[dict]:
|
| 13 |
+
if not recommendations:
|
| 14 |
+
return recommendations
|
| 15 |
+
|
| 16 |
+
rows = [dict(r) for r in recommendations]
|
| 17 |
+
|
| 18 |
+
for row in rows:
|
| 19 |
+
row["opportunity_badges"] = []
|
| 20 |
+
|
| 21 |
+
def _best_row_index(metric: str) -> int | None:
|
| 22 |
+
best_idx = None
|
| 23 |
+
best_val = None
|
| 24 |
+
|
| 25 |
+
for idx, row in enumerate(rows):
|
| 26 |
+
value = row.get(metric)
|
| 27 |
+
try:
|
| 28 |
+
numeric_value = float(value)
|
| 29 |
+
except Exception:
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
if best_val is None or numeric_value > best_val:
|
| 33 |
+
best_val = numeric_value
|
| 34 |
+
best_idx = idx
|
| 35 |
+
|
| 36 |
+
return best_idx
|
| 37 |
+
|
| 38 |
+
best_overall_idx = _best_row_index("priority_score")
|
| 39 |
+
best_hr_idx = _best_row_index("hr_edge")
|
| 40 |
+
best_hit_idx = _best_row_index("hit_edge")
|
| 41 |
+
best_tb_idx = _best_row_index("tb2p_edge")
|
| 42 |
+
|
| 43 |
+
if best_overall_idx is not None:
|
| 44 |
+
rows[best_overall_idx]["opportunity_badges"].append("BEST OVERALL")
|
| 45 |
+
|
| 46 |
+
if best_hr_idx is not None:
|
| 47 |
+
rows[best_hr_idx]["opportunity_badges"].append("BEST HR EDGE")
|
| 48 |
+
|
| 49 |
+
if best_hit_idx is not None:
|
| 50 |
+
rows[best_hit_idx]["opportunity_badges"].append("BEST HIT EDGE")
|
| 51 |
+
|
| 52 |
+
if best_tb_idx is not None:
|
| 53 |
+
rows[best_tb_idx]["opportunity_badges"].append("BEST TB EDGE")
|
| 54 |
+
|
| 55 |
+
for row in rows:
|
| 56 |
+
tier = str(row.get("recommendation_tier", "") or "").strip().lower()
|
| 57 |
+
|
| 58 |
+
if tier == "bet":
|
| 59 |
+
row["opportunity_badges"].append("BET")
|
| 60 |
+
elif tier == "watch":
|
| 61 |
+
row["opportunity_badges"].append("WATCH")
|
| 62 |
+
|
| 63 |
+
# de-duplicate while preserving order
|
| 64 |
+
seen = set()
|
| 65 |
+
deduped = []
|
| 66 |
+
for badge in row["opportunity_badges"]:
|
| 67 |
+
if badge in seen:
|
| 68 |
+
continue
|
| 69 |
+
seen.add(badge)
|
| 70 |
+
deduped.append(badge)
|
| 71 |
+
|
| 72 |
+
row["opportunity_badges"] = deduped[:3]
|
| 73 |
+
|
| 74 |
+
return rows
|
| 75 |
+
|
| 76 |
def _lineup_distance_from_slot(slot: str) -> int:
|
| 77 |
s = str(slot or "").strip().lower()
|
| 78 |
|