Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.2 — Keyword Search First
LLM Engineering · Example 2 of 2

TF-IDF scores you can check by hand

N = 8 documents. Scores are rounded to four decimals before sorting, so the order is the same in all three languages.

An inverted index lists, for every word, the documents that contain it. TF-IDF scores a match higher when the word is frequent in the document and rare across all documents. This simple search is a strong baseline that real systems still use.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write idf(n_docs, df) returning ln(n_docs / df), and print it to 4 decimals for df = 1, 2, 4 and 8 with 8 documents.

02

Write tf_idf(word, words, n_docs, df) for one word in one document (tf times idf). Use 8 documents; the word appears twice in the document and in only 1 document overall. Print to 4 decimals.

Explain it without notes

01

What is an inverted index and why is it faster than scanning every document?

02

Explain tf and idf, and why their product is a useful score.

03

Name two limits of plain keyword search and one fix for each.

04

Why do you need an explicit tie-breaker when sorting by score?

TF-IDF scores you can check by hand
Sign in to run this example in your browser.

Expected output

query: how many days for shipping
  how: in 0 of 8 documents, idf 0.0000
  many: in 0 of 8 documents, idf 0.0000
  days: in 3 of 8 documents, idf 0.9808
  for: in 2 of 8 documents, idf 1.3863
  shipping: in 1 of 8 documents, idf 2.0794
6.1205  shipping
2.3671  returns
1.3863  payment
0.9808  repairs