Graph-based topic modeling using embeddings, Leiden clustering and semantic merging
Project description
XidooTopic
Graph-based topic modeling using embeddings, Leiden clustering, and semantic merging
🚀 Overview
XidooTopic is a modular framework for unsupervised topic modeling built on top of modern NLP embeddings and graph-based clustering. It transforms raw text corpora into coherent, semantically meaningful topics through a pipeline that combines:
- Transformer-based embeddings
- Dimensionality reduction (UMAP)
- Outlier detection (Isolation Forest)
- Graph construction (k-NN via FAISS)
- Community detection (Leiden algorithm)
- Semantic cluster merging
- Keyword extraction (KeyBERT)
📦 Installation
pip install xidootopic
Optional dependencies:
pip install xidootopic[nlp]
pip install xidootopic[llm]
🧠 Pipeline Summary
The method follows a structured six-stage pipeline:
-
Text Preprocessing
Raw reviews are cleaned, normalized, and filtered to remove noise and low-information samples. -
Embedding Generation
Each document is mapped into a dense semantic vector space using transformer-based models. -
Dimensionality Reduction
UMAP reduces embeddings to a low-dimensional manifold while preserving local structure. -
Outlier Detection
Isolation Forest removes anomalous samples that may distort clustering structure. -
Graph Construction & Clustering
A k-NN graph is built using FAISS and clustered using the Leiden algorithm with resolution optimization. -
Postprocessing
Clusters are refined via:- Removal of small clusters
- Semantic merging of similar clusters
- Keyword extraction with KeyBERT
📊 Core Ideas Behind the Method
Isolation Forest
[!NOTE] Isolation Forest assumes anomalies are easier to isolate than normal points. It builds random partition trees and assigns higher anomaly scores to points that require fewer splits to isolate.
This makes it highly effective for high-dimensional embeddings where distance-based methods degrade.
Leiden Clustering
[!NOTE] Leiden improves over Louvain by guaranteeing connected communities and better modularity optimization.
It operates in three phases:
- Local node movement
- Partition refinement
- Graph aggregation
This results in more stable and coherent topic clusters.
Semantic Cluster Merging
After initial clustering, clusters are merged based on:
- Cosine similarity between centroids
- Cluster cohesion
- Adaptive thresholds combining global and local structure
[!NOTE] This step reduces redundancy and improves topic interpretability by merging semantically overlapping clusters.
Keyword Extraction (KeyBERT)
[!NOTE] KeyBERT selects keywords based on embedding similarity rather than frequency.
Unlike TF-IDF, KeyBERT captures semantic relevance, allowing rare but meaningful terms to surface as keywords.
Implementation details:
- Only top 35% most representative documents per cluster are used
- MMR is applied to ensure diversity
- Results are lemmatized and aggregated
🧪 Features
- Modular architecture for experimentation
- Graph-based clustering pipeline
- Automatic resolution tuning for Leiden
- Semantic cluster merging
- Robust outlier filtering
- KeyBERT-based topic representation
📁 Project Structure
src/
├── embedding/
├── preprocessing/
├── modeling/
├── xidootopic/
⚙️ Configuration Example
from xidootopic.xidootopic import xidootopic
model = xidootopic(
preprocessor=TextCleaner(),
chunker=TextChunker(),
embedder=TextEmbedder(...),
reducer=UMAPReducer(),
outlier_detector=IsolationForestOutlierRemover(),
graph_builder=KNNGraphBuilder(),
clusterer=LeidenClusterer(),
cluster_merger=ClusterMerger(),
keyword_model=KeyWordExtractor(...)
)
result = model.fit_transform(texts)
📌 Key Contributions
- Graph-based topic modeling pipeline
- Hybrid global/local cluster merging strategy
- Efficient embedding-based keyword extraction
- Fully modular design for research and experimentation
📚 Methodological Foundations
- Isolation Forest — Liu et al. (2008, 2012)
- Leiden Algorithm — Traag et al. (2019)
- KeyBERT — Grootendorst (2020)
- UMAP — McInnes et al. (2018)
🧾 Summary of Process
- Clean text data
- Generate embeddings
- Reduce dimensionality
- Remove outliers
- Build similarity graph
- Apply Leiden clustering
- Optimize resolution
- Filter small clusters
- Merge semantically similar clusters
- Extract keywords and representations
📌 License
MIT License
✨ Status
[!NOTE] This project is currently in beta stage, designed for research and experimentation in topic modeling and semantic clustering.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xidootopic-0.1.0.tar.gz.
File metadata
- Download URL: xidootopic-0.1.0.tar.gz
- Upload date:
- Size: 2.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0453eff7b766e3b163f4a00edc3307059d4d08753ee76bc394c8910b2f4a1cb
|
|
| MD5 |
d999a6335a7203988279f0256e3df7df
|
|
| BLAKE2b-256 |
fd945e1a6b4227c03de520862ff38c4f350a84c402979b39e38bde46bc9511b3
|
File details
Details for the file xidootopic-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: xidootopic-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60fe0afef906c58594c84205e47ebaeea5604b8a9ae29d970850aac8b6ae0ec5
|
|
| MD5 |
186f43c5868c674b4c1c4c740e63edde
|
|
| BLAKE2b-256 |
881f1dc03df129cd8505dcc60d41fad57d43e46f8b2f3620cd46eaff4d7d4be2
|