Repository navigation
Conversation
When BERTopic has no embedding_model (e.g. precomputed embeddings were passed), MaximalMarginalRelevance returned the 30 c-TF-IDF candidates untruncated, and its warnings.warn() was suppressed by the module-level UserWarning filter in _bertopic.py. Truncate the fallback to top_n_words and log through MyLogger, matching the other warnings in the package. Fixes MaartenGr#2323
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #2323. Root-cause analysis: #2323 (comment)
When BERTopic is fitted with precomputed embeddings and no
embedding_model,MaximalMarginalRelevancecannot run and falls back to the c-TF-IDF words. Two things went wrong in that fallback:_extract_words_per_topicpasses at least 30 candidate words to representation models (max(self.top_n_words, 30)), and the fallback returned them untruncated. That is whyupdate_topicswith MMR gave 30+ words regardless oftop_n_words.warnings.warn(...), which raises aUserWarning._bertopic.pysetswarnings.filterwarnings("ignore", category=UserWarning)at import time, so the warning never showed up. This explains why it "should fire a warning, but in my tests it doesn't". The message also lacked a space (instantiatedwith).Changes
self.top_n_words, the same way the regular MMR path limits its output.MyLogger, like the other warnings in the package (_utils.py,backend/_utils.py), so the module-level filter no longer hides it. Fix the wording.tests/test_representation/test_mmr.py, a fast regression test with no model or dataset downloads.Before / after (precomputed embeddings,
update_topics(docs, representation_model=MaximalMarginalRelevance())):masterI left the global
UserWarningfilter in_bertopic.pyuntouched because it seems deliberate. Note that it also hidesUserWarnings from user code afterimport bertopic. Happy to open a separate issue if you'd like to revisit it, or to adjust the approach here.Before submitting
ruff check/ruff formatpasstests/test_representationandtests/test_bertopic.pypass locally (97 passed, 1 skipped)