Implementation:Neuml Txtai Topics
| Knowledge Sources | |
|---|---|
| Domains | Topic_Modeling, Graph_Analysis |
| Last Updated | 2026-02-09 17:00 GMT |
Overview
The Topics class performs community-based topic modeling on graph networks, using community detection algorithms combined with TF-IDF scoring to identify and label topic clusters.
Description
The Topics class operates on txtai's graph structures to discover topical communities within indexed data. It first applies community detection algorithms to identify clusters of related nodes in the graph, then uses TF-IDF scoring to extract the most representative terms for each community. These terms are merged and ranked to produce human-readable topic labels. The class also provides methods for scoring token relevance, filtering, and merging similar topics to produce a clean, interpretable topic hierarchy.
Usage
Use the Topics class when you need to automatically discover and label thematic groups within a txtai graph. This is particularly useful for exploratory data analysis, content organization, and building topic-based navigation over large document collections. It is typically invoked internally through txtai's graph configuration rather than instantiated directly.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File: src/python/txtai/graph/topics.py
- Lines: 1-166
Signature
class Topics:
def __init__(self, config):
"""
Creates a new Topics instance.
Args:
config: topic configuration dict with parameters for
community detection and term extraction
"""
def __call__(self, graph):
"""
Runs topic modeling on the provided graph.
Args:
graph: txtai graph instance with nodes and edges
Returns:
dict mapping topic names (str) to lists of node ids
"""
def score(self, texts):
"""Computes TF-IDF scores for tokens across community texts."""
def tokenize(self, text):
"""Tokenizes text for TF-IDF scoring."""
def topn(self, scores, n):
"""Returns top-n scoring tokens from a score dict."""
def merge(self, topics):
"""Merges similar topics that share significant term overlap."""
Import
from txtai.graph import Topics # internal usage
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | dict | Yes | Configuration dictionary controlling community detection parameters, number of top terms, and merge thresholds |
| graph | Graph | Yes (for __call__) | txtai Graph instance containing nodes (documents) and edges (relationships) to analyze |
Outputs
| Name | Type | Description |
|---|---|---|
| topics | dict[str, list] | Dictionary mapping topic name strings (derived from top TF-IDF terms) to lists of node IDs belonging to each topic community |
Usage Examples
Basic Usage
from txtai.embeddings import Embeddings
# Create embeddings with graph and topic support
embeddings = Embeddings(
path="sentence-transformers/all-MiniLM-L6-v2",
content=True,
graph={
"topics": {
"terms": 4, # Number of terms per topic label
"resolution": 1.0 # Community detection resolution
}
}
)
# Index documents
data = [
"Machine learning for image classification",
"Deep learning convolutional neural networks",
"Natural language processing with BERT",
"Text summarization using transformers",
"Reinforcement learning in robotics",
"Autonomous robot navigation systems"
]
embeddings.index(data)
# Access discovered topics from the graph
graph = embeddings.graph
if graph.topics:
for topic_name, node_ids in graph.topics.items():
print(f"Topic: {topic_name}")
print(f" Members: {node_ids}")
Topic Exploration
from txtai.embeddings import Embeddings
# Build embeddings with content and graph topics
embeddings = Embeddings(
path="sentence-transformers/all-MiniLM-L6-v2",
content=True,
graph={
"topics": {
"terms": 3,
"stopwords": True
}
}
)
# Index a document collection
documents = [
{"text": "Python web development with Flask", "category": "web"},
{"text": "Django REST framework tutorial", "category": "web"},
{"text": "Data analysis with pandas", "category": "data"},
{"text": "NumPy array operations guide", "category": "data"},
{"text": "TensorFlow model training", "category": "ml"},
{"text": "PyTorch deep learning basics", "category": "ml"}
]
embeddings.index(documents)
# Explore topics discovered through community detection
graph = embeddings.graph
for topic, members in graph.topics.items():
print(f"\n== {topic} ==")
for member_id in members:
print(f" - Document {member_id}")