Command Palette

Search for a command to run...

Hectal
Phase 1: Search Before Vectors
RAG and Vector Search · Exercise 3 of 5 PreviousNext
Intermediate

Compute a BM25 term score

Write idf(n, df) with BM25's form: ln(1 + (n - df + 0.5) / (df + 0.5)).

Write term_score(tf, length, avg_len, idf_value, k1=1.2, b=0.75): idf_value * tf * (k1 + 1) / (tf + k1 * (1 - b + b * length / avg_len)). A term with tf = 0 scores 0.

What Check tests

  1. idf of a rare word
  2. idf stays positive for a word in every passage
  3. The handbook example: monthly in D8
  4. A missing term scores 0
  5. Repetition saturates
  6. Longer passages score less for the same tf

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.