TF-IDF
The scoring that ran search for decades — and still seeds hybrid retrieval today. It rewards a word that is frequent in one document but rare across the corpus, and shrugs off words that appear everywhere. No numpy, just a logarithm.
The problem
Implement TF-IDF: the weight of a term in one document of a corpus. The corpus is a list of documents, each a list of tokens; doc_index selects the document. Term frequency is the term's share of that document: tf = count(term in doc) / len(doc). Inverse document frequency uses how many documents contain the term (df) out of N total: idf = log(N / df). Return tf · idf. If the term is absent from the document, its weight is 0.
corpus=[['a','b'],['a','c'],['a','d']], doc_index=0, term='a'0.0corpus=[['a','a','b','c'],['d'],['e']], doc_index=0, term='a'≈0.5493corpus=[['a','b'],['c']], doc_index=0, term='z'0.0- Term frequency:
tf = count(term in doc) / len(doc). - Document frequency
df= number of documents containing the term;idf = log(N / df)withN = len(corpus)(natural log). - If the term is absent from the document, return
0(so you never divide bydf = 0). - Pyodide has no numpy — use the
mathstdlib and plain lists only.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement tf_idf(corpus, doc_index, term). Return tf · idf for the term in the selected document — the term's share of that doc, scaled by how rare it is across the corpus.
- tf = (times the term appears in the doc) / (number of tokens in the doc).
- If tf is 0 the whole weight is 0 — return early, and you avoid a df = 0 divide.
- df = how many documents contain the term at all; idf = log(N / df) with N = len(corpus).
- A term in every document has df = N, so idf = log(1) = 0 — it carries no weight.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.