BM25 Ranking

Medium
NLP

Implement the BM25 ranking function to calculate document scores for a query in an information retrieval context. BM25 is an advanced variation of TF-IDF that incorporates term frequency saturation, document length normalization, and a configurable penalty for document length effects.

Examples

Example 1:
Input: corpus = [['the', 'cat', 'sat'], ['the', 'dog', 'ran'], ['the', 'bird', 'flew']], query = ['the', 'cat']
Output: [0.693, 0., 0. ]
Explanation: BM25 calculates scores for each document in the corpus by evaluating how well the query terms match each document while considering term frequency saturation and document length normalization.

Starter Code

import numpy as np
from collections import Counter

def calculate_bm25_scores(corpus, query, k1=1.5, b=0.75):
	# Your code here
	pass
	return np.round(scores,3)
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews