from typing import Any DEFAULT_WEIGHTS = { "semantic": 0.20, "skill": 0.35, "yoe": 0.15, "company": 0.10, "growth": 0.10, "education": 0.10, } def normalize_weights(weights: dict[str, float]) -> dict[str, float]: total = sum(weights.values()) if total == 0: return DEFAULT_WEIGHTS.copy() return {k: v / total for k, v in weights.items()} def rerank_with_weights( match_results: list[dict[str, Any]], weights: dict[str, float], ) -> list[dict[str, Any]]: w = normalize_weights({**DEFAULT_WEIGHTS, **weights}) reranked = [] for item in match_results: components = item.get("component_scores") or {} new_score = sum(w.get(k, 0) * v for k, v in components.items()) reranked.append({**item, "final_score": round(new_score, 4), "weights_used": w}) reranked.sort(key=lambda x: x["final_score"], reverse=True) for i, item in enumerate(reranked): item["rank"] = i + 1 return reranked