01-02 Counting What the Model Sees
Why this matters
Token-frequency counting is the first measurable signal a language model can extract from data.
Intuition first (no jargon)
A frequency table is the model's first memory: it records what appears often.
Code walkthrough
jsexport function countChars(text) { const counts = {}; // fill counts return counts; }
Your task
Implement countChars(text).
- Return an object mapping each character to count.
- Include spaces as characters.
- Return
{}for empty input.
Hints
- Iterate with
for (const ch of text). - Initialize with
counts[ch] = (counts[ch] || 0) + 1. - Keep the function pure.
Check your thinking
- Why might spaces matter for language?
- What does a high count mean?
- Does counting alone preserve order?
Stretch (optional)
Add countWords(text) and compare character vs word counts.
Likely test focus
- Exact counts for toy strings.
- Handles empty strings.
- Includes whitespace tokens.
What should improve
You can now compute reproducible corpus statistics that drive simple probabilistic baselines.
Bridge to next lesson
Next lesson: add weighted randomness for generation.