Skip to main content

Topica: fast, all-purpose topic modeling for Python — a Rust core for LDA, STM, and more

Project description

Topica: fast, all-purpose topic modeling for Python

topica is a fast topic-modeling library for Python with more than a dozen models, built for social scientists who want to move from text data to publishable results in a single workflow. It brings together models and tools usually split across JVM software like MALLET and R packages like stm, and runs them on a parallel Rust core competitive with the standard implementations, with every fit reproducible from a fixed seed. Each model comes with the validation, covariate-effect, and reporting tools to meet the standards reviewers expect.

pip install topica

The core needs only NumPy. Optional extras add features without weighing the core down: topica[viz] (matplotlib plots), topica[formula] (R-style formulas), topica[polars] (Polars frames), and topica[llm] (LLM labels and embeddings, OpenAI or local via ollama). PyTorch is never required.

from topica import LDA

model = LDA(num_topics=2, seed=42)
model.fit([["cat", "dog", "fish"]] * 15 + [["planet", "star", "moon"]] * 15, iterations=1000)

for i, words in enumerate(model.top_words(3)):
    print(f"Topic {i}:", " ".join(w for w, _ in words))

See the getting-started guide and the worked examples for end-to-end analyses.

Models

Count-based models learn topics from word counts (collapsed Gibbs or variational EM):

Model What it's for
LDA Classic topics via fast collapsed-Gibbs (SparseLDA); optional multi-threaded and LightLDA alias samplers
DMR Topics conditioned on document metadata (Dirichlet-multinomial regression)
LabeledLDA Supervised topics tied to document labels
CTM Correlated topics (logistic-normal)
STM The Structural Topic Model: correlated topics with prevalence and content covariates
SAGE Content-covariate topics: the same topic worded differently across groups
HDP Nonparametric LDA that infers the number of topics
DTM Dynamic topics that evolve across time slices
SupervisedLDA Topics shaped to predict a per-document response
PT / GSDMM Short-text models for tweets, survey answers, headlines
SeededLDA / KeyATM Guided topics steered by seed words
PA / HLDA Topic hierarchies (Pachinko, nested-CRP)

Embedding-based models start from document embeddings you supply (no PyTorch, no UMAP/numba in the wheel):

Model What it's for
BERTopic Cluster document embeddings, label topics by class-TF-IDF; topic reduction and a soft per-document distribution
Top2Vec Topics as points in the embedding space; topic words are the nearest word vectors
ETM Generative LDA with the topic-word distribution factored through embeddings (β = softmax(ρ·α)); per-document EM or an amortized VAE (inference="vae")
FASTopic Topics read off two optimal-transport plans between document, topic, and word embeddings

Every model exposes the same shape: fit(docs, …), then topic_word (φ), doc_topic (θ), top_words(n), and save/load. The count-based variational models (CTM/STM/SupervisedLDA/DTM) parallelize across cores while staying bit-for-bit deterministic. The embedding models split into two kinds: BERTopic and Top2Vec run the reduce → cluster → represent pipeline, while ETM and FASTopic are generative and mixed-membership; all of them take vectors from any embedder (sentence-transformers, an API, a local model such as ollama). Full guides: the models and embedding topics.

Diagnostics & analysis

Model-agnostic: they work on any fitted model's topic_word/doc_topic:

  • Quality: coherence (u_mass, c_v, c_uci, c_npmi; computed in the Rust core), exclusivity, topic_diversity, quality_frontier
  • Labeling: label_topics (prob / FREX / lift / score), frex, relevance, find_thoughts, topic_table, summary
  • Validation: word_intrusion, document_intrusion, bootstrap_stability, search_k
  • Comparison: fighting_words (weighted log-odds) for contrasting corpora
  • stm toolkit: estimate_effect (method of composition, cluster-robust SEs, GLM links), posterior_theta_samples, spline, interaction, one_hot, topic_correlation
  • Preprocessing: tokenize, learn_phrases / apply_phrases, split_documents, the Corpus class

See diagnostics and covariate effects.

Performance

topica runs on a parallel Rust core. With multithreading it fits the standard models 2 to 11 times faster than the reference R and Java implementations, and it matches MALLET's hand-tuned Java sampler core for core. On the political-blog corpus (2,000 documents, fit time only, same iterations on both sides):

Model Reference topica speedup
STM R stm 11×
LDA Java MALLET parity single-threaded, 2.5× multithreaded
keyATM R keyATM parity single-threaded, multithreaded

Every fit is reproducible from a fixed seed and validated against its reference. See Benchmarks for the full methodology, and reproduce the table with python benchmarks/speed_vs_r.py.

Install from source

pip install maturin
git clone https://github.com/nealcaren/topica && cd topica
python -m venv .venv && source .venv/bin/activate
maturin develop --release --features python

Requires numpy >= 1.21. Use --release (the debug build is much slower).

Acknowledgements

