MaartenGr / MaartenGr/BERTopic

Creating Topic Taxonomies

Open
#2,526 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

### Feature request

Hey @MaartenGr,

first off: great repository! It was really easy to get into it and we're using BERTopic extensively, which brings me to my feature request (that I will follow up with a PR -> Update: [It's here :)](https://github.com/MaartenGr/BERTopic/pull/2527):

`create_topic_taxonomy()`: controllable multi-level topic hierarchies

We've been building topic taxonomies for our use case and ran into a gap between the existing options:

- hierarchical_topics() gives a full dendrogram but no control over the number of levels
- reduce_topics() can reduce to a target count but isn't really designed for building hierarchies - calling it repeatedly works but produces a lot of single-child branches and the quality of the hiearchy is questionable.

What we needed was a way to say *"give me 3 levels with roughly X, Y, Z topics each, and make sure every parent has at least N children."*

### Motivation

We're using BERTopic a lot, also in production, to analyze textual data, but we need levels and a topic hierarchy, which is why I'd like to contribute directly to the repo.

### Your contribution

So we built create_topic_taxonomy(), which:

- Takes nr_topics_per_level (e.g. [20, 5]) to define the shape
- Enforces a min_children constraint per parent (default 2) to avoid degenerate branches
- Optionally blends document centroids with topic embeddings (configurable weight) for richer topic vectors when building the hierarchy, which works really well for us.
- Clusters bottom-up using agglomerative clustering + a repair step for undersized clusters
- Labels parents via c-TF-IDF on merged documents
- Returns a clean DataFrame with Topic_ID, Level, Parent_ID, etc.
- Does not touch the fitted model - it's a read-only lookup table on top of the leaves

The method sits right next to hierarchical_topics() and follows existing code patterns. PR incoming with tests and docs.

---

We've added a dedicated documentation page to the mkdocs (Topic Taxonomy), the actual implementation is only a single method. :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the existing hierarchical_topics() implementation and PR #2527, which contains the proposed method. Review its accompanying tests and the mkdocs Topic Taxonomy page; done means the requested multi-level taxonomy behavior is covered and documented without modifying the fitted model.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.