Concepts / Term-Based Retrieval Models

Term-Based Retrieval Models

Term frequency, written f(t, D), counts how many times a term appears in one document, and a higher count suggests the document is more relevant to that term.

  • Programming

Choosing Documents Under a Context Limit

Imagine asking a language model a question and giving it access to a large pile of documents to read first. A model can only take a limited amount of text into its context at once, so you cannot hand it everything. You need a rule for choosing which documents to include. This article covers two numbers that information retrieval systems use to make that choice, and a way of combining them into one score.

What do you think happens?

Your query is 'Easy-to-follow recipes for Vietnamese food to cook at home'. Which words should a retrieval rule concentrate on: the small words such as for and at, or the more specific words such as vietnamese and recipes?

  • The small words for and at, because they appear in the query more often

  • The specific words vietnamese and recipes, because they are more selective across documents

Reveal answer
Answer:

The specific words vietnamese and recipes.

Small words like for and at are likely to show up in most documents, so finding them tells you very little about any one document. Words like vietnamese are more selective, so a term that appears in only a few documents is more informative than one that appears almost everywhere.

The starting point is a simple heuristic. When many documents contain the term you care about, include the ones that mention it most often. The reasoning is that a document which repeats a term many times is probably more relevant to that term than one that mentions it once. Before we can use that rule, we need a precise name for the repetition and a way to count it.

Counting Term Frequency in One Document

Definition

Term frequency, written f(t, D), counts how many times a term t appears in one document D. A higher count suggests the document is more relevant to that term.

TF is a count that depends on only two things: the term and the one document you are looking at. If the word recipes appears three times in a particular document, then f(recipes, D) is 3 for that document. A different document might give a different count for the same word. Because the count is computed per document, it can be used directly as a ranking signal: a larger count means the document is more relevant to that term.

Worked example

Counting TF for two documents

The term recipes appears in two different documents. Document A contains the word recipes 3 times, and Document B contains it once. What is f(recipes, D) for each document?

  1. 1
    Count within Document A

    Scan Document A and count each occurrence of recipes. There are 3 occurrences, so f(recipes, A) = 3.

  2. 2
    Count within Document B

    Scan Document B and count each occurrence of recipes. There is 1 occurrence, so f(recipes, B) = 1.

  3. 3
    Compare the counts

    The same word gives different TF values in different documents. By the rule that a larger count suggests greater relevance, Document A is the stronger match for recipes.

Result

f(recipes, A) = 3 and f(recipes, B) = 1, so Document A ranks higher for this term based on TF alone.

Why Rare Terms Carry More Weight

A term that appears almost everywhere is less informative than one that appears in only a few documents. Finding the word for in a document tells you very little, because almost every document contains it. Finding vietnamese in a document tells you much more, because few documents do. Inverse document frequency turns this intuition into a number.

Definition

Inverse document frequency, IDF, is computed from the total number of documents N and the number C(t) of documents that contain the term t. It divides N by C(t) and takes a logarithm of that ratio. The higher a term's IDF, the more important it is, so IDF rises when a term becomes rarer across the collection and falls when the term becomes common.

Rule

In this formula, N is the total number of documents and C(t) is the number of documents that contain t. The source writes the value as a logarithm of the ratio N divided by C(t), but it does not state which base the logarithm uses, so this article does not assume one. The source's own worked example uses the plain ratio $\frac{N}{C(t)}$, which is the part to focus on first. Whatever the base, the logarithm increases as its input increases, so a term found in fewer documents still gets the higher IDF.

Documentscontaining term1 of 10IDF10Documentscontaining term5 of 10IDF2N / C(t) = 10 / 1N / C(t) = 10 / 5
If a term appears in 1 of 10 documents, its IDF is larger than the IDF of a term that appears in 5 of 10 documents. The rare term receives the larger weight.
Worked example

Computing IDF for a rare and a common term

A collection has N = 10 documents. The term vietnamese appears in 1 document, and the term recipes appears in 5 documents. Using the plain ratio N divided by C(t), what is the IDF of each term?

  1. 1
    Identify N and C(t) for vietnamese

    N = 10 and C(vietnamese) = 1.

  2. 2
    Compute the ratio for vietnamese

    Divide N by C(t): 10 / 1 = 10. The IDF of vietnamese is 10.

  3. 3
    Identify N and C(t) for recipes

    N = 10 and C(recipes) = 5.

  4. 4
    Compute the ratio for recipes

    Divide N by C(t): 10 / 5 = 2. The IDF of recipes is 2.

  5. 5
    Compare the two values

    The rarer term, vietnamese, has the larger IDF. The term in one document of ten gets the larger value, and the term in five documents gets the smaller value.

Result

IDF(vietnamese) = 10 and IDF(recipes) = 2. The rarer term receives the larger weight.

Counting Documents for Each Term

To get C(t), you need to know which documents contain each term. A simple way to picture this is a mapping from each term to the documents that list it. Once you have that mapping, the number of documents containing a term is just the number of entries attached to it. The index below is a small teaching example with N = 10 documents, where vietnamese appears in one document and recipes appears in five.

vietnameseD1 (count 1)recipesD1, D2, D3, D4, D5 (count5)forD1 to D10 (count 10)
Each term maps to the documents that contain it. Counting the entries gives C(t), which is the value used in the IDF ratio.

