concept
Pure Python category and word counts
Use a counter initialized to zero and iterate over records, incrementing when a field matches. For text counts, update counts[word] = counts.get(word, 0) + 1 for each whitespace-delimited piece. Empty inputs yield zero or an empty dictionary. Lowercasing and whitespace splitting are not full linguistic tokenization.
Formal Syntax & Structure
artificial-intelligence syntaxartificial-intelligence
Pure Python category and word countsWorking Example
Example Snippetartificial-intelligence
def word_counts(text): counts = {} for word in text.lower().split(): counts[word] = counts.get(word, 0) + 1 return counts
counts = word_counts("Blue box blue bag")print(counts["blue"])print(counts.get("green", 0))print(word_counts(""))