Sub topic 2
Preparing Text for Topic Models
Assume you know tokenisation, stop words, and the difference between a word and a document embedding. Preprocessing converts raw documents into a consistent form that a topic model can use. The right amount of cleaning depends on the model.
LDA sees documents as bags of word counts. It does not use word order. Therefore, spelling variants, punctuation, boilerplate, and very common words can hide useful topics. BERTopic uses document embeddings before clustering. Embeddings depend on word order and context, so aggressive cleaning can remove meaning.
A practical preprocessing workflow
A typical workflow is:
- Remove content that is not part of the language, such as HTML tags, navigation text, and repeated email signatures.
- Normalise text, usually by converting it to lowercase.
- Tokenise the text into words or word-like units.
- Remove tokens that carry little information, such as punctuation and selected stop words.
- Remove very rare and very common terms across the collection.
- Optionally lemmatise words, which maps forms such as “running” and “ran” to a common base form.
- Create phrases such as “climate change” when their combined meaning matters.
import re
from collections import Counter
documents = [
"The players were running quickly in the final match!",
"A final match can change the season standings.",
]
stop_words = {"a", "an", "and", "in", "the", "to", "were"}
def preprocess(text):
text = text.lower()
tokens = re.findall(r"[a-z]+", text)
return [token for token in tokens if token not in stop_words]
tokenised = [preprocess(document) for document in documents]
counts = Counter(token for document in tokenised for token in document)
# Keep terms that occur in at least one document and in no more than 90% of documents.
document_frequency = Counter(
token for document in tokenised for token in set(document)
)
n_documents = len(tokenised)
vocabulary = {
token for token, frequency in document_frequency.items()
if frequency >= 1 and frequency / n_documents <= 0.9
}
filtered = [
[token for token in document if token in vocabulary]
for document in tokenised
]
print(filtered)
print(counts)The thresholds for document frequency need enough documents to be meaningful. Removing a term only because it is rare can discard a small but important topic.
LDA and BERTopic need different choices
| Decision | LDA | BERTopic |
|---|---|---|
| Lowercase text | Usually useful | Usually useful |
| Remove stop words | Often useful | Optional |
| Lemmatise | Often useful | Use cautiously |
| Preserve word order | Not used | Important for embeddings |
| Remove short documents | Usually useful | Usually useful |
For LDA, a smaller, cleaner vocabulary often improves interpretability and reduces computation. For BERTopic, keep enough natural language for the embedding model to understand meaning. You can pass cleaned text to BERTopic, but test it against lightly cleaned or raw text.
Exercise
Why might removing every rare word damage an LDA model?
Show answer
A rare word may identify a real but small topic. Remove terms using collection-level evidence and inspect the effect.