Reading the index this way makes the rarity idea concrete. The term for has the longest list of documents, so its C(t) is largest and its IDF is smallest. The term vietnamese has the shortest list, so it receives the largest IDF.

Combining TF and IDF into a Score

TF-IDF is an algorithm that combines term frequency and inverse document frequency. It produces a score for a document D with respect to a query Q. The query is treated as a list of terms t_1, t_2, and so on up to t_q. For each query term, you multiply that term's IDF by the term's frequency in the document, and then add up those products across all the query terms.

Rule

Read this formula from the inside out. For each query term t_i, take its IDF, which reflects how rare the term is across the collection, and multiply it by f(t_i, D), which reflects how often the term appears in document D. Each term therefore contributes a product that rewards both being rare overall and being repeated in this particular document. The summation symbol means you add these contributions together for every term in the query. The source calls this a naive definition, which signals that it is the simplest way to combine the two measures, not a final or tuned one.

vietnamesequery termTF = 1f(vietnamese, D)Contribution = 10IDF x TF = 10 x 1Score = 12sum of contributionsIDF = 10N / C(t) = 10 / 1recipesquery termContribution = 2IDF x TF = 2 x 1TF = 1f(recipes, D)IDF = 2N / C(t) = 10 / 5count in Dcount across Nmultiplymultiplycount in Dcount across Nmultiplymultiplyaddadd
For each query term, TF comes from counting inside the document and IDF comes from counting documents across the collection. The two meet in a multiplication, and the per-term contributions are summed into the final score.
Worked example

Scoring one document for a two-term query

Query Q contains the terms vietnamese and recipes. Document D contains vietnamese once and recipes once. The collection has N = 10 documents, with C(vietnamese) = 1 and C(recipes) = 5. Using the plain ratio, what is score(D, Q)?

  1. 1
    Trace the first term

    For vietnamese, IDF = 10 / 1 = 10 and f(vietnamese, D) = 1. The contribution is 10 x 1 = 10.

  2. 2
    Trace the second term

    For recipes, IDF = 10 / 5 = 2 and f(recipes, D) = 1. The contribution is 2 x 1 = 2.

  3. 3
    Sum the contributions

    Add the per-term products across all query terms: 10 + 2 = 12.

Result

score(D, Q) = 12. Most of the score comes from the rare term vietnamese, even though both terms appear once in D.

Now suppose recipes appeared 10 times in D instead of once. Its contribution would rise to 2 x 10 = 20, which would now dominate the score. The score therefore balances how often a term appears in the document against how rare that term is across the collection. TF alone would have favored the repeated word, and IDF alone would have favored the rare word; the product and sum combine both signals.

Mistakes When Reading TF and IDF

  • Common mistake

    Treating a high TF as enough on its own

    What it looks like

    A document contains the word for 20 times, so it looks like the best match for the query.

    Why it fails

    TF counts repetition inside one document. A term that appears in almost every document carries little information, so its large TF is multiplied by a small IDF.

    Do this instead

    Always multiply TF by IDF before comparing documents for a term.

  • Common mistake

    Thinking a rare term is important in every sense

    What it looks like

    Because vietnamese has the largest IDF, it should decide the ranking regardless of how often it appears in the document.

    Why it fails

    The score also depends on f(t, D). A rare term that does not appear in the document contributes zero, since its TF is zero.

    Do this instead

    Remember that the contribution is a product, so both the rarity and the in-document count must be present.

  • Common mistake

    Forgetting that N is the whole collection

    What it looks like

    Computing IDF as 1 divided by C(t) without the total N.

    Why it fails

    IDF depends on the ratio of the total number of documents to the number containing the term. Without N, a term found in every document and a term found in one document cannot be compared.

    Do this instead

    Use IDF = N divided by C(t), with N the full document count.

  • Common mistake

    Confusing f(t, D), which is per document, with C(t), which counts documents across the collection

Practicing the Score Trace

MEDIUM

A collection has N = 20 documents. The term python appears in 4 documents, and the term tutorial appears in 16 documents. Document D contains python 3 times and tutorial 2 times. Using the plain ratio N divided by C(t), compute the TF-IDF score of D for the query python tutorial. Then say which term contributed more to the score and why.

Hints
  • First compute IDF for each term as N divided by C(t): 20 / 4 for python and 20 / 16 for tutorial.
  • Multiply each IDF by the term's count in D, then add the two products.
  • Compare the contributions: a larger product means that term contributed more, even if its TF was lower.
EASY

Explain, in your own words, why a term found in 1 document of 10 can outweigh a term found in 5 documents of 10, even when the rarer term appears only once in the document being scored.

Hints
  • Compare the IDF values first: 10 / 1 against 10 / 5.
  • Think about what a document that contains the rare term tells you that a document containing the common term does not.

Key Takeaways

  • Term frequency f(t, D) counts how many times a term appears in one document, and a higher count suggests greater relevance to that term.
  • Inverse document frequency is computed as the logarithm of N divided by C(t), so terms found in fewer documents receive higher IDF values. The source does not specify the logarithm base.
  • TF-IDF scores a document for a query by multiplying each query term's IDF by its frequency in the document and summing the products across all query terms.
  • Each term's contribution rewards both being rare across the collection and being repeated in the particular document, so neither TF nor IDF alone decides the score.

Related concepts

More concepts starting with T