MaartenGr / MaartenGr/BERTopic

Topic aspects are not updated when merging topics and the model uses a custom vectorizer

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

Nobody has claimed this yet.

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

Description

### Have you searched existing issues? 🔎

- [x] I have searched and found no existing issues

### Desribe the bug

If the update_topics function is called with a custom vectorizer, any topic merging afterwards will not update the aspects of the topic representations, causing them to refer to the old topic, the following is before and after calling merge

Before calling merge (aspects are correctly mapped to the topics)

Image

after merging topics 6 and 7, the newly merged topic now topic 5, has Aspect1 and Aspect2 of the pre-merge topic 5, and every topic after 5 has the wrong aspects, still referring the old topic, displaced by one in this case (since one topic was gone)

Image

When not using a custom vectorizer merging works fine as expected.

### Reproduction

```python
from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups

docs = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))['data']

main_representation_model = KeyBERTInspired()
aspect1_representation_model = PartOfSpeech("en_core_web_sm")
aspect2_representation_model = MaximalMarginalRelevance(diversity=0.3)

representation_model = {
"Main": main_representation_model,
"Aspect1": aspect1_representation_model,
"Aspect2": aspect2_representation_model
}

topic_model = BERTopic(representation_model=representation_model)

topics, probs = topic_model.fit_transform(docs)

vectorizer_model = CountVectorizer(stop_words="english", ngram_range=(1, 3), min_df=15)
topic_model.update_topics(docs, vectorizer_model=vectorizer_model)

topic_model.merge_topics(docs, [6,7])

topic_model.get_topic_info()[1:12]

```

### BERTopic Version

0.17.4

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 by running the provided reproduction with BERTopic 0.17.4, then inspect update_topics, merge_topics, and get_topic_info. Compare the topic aspects before and after merging with the custom CountVectorizer; done means merged topics retain correctly mapped aspects, as they do without a custom vectorizer.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, scikit-learn
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.