

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: BM25
incomplete
2: Term Frequency Saturation
incomplete
3: Document Length Normalization
incomplete
4: BM25 Search
incomplete
This lesson's interactive features are locked, please to keep using them
While TF-IDF is a good start, there's a keyword ranking algorithm called Okapi BM25 that performs even better. BM25 addresses three problems with basic TF-IDF:
In basic TF-IDF, IDF is calculated as log(N / df):
The problems with this formula are:
df = 0 (we "solved" this by adding 1 to both the numerator and denominator)0BM25 uses a more stable IDF formula:
IDF = log((N - df + 0.5) / (df + 0.5) + 1)
Let's break down the reasoning behind each piece, starting with the core ratio:
(N - df + 0.5) / (df + 0.5)
This compares the documents that don't contain the term against documents that do contain it.
(N - df + 0.5): Documents without the term, plus smoothing(df + 0.5): Documents with the term, plus smoothingThe smoothing part of this formula is where we add 0.5 to the numerator and denominator, then add 1 to the division result.
0.5? To prevent division by 0. The fancy term for this is Laplace smoothing.1? The final + 1 ensures IDF is always positive, which handles some edge cases.In this lesson we'll focus on the first improvement offered by BM25: better IDF calculation.
bm25_idf_parser = subparsers.add_parser(
"bm25idf", help="Get BM25 IDF score for a given term"
)
bm25_idf_parser.add_argument("term", type=str, help="Term to get BM25 IDF score for")
print(f"BM25 IDF score of '{args.term}': {bm25idf:.2f}")
Run and submit the CLI tests.