← Lesson
Reciprocal Rank Fusion
Joeven
Run
Reset
Python loads on first run
from collections import defaultdict def rrf(*ranked_lists, k=60, n=3): scores = defaultdict(float) for lst in ranked_lists: for rank, doc_id in enumerate(lst, start=1): scores[doc_id] += 1.0 / (k + rank) return sorted(scores.items(), key=lambda kv: kv[1], reverse=True)[:n] keyword = ["a", "c", "b"] vector = ["c", "b", "a"] print("rrf", rrf(keyword, vector)) print("keyword only", rrf(keyword)) print("vector only", rrf(vector)) # Wider pools before cutting kw20 = ["inv-17", "cash-blog", "oom"] vec20 = ["cash-blog", "oom", "inv-17"] print("wide", rrf(kw20, vec20, n=2))
Run to execute this in your browser. Nothing is sent to a server.