MaartenGr / MaartenGr/BERTopic

Assigning documents to multiple topics using zero-shot topic modeling

Open
#1,928 1 comment 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

Goal

I am interested in fitting a BERTopic model using zero-shot topic modeling. I want it to be possible for documents to be assigned to more than one of my suggested topics. I have patched several BERTopic functions to enable this but wanted to get the author's opinion on correctness or alternatives.

The current implementation assigns documents to at most one suggested topic based on a specified cosine similarity threshold during model construction. If the threshold is met for a specific document, it is assigned to the topic with which it has the highest similarity.

My Approach

My first change is to the _zeroshot_topic_modeling function. I calculate which topics each document has a similarity with that exceeds the specified threshold. Next, if a document has more than one match, additional copies of that document (and its embedding) are made as necessary (keeping copies adjacent in the list of documents). Because this function does not have access to my documents outside of BERTopic, I set an instance variable that provides enough information to make copies of my documents and embeddings as necessary.

In the _combine_zeroshot_topics step, there is an occasional issue where the merged model topics is set as a np.ndarray rather than a list, which causes problems later on. Fixing this is my second patch.

The next step is to reduce_topics. Here, multiple zero-shot topics may essentially be merged, causing the new topics to sometimes have duplicated documents. I have patched _reduce_to_n_topics just before documents.Topic = new_topics to remove duplicate documents within a topic based on external document IDs I provide via an instance variable. I keep only unique (topic_id, external_document_ID) pairs. That instance variable with external document IDs is updated to a (possibly) reduced list of IDs, which I use outside of BERTopic to update my list of documents and embeddings.

The next step is reduce_outliers, where documents is my expanded list of documents (with potential duplicates) after fitting. I do not believe there is any risk here from duplicated documents, because any outliers that get reclassified only had one copy anyway.

The last step is update_topics using the updated documents list and topic IDs from the reduce_outliers step. Because there is no reorganization of topics, I believe there is no risk here from duplicated documents.

After all this, I have postprocessing to determine the list of topics for each of my original documents.

Questions

Keeping in mind my original goal, are there any apparent flaws in this approach or suggestions for improvement. I understand there are some other methods out there related to multiple topics per document, such as Topic Distributions or using the probabilities that are returned by transforming my documents after all my steps, but I have not had much luck getting any sort of useful distribution, and a probability matrix is only returned on fit.

One alternative I thought of is to update topic_model.topics_ after fitting based on a threshold and the probabilities, update my documents and embeddings accordingly, and then keep the reduce_topics patch to avoid duplicates. This would have the benefit of multiple topics for a document not just for the suggested topics but also for ones that came for clustering. A downside is an additional threshold to specify.

Thoughts?

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 bertopic/_bertopic.py, especially _zeroshot_topic_modeling, _combine_zeroshot_topics, _reduce_to_n_topics, reduce_outliers, and update_topics, alongside the linked zero-shot documentation. Trace how topic assignments and document rows move through fitting and reduction; done would require a maintainer-approved design for multi-topic assignments and its treatment of duplicate documents.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.