Count word frequency in text
Tokenize text and list the most common words using collections.Counter.
- Python
- GreenSnippets
- MIT
- Executed with Python 3.11
import re
from collections import Counter
WORD = re.compile(r"[\w']+", re.UNICODE)
def top_words(text, n=10):
words = (w.lower() for w in WORD.findall(text))
return Counter(words).most_common(n)Example / expected output
top_words("the cat and the hat", 2) # [('the', 2), ('cat', 1)]How it works
Counter does the counting and most_common(n) returns the top entries. The regular expression keeps letters and apostrophes, and lower-casing treats "The" and "the" as one word. Real language processing needs more care (stop words, stemming, non-Latin scripts), but this covers most quick analyses.
Related snippets
- JavaScript Tested
Truncate a string with an ellipsis
Shorten text to a maximum length without cutting in the middle of a word or a Unicode character.
- PHP Tested
Truncate multibyte strings safely in PHP
Shorten UTF-8 text with mb_ functions so accents and emoji are never cut in half.
- Python Tested
Slugify a string
Turn text into a lowercase, hyphenated, ASCII-only slug using only the standard library.
- PHP Tested
Slugify a string in PHP
Create URL-safe lowercase slugs from titles, removing accents and punctuation.

