Sub topic 1
What topic modelling does and when to use it
Prerequisites
This chapter assumes you have the following background:
- Python basics and installing packages with pip.
- Basic probability: distributions, sampling, and conditional probability.
- Familiarity with tokenisation, stop words, and TF-IDF.
- A general idea of what a vector embedding is.
No code in this chapter needs to run. It sets up the vocabulary and the decision framework that the later chapters build on.
The problem topic modelling solves
Suppose you have 200,000 customer support tickets, a decade of news articles, or every abstract in a research archive. You cannot read them all, and you do not know in advance which categories exist. You want a summary of what the collection is about, and you want each document linked to that summary.
Topic modelling is a family of unsupervised methods for this task. Unsupervised means you provide no labelled examples. The method receives only the text and returns two things:
- A set of topics, each described by a ranked list of words.
- For each document, an assignment to one or more of those topics.
You then read the topics, name them yourself, and use the assignments to count, filter, search, or track documents.
This differs from text classification. A classifier needs labels you defined beforehand and learns to reproduce them. A topic model proposes the categories. The output is a starting point for human interpretation, not a ground truth.
What a topic is
The word "topic" means different things in the two methods this tutorial covers. Keeping the two definitions apart prevents most early confusion.
A topic in LDA
Latent Dirichlet Allocation (LDA) is a probabilistic model of word counts. In LDA a topic is a probability distribution over the vocabulary. A sports topic gives high probability to words such as "team", "season", and "coach", and low probability to "election". Every topic assigns some probability to every word.
A document is a mixture of topics. One article can be 70% politics and 30% economics. LDA therefore performs soft assignment: a document belongs to several topics at once, each with a weight.
A topic in BERTopic
BERTopic is a pipeline that clusters document embeddings. In BERTopic a topic is a cluster of documents whose embeddings lie close together. The cluster is then described by its most characteristic words, selected with class-based TF-IDF (c-TF-IDF).
By default each document belongs to exactly one cluster, or to none. Documents that fit no cluster go to a special outlier topic, numbered -1. BERTopic therefore performs hard assignment by default, although it can also report per-document topic probabilities.
Side by side
| Aspect | LDA | BERTopic |
|---|---|---|
| A topic is | A distribution over words | A cluster of similar documents |
| Input representation | Bag-of-words counts | Sentence embeddings |
| Document assignment | A mixture over topics | One cluster, or the outlier topic |
| Word order and context | Ignored | Captured by the embedding model |
| Number of topics | Set by you in advance | Found by the clustering step |
The last row is a simplification. BERTopic lets you reduce the topic count after fitting, and LDA lets you try several values and compare them. Later chapters cover both.
Two routes to the same goal
Both methods turn a pile of documents into topics, but they take different routes. The diagram shows the structure of each.
LDA works on counts and asks which hidden topic mixture best explains the words that appear. BERTopic works on meaning. It first places each document in a vector space where similar texts are near each other, groups the dense regions, and only then uses word statistics to label the groups.
This explains how the two behave in practice. LDA depends on which words co-occur across documents, so it needs reasonably long texts and careful preprocessing. BERTopic depends on the embedding model, so it handles short texts and synonyms better but needs more compute and gives less probabilistic structure.
What you can do with the output
Topic models are a means to an end. Common uses include:
- Exploration. Skim the top words of each topic to learn what a corpus contains before designing a project around it.
- Navigation and search. Tag documents with topics so users can browse a large archive.
- Triage. Route support tickets or survey responses to the right team, or find the largest groups of complaints.
- Trend analysis. Count topic frequency per month to see what is growing or fading. The final chapters of this tutorial cover tracking topics over time.
- Feature generation. Use a document's topic mixture as a compact input to a downstream model.
When topic modelling is the wrong tool
Topic models are often applied where they do not fit. Check these cases first.
- You already have labels. If you have categories and training examples, train a classifier. It will be more accurate and its output will match your definitions.
- You need a specific answer. If the question is "how many tickets mention refunds?", a keyword search or a zero-shot classifier answers it directly. A topic model might split refunds across three topics or merge it with billing.
- The corpus is tiny. With a few dozen documents, LDA has too little data to estimate word distributions, and BERTopic may find only outliers. Read the documents instead.
- You need stable, reproducible categories over time. Both methods are sensitive to settings and random seeds. Retraining on new data can change the topics, which is a problem for reports that compare quarters.
How the tutorial proceeds
The remaining chapters follow the dependency order of the ideas:
- Preprocessing and bag-of-words counts, which LDA and the c-TF-IDF step both depend on.
- The LDA generative story, Dirichlet priors, and how LDA is fitted.
- Training LDA with gensim, then evaluating it with coherence scores and manual inspection, and understanding its limits.
- Sentence embeddings, UMAP, HDBSCAN, and c-TF-IDF, then the full BERTopic pipeline.
- Outlier handling and topic refinement in BERTopic.
- A decision guide for choosing between the two methods, and a short look at topics over time.
By the end you should be able to fit either model, judge whether its topics are useful, and justify your choice for a given corpus.
Check your understanding
Exercise
A colleague has 50,000 short product reviews with no labels and wants to know what customers talk about. Another colleague has 5,000 emails already sorted into "billing", "technical", and "sales" folders and wants to sort new emails the same way. Which task suits topic modelling, and why does the other not?
Show answer
The product reviews suit topic modelling. There are no labels and the categories are unknown, so an unsupervised method can propose themes for a person to interpret. The emails already have defined categories and labelled examples, so a supervised text classifier will sort new emails more accurately and in line with the existing definitions.
Exercise
State how a "topic" is defined in LDA and in BERTopic, and say how a single document relates to topics in each.
Show answer
In LDA a topic is a probability distribution over the vocabulary, and a document is a mixture of several topics with weights. In BERTopic a topic is a cluster of documents with nearby embeddings, described by its top c-TF-IDF words, and by default a document belongs to one cluster or to the outlier topic -1.