Hybrid Search: Normalise and Blend Scores

easy
Unit 3 · RAG I0/3 to milestone

BM25 scores might range from 0 to 30 while cosine similarities sit between 0.6 and 0.9, so you can't just add them. Min-max normalise each retriever's scores to [0, 1] first, then blend:

score(d)=α⋅dense(d)+(1−α)⋅sparse(d)\text{score}(d) = \alpha \cdot \text{dense}(d) + (1 - \alpha) \cdot \text{sparse}(d)

A document missing from one retriever's results gets 0 for that retriever.

Implement min_max(scores) (a dict of doc → score; if all scores are equal, map them all to 1.0; an empty dict gives an empty dict) and hybrid_rank(dense, sparse, alpha=0.5, k=None), returning (doc, score) pairs sorted by blended score, highest first (ties by doc id), truncated to k if given.

#rag#retrieval#ranking2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Hybrid Search: Normalise and Blend Scores · TechGradify