MaartenGr / MaartenGr/BERTopic

Add contrastive and embedding-aware feature importance as representation aspects

Open
#2,506 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

### Feature request

BERTopic's topic term importance is based solely on c-TF-IDF. Add a `FeatureImportance` representation class with pluggable importance methods:

| Method | Algorithm | Answers |
|--------|-----------|---------|
| `"fighting_words"` | Bayesian log-odds with Dirichlet priors (Monroe et al. 2008) | "What terms distinguish this topic from the rest?" |
| `"centroid_distance"` | Cosine similarity between topic centroid and term embeddings | "What terms are semantically central to this topic?" |

```python
from bertopic.representation import FeatureImportance

topic_model = BERTopic(
representation_model={
"Main": main_model,
"FightingWords": FeatureImportance(method="fighting_words"),
"CentroidDistance": FeatureImportance(method="centroid_distance"),
}
)
# Access via existing API:
topic_model.get_topic(0, aspect="FightingWords")
```

### Motivation

c-TF-IDF identifies frequent terms within a topic but doesn't capture:

- **Contrastive importance** — which terms *distinguish* this topic from others? (A term can be frequent in a topic but also frequent everywhere.)
- **Embedding-aware importance** — which terms are semantically closest to the topic centroid? (c-TF-IDF is bag-of-words; it misses semantic similarity.)

These methods slot naturally into BERTopic's existing `topic_aspects_` infrastructure — no changes to core BERTopic code needed.

### Your contribution

I can submit a PR that adds a `FeatureImportance` class under `representation/` implementing both methods. It follows the existing `BaseRepresentation` interface and integrates with the aspect model pipeline.

I've already been prototyping this in my fork (working implementation with tests). If you'd prefer to start with a single method to keep the surface minimal, I'm happy to align on scope before opening the PR.

---

Contributor guide

Open the contributing guide

Research direction

The requested class belongs under representation/ and must follow the existing BaseRepresentation interface; start by reading the current representation classes and topic_aspects_ integration. Verify both named methods through the existing get_topic(..., aspect=...) path and the contributor's tests, with completion shown by both aspects returning the requested topic-term importance.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.