Skip to main content

Biterm Topic Model

GitHub Actions Workflow Status Documentation Status Codacy grade Issues Downloads PyPI

Bitermplus is a high-performance implementation of the Biterm Topic Model for short text analysis, originally developed by Xiaohui Yan, Jiafeng Guo, Yanyan Lan, and Xueqi Cheng. Built on a cythonized version of BTM, it features a modern scikit-learn compatible API for seamless integration into ML workflows.

Key Features

  • Scikit-learn Compatible API — Familiar fit(), transform(), and fit_transform() methods for easy adoption
  • ML Pipeline Integration — Seamless compatibility with sklearn workflows, cross-validation, and grid search
  • High-Performance Computing — Cythonized implementation with NumPy vectorization for speed
  • Advanced Inference Methods — Multiple approaches including sum of biterms, sum of words, and mixed inference
  • Comprehensive Model Evaluation — Built-in perplexity, semantic coherence, and entropy metrics
  • Intuitive Topic Interpretation — Simple extraction of topic keywords and document-topic assignments
  • Flexible Text Preprocessing — Customizable vectorization pipeline with sklearn CountVectorizer integration

Donate

If you find this package useful, please consider donating any amount of money. This will help me spend more time on supporting open-source software.

Buy Me A Coffee

Requirements

  • Python ≥ 3.9
  • NumPy ≥ 1.19.0 — Numerical computing foundation
  • Pandas ≥ 1.2.0 — Data manipulation and analysis
  • SciPy ≥ 1.6.0 — Scientific computing library
  • scikit-learn ≥ 1.0.0 — Machine learning utilities and API compatibility
  • tqdm ≥ 4.50.0 — Progress bars for model training

Installation

Standard Installation

Install the latest stable release from PyPI:

pip install bitermplus

Development Version

Install the latest development version directly from the repository:

pip install git+https://github.com/maximtrp/bitermplus.git

Platform-Specific Setup

Linux/Ubuntu: Ensure Python development headers are installed:

sudo apt-get install python3.x-dev  # where x is your Python minor version

Windows: No additional setup required with standard Python installations.

macOS: No additional setup required beyond standard Python installations.

Quick Start

Sklearn-style API (Recommended)

import bitermplus as btm

# Sample documents
texts = [
    "machine learning algorithms are powerful",
    "deep learning neural networks process data",
    "natural language processing understands text"
]

# Create and train model
model = btm.BTMClassifier(n_topics=2, random_state=42)
doc_topics = model.fit_transform(texts)

# Get topic keywords
topic_words = model.get_topic_words(n_words=5)
print("Topic 0:", topic_words[0])
print("Topic 1:", topic_words[1])

# Evaluate model
coherence_score = model.score(texts)
print(f"Coherence: {coherence_score:.3f}")

Traditional API

import bitermplus as btm
import numpy as np
import pandas as pd

# Importing data
df = pd.read_csv(
    'dataset/SearchSnippets.txt.gz', header=None, names=['texts'])
texts = df['texts'].str.strip().tolist()

# Preprocessing
X, vocabulary, vocab_dict = btm.get_words_freqs(texts)
docs_vec = btm.get_vectorized_docs(texts, vocabulary)
biterms = btm.get_biterms(docs_vec)

# Initializing and running model
model = btm.BTM(
    X, vocabulary, seed=12321, T=8, M=20, alpha=50/8, beta=0.01)
model.fit(biterms, iterations=20)
p_zd = model.transform(docs_vec)

# Metrics
coherence = model.coherence_
perplexity = model.perplexity_

Visualization

Visualize your topic modeling results with tmplot:

pip install tmplot
import tmplot as tmp

# Generate interactive topic visualization
tmp.report(model=model, docs=texts)

Topic Modeling Visualization

Documentation

Sklearn-style API Guide Complete guide to the modern sklearn-compatible interface with examples and best practices

Traditional API Tutorial In-depth tutorial covering advanced topic modeling techniques and model evaluation

API Reference Comprehensive documentation of all functions, classes, and parameters

Migration from v0.7.0 to v0.8.0

The traditional API remains fully compatible. The new sklearn-style API provides a simpler alternative:

Old approach (still works)

# Multi-step manual process
X, vocabulary, vocab_dict = btm.get_words_freqs(texts)
docs_vec = btm.get_vectorized_docs(texts, vocabulary)
biterms = btm.get_biterms(docs_vec)

model = btm.BTM(X, vocabulary, seed=42, T=8, M=20, alpha=50/8, beta=0.01)
model.fit(biterms, iterations=100)
p_zd = model.transform(docs_vec)

New approach (recommended)

# One-liner with automatic preprocessing
model = btm.BTMClassifier(n_topics=8, random_state=42, max_iter=100)
p_zd = model.fit_transform(texts)

Migration Benefits

  • Streamlined Workflow — Direct text input with automatic preprocessing eliminates manual steps
  • Enhanced ML Integration — Native support for sklearn pipelines, cross-validation, and hyperparameter tuning
  • Improved Developer Experience — Clear parameter validation with informative error messages
  • Advanced Model Evaluation — Built-in scoring methods and intuitive topic interpretation tools
  • Backward Compatibility: All existing code using the traditional API will continue to work without modifications.

Metadata

Release files for bitermplus 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bitermplus 0.12.0
File Size Uploaded
bitermplus-0.12.0.tar.gz 448.2 kB Details

Release files / bitermplus-0.12.0.tar.gz

Download URL bitermplus-0.12.0.tar.gz
Size 448.2 kB
Tags Source
SHA-256 checksum
How to use checksums
43bf7c9fb626c4a9d642948c4c41a8f7e8e1f8928c16037bf13ea7ffb59f2173
BLAKE2b-256 checksum
How to use checksums
cdc425252d98232de18d6bee229d3d7898eeef8718c5065dee104ffe5888be3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.

Transparency log

Release history Release notifications | RSS feed

1.0.0

25 release files

This release

0.12.0 This release

1 release file

0.11.0

1 release file

0.10.0

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.12

1 release file

0.6.11

1 release file

0.6.10

1 release file

0.6.9

1 release file

0.6.8

1 release file

0.6.7

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.10

1 release file

0.5.9

1 release file

0.5.8

1 release file

0.5.7

1 release file

0.5.6

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.2

1 release file

0.3.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page