Skip to content

refactor: extract shared helper methods for document aggregation and topic naming#2509

Open
pidefrem wants to merge 1 commit into
MaartenGr:masterfrom
pidefrem:pr02-shared-helpers-refactoring
Open

refactor: extract shared helper methods for document aggregation and topic naming#2509
pidefrem wants to merge 1 commit into
MaartenGr:masterfrom
pidefrem:pr02-shared-helpers-refactoring

Conversation

@pidefrem

@pidefrem pidefrem commented Jul 8, 2026

Copy link
Copy Markdown

What does this PR do?

refactor: extract shared helper methods for document aggregation and topic naming

Several methods in _bertopic.py contain near-identical copy-paste patterns that have diverged slightly over time:

  1. Document aggregationgroupby("Topic").agg({"Document": " ".join}) appears in 4 methods (_extract_topics, hierarchical_topics, update_topics, partial_fit). When a new column needs aggregating, every call site must be updated independently.
  2. Feature names extraction — a 5-line sklearn version check (get_feature_names vs get_feature_names_out) is duplicated in hierarchical_topics and _c_tf_idf.
  3. Topic name construction"_".join([x[0] for x in words][:N]) appears in 4 places across 2 methods with inconsistent slicing ([:4] in topic_labels_, [:5] in hierarchical_topics).

Changes:

Extract three private helper methods:

  • _aggregate_documents() — centralizes the groupby/agg pattern
  • _get_feature_names() — centralizes the sklearn version check
  • _topic_name_from_words() — centralizes topic name string construction with consistent slicing

All existing call sites updated to use the helpers. Zero behavior change — pure refactoring. All existing tests pass without modification.

Fixes #2497

Before submitting

  • This PR fixes a typo or improves the docs (if yes, ignore all other checks!).
  • Did you read the contributor guideline?
  • Was this discussed/approved via a Github issue? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes (if applicable)?
  • Did you write any new necessary tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract duplicated document-aggregation and topic-naming patterns into shared helpers

2 participants