Term Frequency › Worked Example

Michael BrenndoerferMarch 30, 20253 min read
Language AI HandbookAll chapters
Part 2 of 67Classical Text Representations
Chapter 11Term Frequency › Worked Example
Reading section 8 of 17 · Worked Example · 3 minSection 8 of 17 · 3 min

Book contents

Section 8 of 17 · 3 min read

Term Frequency › Worked ExampleOne section · Sections in this chapter8 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through all five variants on a small example to build concrete intuition. Consider two documents:

  • Doc A: "the cat sat on the mat the cat" (8 tokens)
  • Doc B: "the dog chased the cat across the yard the dog" (10 tokens)

The vocabulary is: {across, cat, chased, dog, mat, on, sat, the, yard}

Raw counts:

Raw term counts for two sample documents over a 9-term vocabulary.
TermDoc ADoc B
across01
cat21
chased01
dog02
mat10
on10
sat10
the34
yard01

Applying each weighting scheme to the term "the":

  • Raw: Doc A = 3, Doc B = 4
  • Log: Doc A = 1+ln⁡(3)≈2.101 + \ln(3) \approx 2.10, Doc B = 1+ln⁡(4)≈2.391 + \ln(4) \approx 2.39
  • Boolean: Doc A = 1, Doc B = 1 (both contain "the")
  • Augmented (K=0.5K=0.5): In Doc A, the maximum count is 3 (for "the"), so tfaug=0.5+0.5⋅(3/3)=1.0\text{tf}_{\text{aug}} = 0.5 + 0.5 \cdot (3/3) = 1.0. In Doc B, the maximum count is 4 (also "the"), so tfaug=0.5+0.5⋅(4/4)=1.0\text{tf}_{\text{aug}} = 0.5 + 0.5 \cdot (4/4) = 1.0. Both get weight 1.0 since "the" is the dominant term in both.
  • L2 normalization: Divide each raw count vector by its Euclidean norm before comparison.

Notice how augmented TF reveals that "the" dominates both documents equally (weight 1.0 in both), while "cat" gets weight 0.5+0.5⋅(2/3)≈0.8330.5 + 0.5 \cdot (2/3) \approx 0.833 in Doc A and 0.5+0.5⋅(1/4)=0.6250.5 + 0.5 \cdot (1/4) = 0.625 in Doc B, correctly showing that "cat" is proportionally more important in Doc A.

Now let's compute the L2 norms for these two documents:

∥vA∥2=02+22+02+02+12+12+12+32+02=4+1+1+1+9=16=4\|\mathbf{v}_A\|_2 = \sqrt{0^2 + 2^2 + 0^2 + 0^2 + 1^2 + 1^2 + 1^2 + 3^2 + 0^2} = \sqrt{4 + 1 + 1 + 1 + 9} = \sqrt{16} = 4 ∥vB∥2=12+12+12+22+02+02+02+42+12=1+1+1+4+16+1=24≈4.90\|\mathbf{v}_B\|_2 = \sqrt{1^2 + 1^2 + 1^2 + 2^2 + 0^2 + 0^2 + 0^2 + 4^2 + 1^2} = \sqrt{1+1+1+4+16+1} = \sqrt{24} \approx 4.90

After normalization, the "cat" entry in Doc A becomes 2/4=0.52/4 = 0.5, and in Doc B it becomes 1/4.90≈0.2041/4.90 \approx 0.204. So the L2 representation also correctly reflects that "cat" plays a larger role in Doc A than in Doc B, and does so without requiring any free parameter like KK.

This worked example illustrates a key insight: all five variants agree qualitatively on the relative importance of terms within each document, but they differ in how much weight they assign to high-count terms like "the" versus low-count terms like "sat." The choice of variant affects the geometry of the document space, which in turn affects the behavior of any downstream classifier or retrieval system.