Skip to content

Add contrastive and embedding-aware feature importance as representation aspects #2506

Description

@pidefrem

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?"
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.


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions