"""Reciprocal Rank Fusion for combining multiple ranked-id lists.""" def reciprocal_rank_fusion(rankings, k=60): """rankings: list of ranked-id lists (best first). Returns a single fused ranking.""" scores = {} for ranking in rankings: for rank, doc_id in enumerate(ranking, start=1): scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank) return sorted(scores, key=scores.get, reverse=True)