CSV and Text
Count Token Frequencies
Split a sentence into words with .split() and count how many times each
word appears. The trace shows counts seeding new words at 1 and
incrementing on repeats — the word 'the' appears three times and 'cat'
twice.
By hand
Split the text into a word list with .split(), then walk the words with the
same seed-or-increment dict pattern from frequency-count (ch05).
naive.py
Replay: real traced execution (multi-file project)
text = 'the cat sat on the mat the cat'
words = text.split()
counts = {}
for w in words:
if w in counts:
counts[w] = counts[w] + 1
else:
counts[w] = 1
print('RESULT:', {k: counts[k] for k in sorted(counts)})
text ← 'the cat sat on the mat the cat'
1text = 'the cat sat on the mat the cat'2words = text.split()values this step'the cat sat on the mat the cat'textwords ← ['the', 'cat', 'sat', 'on', 'the', 'mat', 'the', 'cat']
1text = 'the cat sat on the mat the cat'2words = text.split()3counts = {}values this step['the', 'cat', 'sat', 'on', 'the', 'mat', 'the', 'cat']wordscounts ← {}
2words = text.split()3counts = {}4for w in words:values this step{}countsw ← 'the'
3counts = {}4for w in words:5 if w in counts:values this step'the'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 1}
7 else:8 counts[w] = 19print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{} → {'the': 1}countsw ← 'cat'
3counts = {}4for w in words:5 if w in counts:values this step'the' → 'cat'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 1, 'cat': 1}
7 else:8 counts[w] = 19print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'the': 1} → {'the': 1, 'cat': 1}countsw ← 'sat'
3counts = {}4for w in words:5 if w in counts:values this step'cat' → 'sat'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 1, 'cat': 1, 'sat': 1}
7 else:8 counts[w] = 19print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'the': 1, 'cat': 1} → {'the': 1, 'cat': 1, 'sat': 1}countsw ← 'on'
3counts = {}4for w in words:5 if w in counts:values this step'sat' → 'on'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 1, 'cat': 1, 'sat': 1, 'on': 1}
7 else:8 counts[w] = 19print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'the': 1, 'cat': 1, 'sat': 1} → {'the': 1, 'cat': 1, 'sat': 1, 'on': 1}countsw ← 'the'
3counts = {}4for w in words:5 if w in counts:values this step'on' → 'the'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 2, 'cat': 1, 'sat': 1, 'on': 1}
5if w in counts:6 counts[w] = counts[w] + 17else:values this step{'the': 1, 'cat': 1, 'sat': 1, 'on': 1} → {'the': 2, 'cat': 1, 'sat': 1, 'on': 1}countsw ← 'mat'
3counts = {}4for w in words:5 if w in counts:values this step'the' → 'mat'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
7 else:8 counts[w] = 19print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'the': 2, 'cat': 1, 'sat': 1, 'on': 1} → {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}countsw ← 'the'
3counts = {}4for w in words:5 if w in counts:values this step'mat' → 'the'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 3, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
5if w in counts:6 counts[w] = counts[w] + 17else:values this step{'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1} → {'the': 3, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}countsw ← 'cat'
3counts = {}4for w in words:5 if w in counts:values this step'the' → 'cat'wif w in counts:
4for w in words:5 if w in counts:6 counts[w] = counts[w] + 1counts ← {'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1}
5if w in counts:6 counts[w] = counts[w] + 17else:values this step{'the': 3, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1} → {'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1}countsfor w in words:
3counts = {}4for w in words:5 if w in counts:stdout ← RESULT: {'cat': 2, 'mat': 1, 'on': 1, 'sat': 1, 'the': 3}
8 counts[w] = 19print('RESULT:', {k: counts[k] for k in sorted(counts)})values this stepRESULT: {'cat': 2, 'mat': 1, 'on': 1, 'sat': 1, 'the': 3}stdout
The Pythonic way
Counter(text.split()) tokenises and tallies in one call. The result is a
Counter (a dict subclass) with the same counts.
library.py
from collections import Counter
text = 'the cat sat on the mat the cat'
counts = Counter(text.split())
print('RESULT:', {k: counts[k] for k in sorted(counts)})
RESULT: {'cat': 2, 'mat': 1, 'on': 1, 'sat': 1, 'the': 3}
Implementation notes
- The mechanism is identical to
python-data-basics/frequency-count(ch05). The distinction is the input: ch05 counts a pre-split label list; this lesson first tokenises a raw text string with.split(). Real NLP pipelines add lowercasing and punctuation stripping before counting. .split()with no argument splits on any whitespace run and ignores leading/trailing whitespace — equivalent to.strip().split().Counter.most_common(n)returns the n highest-frequency tokens, useful for finding stop-words or topic keywords.