Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3b3bfa6
Added code for vectorizer merging and vocabulary updates
x-tabdeveloping Jul 9, 2026
405884e
Removed latent term import from vectorizers init
x-tabdeveloping Jul 9, 2026
e05099e
Sped up SNMF drastically by relying on LAX-native loops
x-tabdeveloping Jul 9, 2026
d0148b4
Moved jitting to more sensible place, added model merging to SNMF
x-tabdeveloping Jul 19, 2026
e409c7d
Added an array of merging methods
x-tabdeveloping Jul 19, 2026
1461d2b
Added model merging partial_fit to SensTopic
x-tabdeveloping Jul 19, 2026
7eedde7
Made model merging the default partial_fit
x-tabdeveloping Jul 19, 2026
77f1721
Replaced copy calls with clone
x-tabdeveloping Jul 19, 2026
110695e
Added warnings and docstrings to merging partial_fit
x-tabdeveloping Jul 19, 2026
1c3d02d
Refactored merging to return merge history
x-tabdeveloping Jul 20, 2026
85daa03
Added WikiAnalyzer
x-tabdeveloping Aug 24, 2026
3fe165e
Added docs to wikianalyzer
x-tabdeveloping Aug 24, 2026
9801c5c
Added wikianalyzer to init
x-tabdeveloping Aug 24, 2026
d9b117a
Fixed SNMF initialization when the number of documents is smaller tha…
x-tabdeveloping Aug 24, 2026
1938e44
Fixed circular import
x-tabdeveloping Aug 24, 2026
c9f5697
Fixed bug with zipping
x-tabdeveloping Aug 24, 2026
99ef787
Added merging as partial_fit in SensTopic
x-tabdeveloping Aug 24, 2026
52e66b3
Added language selection to WikiAnalyzer
x-tabdeveloping Aug 24, 2026
c3ef130
Added partial_fit_transform to SensTopic
x-tabdeveloping Aug 25, 2026
7844176
Added optional length penalty to WikiAnalyzer
x-tabdeveloping Aug 25, 2026
3651c80
Fixed minor bug
x-tabdeveloping Aug 27, 2026
8f7df17
Updated dependencies
x-tabdeveloping Aug 27, 2026
a645182
version bump
x-tabdeveloping Aug 27, 2026
8cb7ace
Added distribution learner
x-tabdeveloping Aug 27, 2026
2bcf486
Added docs for new functionality
x-tabdeveloping Aug 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/SensTopic.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ model.print_topics()
| 4 | tennis, competing, federer, wimbledon, iaaf, olympic, tournament, athlete, rugby, olympics |
| 5 | gdp, stock, economy, earnings, investments, investment, invest, exports, finance, economies |

## Batch fitting

SensTopic models can be fit in mini-batches. This is done by fitting separate models on all batches, and continuously merging models of the new batches into the current model.
!!! info
To find more information on model merging and its variants, please look at [Model Merging](topic_merging.md).

```python
from itertools import batched

BATCH_SIZE = 2000
corpus: Iterable[str] = [...]
batches = batched(corpus, BATCH_SIZE)

model = SensTopic(
sparsity=5.0,
random_state=42,
)
for batch in batches:
batch = list(batch)
batch_doc_topic = model.partial_fit_transform(
batch, merge_method="asymmetric_mean"
)
model.print_topics()
```

## Citation

Please cite Turftopic when using the SensTopic model:
Expand Down
21 changes: 16 additions & 5 deletions docs/analyzers.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Topic Analysis with LLMs
# Topic Analysis

Topic analyzers are large language models, that are capable of interpreting topics' contents and can give human-readable descriptions of topics.
Topic analyzers are language-model based solutions, that are capable of interpreting topics' contents and can give human-readable descriptions of topics.
This can be incredibly useful when it would require excessive manual labour to label and understand topics.

<figure>
Expand All @@ -10,12 +10,11 @@ This can be incredibly useful when it would require excessive manual labour to l

Analyzers can do the following tasks:

- **Summarize documents** to make it easier for your topic model to consume.
- **Summarize documents** (*optional*) to make it easier for your topic model to consume.
- **Name topics** topics in a sensible and human-readable way based on top documents and keywords
- **Describe topics** in a couple of sentences

While previously, smaller language models were not able to meaningfully accomplish this task,
advances in in the field now allow you to generate highly accurate topic descriptions on your own laptop using the power of small LLMs.
This can either be achieved using generative, text2text or extractive language models.

!!! warning

Expand Down Expand Up @@ -45,6 +44,18 @@ There are multiple types of analyzers in Turftopic that you can utilize for thes
analyzer = LLMAnalyzer(use_summaries=True)
```

=== "WikiAnalyzer (experimental)"

You can use the Wikipedia API to search for candidate topic names and descriptions and retrieve the best one using an encoder-style language model. By default, this uses the `encoder` of a topic model:
**NOTE:** The WikiAnalyzer can only name and describe topics and cannot summarize documents.

```python
from turftopic.analyzers import WikiAnalyzer

model = SensTopic()
analyzer = WikiAnalyzer(model)
```

=== "OpenAI API"

You will have to install OpenAI, as it is not installed by default:
Expand Down
53 changes: 53 additions & 0 deletions docs/distribution_learning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Topic Distribution Learning

While in most scenarios you can store an entire document-topic matrix in memory, this is not always the case, especially with extremely large datasets.
Distribution learners in Turftopic are exactly developed for this reason.

With a distribution learner, you can pass document-topic matrices per batch, and update its parameters, while slowly learning the true distribution of topics in the dataset with uncertainty.

To use distribution learners you should install `conjugate-models`:

```bash
pip install turftopic[conjugate]
```

## Example

```python
import numpy as np
import pandas as pd

from sklearn.datasets import fetch_20newsgroups
from turftopic import SensTopic
from turftopic.distribution_learning import GaussianDistributionLearner

ds = fetch_20newsgroups(remove=("headers", "footers", "quotes"), subset="all")
corpus = ds.data

batch_size = 2000
model = SensTopic(random_state=42)
# Initializing the distribution learner
distribution_learner = GaussianDistributionLearner()
# batch fitting over the dataset
for batch_start in range(0, len(corpus), batch_size):
batch_end = batch_start + batch_size
# Calculating doc_topic_matrix for current batch
batch_doc_topic_matrix = model.partial_fit_transform(
corpus[batch_start:batch_end],
merge_method="asymmetric_mean",
)
# Updating the posteriors
distribution_learner.update(batch_doc_topic_matrix)

# `pip install plotly` if you want to plot
distribution_learner.plot_topic_distribution(model.topic_names)
```

<figure>
<iframe src="../images/distribution_learner.html", title="Learned topic distribution", style="height:420px;width:900px;padding:0px;border:none;"></iframe>
<figcaption> Topic distribution learned by the GaussianDistributionLearner. </figcaption>
</figure>

## API Reference

::: turftopic.distribution_learning.GaussianDistributionLearner
Binary file added docs/images/asymmetric_merge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3,888 changes: 3,888 additions & 0 deletions docs/images/distribution_learner.html

Large diffs are not rendered by default.

Loading
Loading