File size: 992 Bytes
63c562f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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