From 003160dc479daa7fb22e63a03c18bb9511a84c03 Mon Sep 17 00:00:00 2001 From: huisman <23581164+huisman@users.noreply.github.com> Date: Wed, 29 Oct 2025 21:40:46 +0100 Subject: [PATCH 1/2] Update topic mapping when all documents match zero-shot topics Fix topic labels when all documents match zero-shot topics by updating the topic id mapping even when all documents are matched with zero-shot topics --- bertopic/_bertopic.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bertopic/_bertopic.py b/bertopic/_bertopic.py index 8c27ba14..bfe84691 100644 --- a/bertopic/_bertopic.py +++ b/bertopic/_bertopic.py @@ -494,6 +494,19 @@ def fit_transform( documents, embeddings, assigned_documents, assigned_embeddings ) else: + # Update topic id to zeroshot topic id mapping + self._topic_id_to_zeroshot_topic_idx = { + new_topic_id: zeroshot_topic_id + for new_topic_id, zeroshot_topic_id in enumerate(set(assigned_documents.Topic)) + } + + # All documents matches zero-shot topics + documents = assigned_documents + embeddings = assigned_embeddings + + # Update topic sizes + self._update_topic_size(documents) + # All documents matches zero-shot topics documents = assigned_documents embeddings = assigned_embeddings From 4a3b74a50874a33b4f60c544bb1fafec515f6b5c Mon Sep 17 00:00:00 2001 From: huisman <23581164+huisman@users.noreply.github.com> Date: Wed, 29 Oct 2025 21:54:55 +0100 Subject: [PATCH 2/2] Fix extra space before comment --- bertopic/_bertopic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bertopic/_bertopic.py b/bertopic/_bertopic.py index bfe84691..d584a598 100644 --- a/bertopic/_bertopic.py +++ b/bertopic/_bertopic.py @@ -504,7 +504,7 @@ def fit_transform( documents = assigned_documents embeddings = assigned_embeddings - # Update topic sizes + # Update topic sizes self._update_topic_size(documents) # All documents matches zero-shot topics