From dcdf0fb646b48dda3d4a768b464f53d428f858e8 Mon Sep 17 00:00:00 2001 From: saif-alnajjar-places Date: Sun, 4 Aug 2024 10:35:54 +0400 Subject: [PATCH 1/2] Added a warning when trying to save c-TF-IDF matrix while it is None. --- bertopic/_bertopic.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bertopic/_bertopic.py b/bertopic/_bertopic.py index 0c732338..a8eb5000 100644 --- a/bertopic/_bertopic.py +++ b/bertopic/_bertopic.py @@ -3346,6 +3346,12 @@ def save( # Additional if save_ctfidf: + if self.c_tf_idf_ is None: + logger.warning( + "The c-TF-IDF matrix could not be saved as it was not found. " + "This typically occurs when merging BERTopic models with `BERTopic.merge_models`." + ) + return save_utils.save_ctfidf( model=self, save_directory=save_directory, From dd19cb2fa0aac351f8e0553f1c68b7c531bac8ba Mon Sep 17 00:00:00 2001 From: sete39 Date: Wed, 7 Aug 2024 16:16:43 +0400 Subject: [PATCH 2/2] Changed to if/else instead of returning when checking if the c-TF-IDF matrix is None --- bertopic/_bertopic.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bertopic/_bertopic.py b/bertopic/_bertopic.py index a8eb5000..388d5f05 100644 --- a/bertopic/_bertopic.py +++ b/bertopic/_bertopic.py @@ -3351,13 +3351,13 @@ def save( "The c-TF-IDF matrix could not be saved as it was not found. " "This typically occurs when merging BERTopic models with `BERTopic.merge_models`." ) - return - save_utils.save_ctfidf( - model=self, - save_directory=save_directory, - serialization=serialization, - ) - save_utils.save_ctfidf_config(model=self, path=save_directory / "ctfidf_config.json") + else: + save_utils.save_ctfidf( + model=self, + save_directory=save_directory, + serialization=serialization, + ) + save_utils.save_ctfidf_config(model=self, path=save_directory / "ctfidf_config.json") @classmethod def load(cls, path: str, embedding_model=None):