Topic Modelling with BERTopic in Python: Pages and Short Text (Notebook)
BERTopic groups documents by meaning using embeddings, then labels each group with its most distinctive words. It is flexible enough to be confusing, so this free Google Colab notebook builds it up step by step: the simplest possible run first, then each component chosen on purpose. Part B tests it where topic models struggle most, on short text like page titles.
Who it’s for
SEOs and content analysts clustering pages, titles or queries into topics, and anyone moving from LDA to an embedding-based approach. It runs on a CPU with no API keys. The embedding model, about 90 MB, downloads from Hugging Face on the first run.
How to use it
- Open the notebook in Colab and run Part A on the demo dataset (the same 20 Newsgroups sample as the LDA notebook), or load your own crawl export.
- Run the one-line BERTopic model to get a baseline.
- Build your own pipeline: pre-computed all-MiniLM-L6-v2 embeddings, UMAP, HDBSCAN, a CountVectorizer, c-TF-IDF that down-weights frequent words, and KeyBERT-inspired labels.
- Inspect the topic map, bar chart and hierarchy, then reduce outliers and merge topics that were split too finely.
- Get a topic mix per page and export.
- Run Part B to compare LDA, NMF and BERTopic on titles, and try seed words and custom labels.
What you get
- A page-level CSV with each page’s dominant topic and its full topic mix
- Topic information with keywords and labels, plus HTML visualisations
- Outlier reduction, so far fewer pages end up unassigned
- A head-to-head of three models on short text, scored with c_v coherence against the same reference corpus
- Examples of guided topic modelling with seed words, merging and custom labels
Why BERTopic over LDA
BERTopic finds the number of topics itself and works from meaning, so pages that use different words for the same idea can still land together. The trade-off is more settings to understand, which is exactly what Part A walks through one component at a time.
Limits
HDBSCAN settings decide how many topics you get, so tune them for your data, and set a random state on UMAP if you need the same topics on every run. On the demo titles, BERTopic matched NMF’s coherence and beat LDA, and still left 7% of titles unassigned: no model is good on three-word documents. De-duplicate short texts first, or the duplicates form topics of their own.
Read Topic Modelling for SEO with BERTopic for the full guide. A guided course edition of this lab, with demo data and a walkthrough, is part of Introduction to Machine Learning for SEO.
What’s included
- BERTopic 0.17 built step by step: embeddings, UMAP, HDBSCAN, c-TF-IDF, KeyBERT-inspired labels
- Outlier reduction and topic merging, with a topic mix per page
- Head-to-head with LDA and NMF on short titles, scored with c_v coherence
- Guided topic modelling with seed words and custom labels
- Runs on CPU with no API keys
Where this resource is used on MLforSEO
Walkthroughs on the blog
More content analysis resources
- Coding Script or NotebookWhisper API Transcription Notebook: Bulk Audio, Long Files and SRT Captions
- Coding Script or NotebookTopic Modelling with LDA in Python: Tuned, Visualised and Linked (Notebook)
- Coding Script or NotebookScreaming Frog Custom Extraction Helper: Selector Tester and Dataset Cleaner
- GuideEntity Extraction Benchmark: Generative AI vs NLP APIs (Raw Results Sheet)
- GuideContent Transformation Approaches in ML: 16 Types Compared (Guide)
- No-Code TemplateNo-code LDA Topic Modeling: Website Content Clustering Output Organiser (Google Sheets Template)
See all 29 content analysis resources →See all 32 keyword research resources →See all 73 coding script or notebook resources →
