Turftopic: Topic Modelling with Contextual Representations from Sentence Transformers

Márton Kardos, Kenneth C. Enevoldsen, Jan Kostkan, Ross Deans Kristensen‐McLachlan, Roberta RoccaJ. Open Source Softw 2025 · DOI · DBLP

Abstract

Turftopic is a Python library designed to unify state-of-the-art contextual topic models under a single, scikit-learn-compatible API. By providing a modular pipeline for document embedding, term vectorization, and LLM-powered topic naming, it addresses the fragmentation in the current software ecosystem. Turftopic enables researchers and practitioners to easily compare, integrate, and deploy diverse models like BERTopic, Top2Vec, and KeyNMF within a cohesive framework.

Introduction

Topic modeling has transitioned from traditional "bag-of-words" methods, such as Latent Dirichlet Allocation (LDA), to sophisticated approaches leveraging Sentence Transformers for contextual understanding. However, this evolution has led to a fragmented ecosystem where different models require distinct APIs, dependencies, and visualization tools. This "fragmentation tax" hinders reproducible research and complicates the integration of topic modeling into production workflows.

Overview of Turftopic's core pillars: Easy Model Interpretation, Unified Interface, and Flexibility.

Turftopic solves this by providing a unified, user-friendly framework. It brings disparate models into one cohesive ecosystem, allowing for seamless experimentation and deployment. By mimicking the familiar scikit-learn interface, it lowers the barrier to entry for data scientists and engineers already integrated into the Python machine learning ecosystem.

Methods and Architecture

Turftopic’s primary innovation is its modular pipeline architecture, which adheres to the fit and transform patterns. This design allows users to treat the underlying topic discovery algorithm as a swappable component. A typical pipeline involves four distinct stages:

  1. Encoder: Documents are converted into continuous vector representations using Sentence Transformers or other embedding models.
  2. Vectorizer: Term counts are extracted (e.g., via CountVectorizer) to define the representative words for each topic.
  3. Topic Model: The core discovery algorithm. Turftopic supports clustering-based models (BERTopic, Top2Vec), auto-encoders (ZeroShotTM, CombinedTM), and decomposition models.
  4. Namer: An optional stage utilizing Large Language Models (LLMs) like ChatGPT or Mistral to generate descriptive, human-readable names for discovered topics.
The unified interface supports a wide variety of models and flexible modeling paradigms like dynamic and online learning.

Notably, Turftopic introduces implementations rarely found elsewhere, such as KeyNMF, Semantic Signal Separation ($S^3$), and a Gaussian Mixture Model (GMM) with a soft-c-tf-idf weighting scheme. The library also features model-agnostic visualization tools and integrates with topicwizard for interactive exploration.

Applications and Case Studies

While Turftopic is a software framework, its utility is demonstrated through diverse real-world applications across various domains:

  • Media Analysis: Investigating information dynamics in Chinese Diaspora media.
  • Public Health: Analyzing media coverage of the HPV vaccine in Denmark.
  • Humanities: Studying Danish golden-age literature.
  • Business Intelligence: Extracting insights from customer feedback and social media data to inform product development.

Key Insights and Results

Turftopic successfully consolidates several state-of-the-art models into a single package, offering several key advantages:

  • Model Agnosticism: Users can experiment with different mathematical approaches (e.g., clustering vs. matrix factorization) without rewriting preprocessing or visualization code.
  • Interpretability: The library prioritizes human understanding through "pretty printing" and automated LLM naming, bridging the gap between high-dimensional vector spaces and actionable insights.
  • Unique Implementations: It provides the only known unified implementations for KeyNMF and $S^3$.
  • Multilingual and Efficient: Supports multilingual corpora and "online" learning, allowing models to be updated incrementally as new data arrives, rather than requiring a full re-train.

Limitations and Future Directions

The field of contextual topic modeling is rapidly evolving. The authors plan to expand the library with new models as they emerge to ensure it remains representative of the state of the art. Current limitations include the computational overhead inherent in transformer-based encoding (often requiring GPUs) and the external dependencies or memory requirements associated with LLM-based topic naming. Future work will focus on optimizing these stages and expanding the suite of supported architectures.

Summary updated

BibTeX
@article{kardos2025turftopic,
  author = {Márton Kardos and Kenneth C. Enevoldsen and Jan Kostkan and Ross Deans Kristensen‐McLachlan and Roberta Rocca and Kenneth Enevoldsen},
  title = {{Turftopic: Topic Modelling with Contextual Representations from Sentence Transformers}},
  journal = {J. Open Source Softw.},
  year = {2025},
  doi = {10.21105/joss.08183},
}
Figure