From cc470ce0bac6164e536d251a8423bcc5007c985f Mon Sep 17 00:00:00 2001 From: aoright <102943475+aoright@users.noreply.github.com> Date: Fri, 11 Sep 2026 16:16:52 +0800 Subject: [PATCH 1/2] Fix topic deletion bugs and find_topics list crash --- bertopic/_bertopic.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/bertopic/_bertopic.py b/bertopic/_bertopic.py index cfafb58a..7b388157 100644 --- a/bertopic/_bertopic.py +++ b/bertopic/_bertopic.py @@ -1471,6 +1471,8 @@ def find_topics( # Extract search_term embeddings and compare with topic embeddings if search_term is not None: + if isinstance(search_term, list): + search_term = search_term[0] search_embedding = self._extract_embeddings([search_term], method="word", verbose=False).flatten() elif image is not None: search_embedding = self._extract_embeddings( @@ -2217,13 +2219,9 @@ def delete_topics( # Initialize custom labels for -1 topic if they exist if hasattr(self, "custom_labels_") and self.custom_labels_ is not None: - self.custom_labels_[-1] = "" + self.custom_labels_ = [""] + self.custom_labels_ + - # Initialize ctfidf model diagonal for -1 topic (ones) if it exists - if hasattr(self, "ctfidf_model") and self.ctfidf_model is not None: - n_features = self.ctfidf_model._idf_diag.shape[1] - outlier_diag = sp.csr_matrix(([1.0], ([0], [0])), shape=(1, n_features)) - self.ctfidf_model._idf_diag = sp.vstack([outlier_diag, self.ctfidf_model._idf_diag]) # Initialize topic aspects for -1 topic (empty dict for each aspect) if they exist if hasattr(self, "topic_aspects_") and self.topic_aspects_ is not None: @@ -2270,12 +2268,16 @@ def delete_topics( # Update custom labels if they exist if hasattr(self, "custom_labels_") and self.custom_labels_ is not None: - new_labels = { + old_unique_topics = sorted(set(self.topics_)) + if not had_outliers and any(topic in topics_to_delete for topic in self.topics_): + old_unique_topics = [-1] + old_unique_topics + + new_labels_dict = { (final_mapping[old_topic] if old_topic != -1 else -1): label - for old_topic, label in self.custom_labels_.items() + for old_topic, label in zip(old_unique_topics, self.custom_labels_) if old_topic not in topics_to_delete } - self.custom_labels_ = new_labels + self.custom_labels_ = [new_labels_dict[t] for t in sorted(new_labels_dict.keys())] # Update topic representations new_representations = { @@ -2305,10 +2307,7 @@ def delete_topics( mask = np.array([topic not in topics_to_delete for topic in range(matrix.shape[0])]) setattr(self, attr, matrix[mask]) - # Update ctfidf model to remove deleted topics if it exists - if hasattr(self, "ctfidf_model") and self.ctfidf_model is not None: - mask = np.array([topic not in topics_to_delete for topic in range(self.ctfidf_model._idf_diag.shape[0])]) - self.ctfidf_model._idf_diag = self.ctfidf_model._idf_diag[mask] + def reduce_topics( self, From 5825e1e10543407c47fd2954e39555405021b1ff Mon Sep 17 00:00:00 2001 From: aoright <102943475+aoright@users.noreply.github.com> Date: Wed, 16 Sep 2026 09:48:04 +0800 Subject: [PATCH 2/2] style: format with ruff Signed-off-by: aoright <102943475+aoright@users.noreply.github.com> --- bertopic/_bertopic.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bertopic/_bertopic.py b/bertopic/_bertopic.py index 7b388157..2c24618a 100644 --- a/bertopic/_bertopic.py +++ b/bertopic/_bertopic.py @@ -2219,9 +2219,7 @@ def delete_topics( # Initialize custom labels for -1 topic if they exist if hasattr(self, "custom_labels_") and self.custom_labels_ is not None: - self.custom_labels_ = [""] + self.custom_labels_ - - + self.custom_labels_ = ["", *self.custom_labels_] # Initialize topic aspects for -1 topic (empty dict for each aspect) if they exist if hasattr(self, "topic_aspects_") and self.topic_aspects_ is not None: @@ -2270,7 +2268,7 @@ def delete_topics( if hasattr(self, "custom_labels_") and self.custom_labels_ is not None: old_unique_topics = sorted(set(self.topics_)) if not had_outliers and any(topic in topics_to_delete for topic in self.topics_): - old_unique_topics = [-1] + old_unique_topics + old_unique_topics = [-1, *old_unique_topics] new_labels_dict = { (final_mapping[old_topic] if old_topic != -1 else -1): label @@ -2307,8 +2305,6 @@ def delete_topics( mask = np.array([topic not in topics_to_delete for topic in range(matrix.shape[0])]) setattr(self, attr, matrix[mask]) - - def reduce_topics( self, docs: List[str],