Prompting Large Language Models for Topic Modeling

Han Wang, Nirmalendu Prakash, Nguyen-Khoi Hoang, Ming Shan Hee, Usman Naseem, Roy Ka-Wei Lee

Introduction

Topic modeling is a fundamental task in natural language processing, aimed at discovering latent thematic structures within large document collections. Traditionally, this field has been dominated by statistical "bag-of-words" models like Latent Dirichlet Allocation (LDA) or, more recently, neural embedding-based approaches like BERTopic. However, these methods often struggle with semantic nuances and data sparsity. This paper introduces PromptTopic, a novel framework that leverages the in-context learning capabilities of Large Language Models (LLMs) to redefine topic modeling as a generative, rather than purely statistical, process.

The Limitations of Traditional Approaches

Despite their widespread use, traditional topic modeling techniques face three primary challenges:

  1. Short Text Sparsity: Models like LDA rely on word co-occurrence. In short texts such as tweets, there is insufficient overlap to establish clear patterns, leading to fragmented or incoherent topics.
  2. Token vs. Sentence Semantics: Most statistical models focus on individual word frequencies, often failing to capture the deeper, contextual meaning inherent in full sentences.
  3. Hyperparameter Sensitivity: Achieving high-quality results typically requires extensive manual tuning of parameters that do not generalize well across different datasets.

PromptTopic addresses these hurdles by utilizing the vast internal knowledge and semantic understanding of LLMs, allowing the system to "understand" the essence of a document rather than simply counting its tokens.

The PromptTopic Framework

The PromptTopic workflow is divided into three distinct stages designed to move from raw text to refined thematic clusters:

1. Topic Generation

Instead of clustering high-dimensional vectors, PromptTopic uses few-shot prompting. The system provides an LLM (such as ChatGPT or LLaMA-13B) with a small set of demonstration examples (typically $N=4$) consisting of "Article -> Topic" pairs. The model then extracts high-level topics for new documents based on these examples. This approach allows the model to leverage its pre-trained knowledge to categorize even the most sparse texts.

2. Topic Collapsing

Because LLMs generate topics independently for different documents, the initial output often contains redundant or highly similar labels (e.g., "cinema" vs. "film"). To consolidate these into a coherent set of $K$ topics, the authors propose two strategies:

  • Prompt-Based Matching (PBM): The LLM is tasked with iteratively merging similar topics from a provided list.
  • Word Similarity Matching (WSM): A statistical approach using c-TF-IDF to identify and merge topics with significant word overlap.

3. Topic Representation

To ensure the final topics are human-readable and descriptive, the system identifies the top 100 words associated with a topic using c-TF-IDF. An LLM then filters these candidates down to the 10 most representative and semantically coherent words, providing a clear summary of the topic's content.

Experimental Evaluation

The researchers benchmarked PromptTopic against six baseline models, including LDA, NMF, and BERTopic, across three diverse datasets:

  • 20 NewsGroup: Long-form forum posts.
  • Yelp Reviews: Medium-length consumer feedback.
  • Twitter: Extremely short, noisy text with an average length of only 8.55 words.

Key Insights

  • The Short-Text Advantage: PromptTopic demonstrated a significant advantage on the Twitter dataset. Because LLMs have been trained on vast amounts of internet data, they can provide external context to a tweet that a statistical model—restricted to the words within the specific dataset—cannot access.
  • Human Evaluation vs. Automated Metrics: The study highlights a discrepancy between automated metrics like NPMI (Normalized Pointwise Mutual Information) and human judgment. While models like BERTopic occasionally scored higher on NPMI, human evaluators consistently found PromptTopic’s results more meaningful and distinct.
  • Open-Source Viability: Experiments with LLaMA-13B showed that open-source, offline models perform comparably to proprietary APIs like ChatGPT. This makes the PromptTopic approach viable for researchers with privacy concerns or limited budgets.

Results

In "Word Intrusion" tests—a standard metric where humans identify a "fake" word inserted into a topic list—PromptTopic-PBM achieved an accuracy of 0.80 on the Twitter dataset, vastly outperforming BERTopic's 0.525. On the 20 NewsGroup dataset, PromptTopic-WSM achieved a Topic Diversity (TD) score of 0.91, indicating that the model maintains high variety while ensuring internal semantic consistency.

Qualitative analysis further supported these findings. While baseline models occasionally grouped unrelated terms (e.g., placing "aquarium" in a "Politics" topic), PromptTopic consistently selected precise, relevant terms such as "president," "protest," and "lawsuit."

Conclusion and Future Work

PromptTopic represents a shift toward semantic-heavy topic modeling. While the approach is currently more computationally intensive than traditional methods and faces challenges regarding context window limits during topic collapsing, it offers a significant leap in coherence and relevance, particularly for short-form text. Future research will focus on batch-wise merging strategies to scale the framework for massive datasets without exceeding LLM token limits.

Summary updated

BibTeX
@inproceedings{wang2023prompting,
  author = {Han Wang and Nirmalendu Prakash and Nguyen-Khoi Hoang and Ming Shan Hee and Usman Naseem and Roy Ka-Wei Lee},
  title = {{Prompting Large Language Models for Topic Modeling}},
  booktitle = {IEEE Big Data},
  year = {2023},
  doi = {10.1109/bigdata59044.2023.10386113},
}
Figure