All problems
BM25 Score for a Single Term
Problem
Return the BM25 score of a single term: idf * (tf * (k1 + 1)) / (tf + k1 * (1 - b + b * doc_len / avg_doc_len)), with idf = log(1 + (N - df + 0.5) / (df + 0.5)).
term_freq is the raw count of the term in the document, N is n_docs, and k1 and b default to 1.5 and 0.75. Return 0.0 when the term does not occur.
Hint
The length ratio only enters through the denominator inside 1 - b + b * dl / avgdl.
Test cases
case 1
in: [2,50,50,10,100]
expected: 3.233921799539688
case 2
in: [0,50,50,10,100]
expected: 0
case 3
in: [1,25,50,1,2]
expected: 0.8943834587870262
case 4
in: [3,40,50,20,100,1.2,0]
expected: 2.5059502768094095
Starter codePython
import math
def bm25(term_freq, doc_len, avg_doc_len, df, n_docs, k1=1.5, b=0.75):
# Your code here
pass4 test cases · run your code against them in the editor