As inquiries and feedback pile up, more items fall outside the category tags you defined in advance. BERTopic converts documents into embedding vectors, clusters them, and discovers topics in the collection without predefined categories. I installed it and tested topic discovery with network access disabled.
BERTopic reduces the dimensionality of document embeddings with UMAP, clusters them with HDBSCAN, and extracts representative words from each cluster as topics. The project uses the MIT License (*1).
1. Install it
FROM docker.io/library/python:3.12.13-slim
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir --quiet 'bertopic==0.17.4'
COPY warmup_model.py /tmp/warmup_model.py
RUN python /tmp/warmup_model.py
WORKDIR /work
Install build-essential to compile HDBSCAN's native extension, which BERTopic uses for clustering.
podman build -t bertopic-sandbox .
2. Mix three genres of text and pass them in
I prepared 30 short texts across three genres: container technology, cooking, and home gardening. I passed them to BERTopic without supplying any genre labels. I ran the experiment with network access disabled.
from bertopic import BERTopic
from sklearn.feature_extraction.text import CountVectorizer
docs = [
"Rootless containers avoid running processes as root on the host.",
"Podman lets you build and run containers without a daemon.",
# ...(10 container-related, 10 cooking-related, 10 home-gardening-related: 30 total)
]
topic_model = BERTopic(
vectorizer_model=CountVectorizer(stop_words="english"),
min_topic_size=5,
)
topics, probs = topic_model.fit_transform(docs)
info = topic_model.get_topic_info()
print(info[["Topic", "Count", "Name"]].to_string(index=False))
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-e HF_HUB_OFFLINE=1 -e TRANSFORMERS_OFFLINE=1 \
-v "$(pwd)":/work -w /work bertopic-sandbox \
python topic_demo.py
-v makes the specified directory readable and writable from the container. Run this in an empty directory dedicated to the experiment. I needed to set HF_HUB_OFFLINE and TRANSFORMERS_OFFLINE to prevent the libraries used by the embedding model from checking online for adapter configuration, even with the model in the cache. I saw the same symptom in an earlier KeyBERT test.
The command printed the following:
Topic Count Name
0 20 0_soil_encourage_plants_sauce
1 10 1_container_containers_host_built
I intended three genres, but the texts split into two topics. BERTopic grouped the 10 container texts into their own topic, represented by container, containers, host, and built. It combined the 20 cooking and home-gardening texts into one topic, represented by soil, encourage, plants, and sauce. In the embedding space, BERTopic found the cooking and home-gardening vocabulary, including words about plants, pots, and care, closer to each other than to the vocabulary of container technology. The min_topic_size=5 setting constrained cluster size, so BERTopic grouped those two genres together.
The completed run illustrates a premise of topic modeling: even if you label the set as three genres in advance, the model uses semantic proximity as its sole criterion. On a small dataset, the model tends to group subjects with close meanings.
Find categories later
Pre-tagging inquiries and feedback takes time, and you can miss topics you did not anticipate. BERTopic lets you discover topics after collecting the text, so you can defer category design. This run showed that BERTopic can group subjects with close meanings at a granularity different from human intuition. Account for that difference when you read the results.
Sources
*1 BERTopic (GitHub), https://github.com/MaartenGr/BERTopic, MIT License