feat: add tokenized_documents parameter to avoid redundant tokenization [stacks on #2515]#2518
Draft
pidefrem wants to merge 2 commits into
Draft
feat: add tokenized_documents parameter to avoid redundant tokenization [stacks on #2515]#2518pidefrem wants to merge 2 commits into
tokenized_documents parameter to avoid redundant tokenization [stacks on #2515]#2518pidefrem wants to merge 2 commits into
Conversation
tokenized_documents parameter to avoid redundant tokenizationtokenized_documents parameter to avoid redundant tokenization [stacks on #2515]
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?
feat: add
tokenized_documentsparameter to avoid redundant tokenizationBERTopic re-tokenizes documents from scratch at every pipeline stage. With expensive tokenizers (spaCy transformer-based, custom CJK), the same costly tokenization runs redundantly across 7+ methods.
Changes:
Add a
tokenized_documentsparameter (following the existingembeddingspattern) threaded through the full pipeline: the core methods (fit,fit_transform,transform,_extract_topics,_c_tf_idf), the propagation methods (partial_fit,topics_over_time,topics_per_class,merge_topics,reduce_topics), the vectorizer callers (hierarchical_topics,reduce_outliers,_extract_representative_docs), andapproximate_distribution. When provided, tokens are stored as aTokenized_Documentcolumn in the documents DataFrame and passed through to the vectorizer instead of raw text.scikit-learn's
CountVectorizeralready supports pre-tokenized input whenanalyzeris a callable — no sklearn changes needed. Defaults toNone— all existing behavior unchanged.Fixes #2507
Coordination note (please read before reviewing): This is the most invasive PR in the set — it threads one parameter through ~13 method signatures. Two things I'd like your steer on before you invest review time:
embeddings=convention deliberately, but if you'd prefer a narrower entry point (e.g. accepting pre-tokenized input only onfit_transform/update_topicsand deriving the rest), I'm happy to rework it that way.Corpus/Topic/Topicsrefactor sketched in Ollama + Standardizing LLM-representations #2467. If that direction is close, this parameter would land more cleanly on top of it — I can rebase onto whatever shape you settle on rather than have this complicate that work. Glad to open a Discussion first if you'd rather agree the surface there.Recommended merge order: touches
_extract_representative_docs, so it's cleanest reviewed after #2499 (representative-docs caching). The two are otherwise independent; whichever lands first, I'll rebase the other.Before submitting