Sparse BM25 Score Calculator
BM25 is a ranking function used by search engines to estimate the relevance of a text document to a query. It computes term frequency weighting normalized by document length.
The formula to score a document $D$ for a query $Q$ is:
$\text{Score}(D, Q) = \sum_{q \in Q} \text{IDF}(q) \cdot \frac{f(q, D) \cdot (k1 + 1)}{f(q, D) + k1 \cdot \left(1 - b + b \cdot \frac{|D|}{\text{avgdl}}\right)}$
Where:
- $f(q, D)$ is the frequency of query term $q$ in document $D$.
- $|D|$ is the length of document $D$ in words.
- $\text{avgdl}$ is the average document length in the corpus.
- $k1$ and $b$ are parameters (typically $k1 = 1.5, b = 0.75$).
The inverse document frequency is computed as:
$\text{IDF}(q) = \ln\left(1 + \frac{N - \text{DF}(q) + 0.5}{\text{DF}(q) + 0.5}\right)$
Where $N$ is the total number of documents in the corpus, and $\text{DF}(q)$ is the number of documents containing query term $q$. If $q$ does not appear in any document, $\text{DF}(q) = 0$.
Tasks
Implement the function compute_bm25_score(query, document, corpus, k1, b) where query and document are lists of words, and corpus is a list of lists of words. Return the float score rounded to 4 decimal places.