Topica stands on a generation of open topic-modeling research and code. Each entry below lists the reference, its authors and year, and the topica class(es) it underlies; the other models are Rust ports or reimplementations, validated against these reference implementations.

  • MALLET (McCallum, 2002) — LDA, DMR, LabeledLDA: the SparseLDA sampler, Dirichlet-multinomial regression, and hyperparameter optimization. LDA binds David Mimno's RustMallet (Apache-2.0) and reproduces MALLET's train output bit-for-bit
  • stm (Roberts, Stewart & Tingley, 2019) — STM, CTM, SAGE: variational EM, estimateEffect, searchK, FREX, spectral initialization, and the method of composition
  • lda-c / ctm-c / dtm and hdp (Blei lab, 2006–2007) — CTM, DTM, HDP: the CTM, Dynamic Topic Model, and HDP samplers
  • gensim (Řehůřek & Sojka, 2010) — DTM: coherence measures and the LdaSeqModel DTM reference
  • tomotopy (bab2min, 2020) — API conventions (summary, the short-text models)
  • keyATM (Eshima, Imai & Sasaki, 2024) — KeyATM: the base, covariate, and dynamic models, the information-theory token weighting, and the Chib (1998) change-point HMM, validated against the package
  • seededlda (Watanabe, 2023) — SeededLDA: the seeded-prior scheme
  • LightLDA (Yuan et al., 2015) — LDA: the alias-table Metropolis-Hastings sampler
  • GSDMM (Yin & Wang, 2014) — GSDMM: the movie-group-process mixture for short text
  • BERTopic (Grootendorst, 2022) and Top2Vec (Angelov, 2020) — BERTopic, Top2Vec: the embedding-clustering pipeline, class-based TF-IDF, and the reduce → cluster → represent design
  • ETM (Dieng, Ruiz & Blei, 2020) — ETM: the Embedded Topic Model (per-document variational EM and an amortized VAE)
  • FASTopic (Wu et al., 2024) — FASTopic: the optimal-transport topic model

The embedding-native models build on two pure-Rust crates: petal-clustering for HDBSCAN and umap-rs for the optional UMAP reducer, both BLAS-free.

Full citations for every model and reference implementation, and how to cite topica, are on the Citing page.

License

Apache-2.0 — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

topica-0.9.0.tar.gz (2.6 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

topica-0.9.0-cp39-abi3-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.9+Windows x86-64

topica-0.9.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

topica-0.9.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

topica-0.9.0-cp39-abi3-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

topica-0.9.0-cp39-abi3-macosx_10_12_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file topica-0.9.0.tar.gz.

File metadata

  • Download URL: topica-0.9.0.tar.gz
  • Upload date:
  • Size: 2.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for topica-0.9.0.tar.gz
Algorithm Hash digest
SHA256 3ca820e46b2c312eb224024da1ccdaa0bd993b4bbc4ac188d2a8e72a95914e75
MD5 50394ba55753372a91c0ec51781d1cdc
BLAKE2b-256 05bb8d02a8e946321e0303dfd6f31392502ef96a7c2c6d247422166f9ddfbbf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for topica-0.9.0.tar.gz:

Publisher: CI.yml on nealcaren/topica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file topica-0.9.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: topica-0.9.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for topica-0.9.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f35eff9c8a01b2649beaeb63bd8ab86168935dc1c93cc7325df82eff7dba85ee
MD5 e8275ab057beefd22d092009b3443773
BLAKE2b-256 6d587c9f818d09b2900c4f32869b7bdbb092cb36b142abc3126020af25fd93aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for topica-0.9.0-cp39-abi3-win_amd64.whl:

Publisher: CI.yml on nealcaren/topica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file topica-0.9.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for topica-0.9.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6ab9c75ea1f295a8c39563e42dd1775f16d92b497e578516959016db3b2f595f
MD5 e81fd2e27a7f47f07b99155f8e6f98d7
BLAKE2b-256 10c4b82420f5e8e3880daa29ba471b9ead851d321c35aee4c9ba7094137ac0fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for topica-0.9.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on nealcaren/topica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file topica-0.9.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for topica-0.9.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 bfec545e45c633c561907abfda29ee8c91c8b30f752fe3801b64937c02cb0322
MD5 a75725c39535fc3190167e4a4ce09fb2
BLAKE2b-256 47d0081349312f4dfd0a8e4b3e62d801f4f5005673d2c60c72a20781e1a1df15

See more details on using hashes here.

Provenance

The following attestation bundles were made for topica-0.9.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: CI.yml on nealcaren/topica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file topica-0.9.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for topica-0.9.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f0d16ca45f1abc7105692c3d9dd576018a1c58390aa8e046411c305e21eec48
MD5 e6e26e86f4e58cb9d96dc4642a9017c7
BLAKE2b-256 9512cdce82eb8d423848a3e566b2c64fb003cf8bf57296ee8f1f0bca8e5fe064

See more details on using hashes here.

Provenance

The following attestation bundles were made for topica-0.9.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: CI.yml on nealcaren/topica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file topica-0.9.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for topica-0.9.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4a476f4a98c8c010c01f9b3fb554b527d3d0095b890540365bf84816cce4ba46
MD5 420976bb8ccb1de78a39a83d8e1e4947
BLAKE2b-256 54fadb07fd8aad4b4afa4726c83653226419b42315cd0355521ace4279a3b491

See more details on using hashes here.

Provenance

The following attestation bundles were made for topica-0.9.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: CI.yml on nealcaren/topica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page