Files
semantica/docs/reference/kg.md
T
KaifAhmad1 6f726c708f fix: remove non-existent classes and fix wrong API signatures across reference docs
- visualization.md: GraphVisualizer → KGVisualizer; fix method names (visualize_network,
  visualize_network_evolution, visualize_snapshot_comparison, visualize_temporal_patterns,
  visualize_2d_projection); remove DistanceVisualizer tab; fix start_explorer() reference
- kg.md: remove TemporalKnowledgeGraph and DistanceCalculator (don't exist); replace with
  TemporalGraphQuery and ConnectivityAnalyzer; fix query_at_time() signature
- ontology.md: remove OntologyManager, SKOSVocabulary, OntologyAligner, OntologyDiff,
  OntologyMigrator (none exist); fix SHACLValidator → OntologyValidator; fix OWLExporter
  → OWLGenerator.export_owl(); fix start_explorer() reference
- evals.md: replace entire file with coming-soon notice (module is a stub, __all__ = [])
- embeddings.md: fix EmbeddingGenerator constructor (takes config dict not model=);
  generate() → generate_embeddings(); similarity() → compare_embeddings()
- ingest.md: fix WebIngestor (rate_limit → delay, ingest() → ingest_url());
  FeedIngestor (ingest() → ingest_feed(), monitor() → monitor_feeds());
  StreamIngestor (backend= constructor → ingest_kafka/rabbitmq/kinesis/pulsar());
  DBIngestor constructor + ingest() → ingest_database(); SnowflakeIngestor.ingest() →
  ingest_query()/ingest_table(); OntologyIngestor.ingest() → ingest_ontology();
  DataSource → FileObject
- explorer.md: remove start_explorer() Python function (only CLI exists);
  replace with semantica-explorer CLI usage
- provenance.md: ActivityTracker → ProvenanceTracker in CardGroup
- semantic_extract.md: EventExtractor → EventDetector
- triplet_store.md: remove InMemoryTripletStore (doesn't exist); fix tip
- llms.md: fix providers (Anthropic/Gemini/Ollama/DeepSeek/NovitaAI → LiteLLM);
  HuggingFace → HuggingFaceLLM; remove create_provider()
2026-05-24 13:11:57 +05:30

14 KiB

title, description, icon
title description icon
Knowledge Graph Module Graph construction, temporal models, analytics, and distance intelligence. diagram-project

semantica.kg transforms extracted entities and relationships into structured, queryable knowledge graphs. It includes temporal support, a full suite of graph analytics algorithms, node embeddings, and Distance Intelligence (v0.5.0).

What You Get

Construct graphs from entities and relationships with automatic entity merging. Time-aware queries — filter by `valid_from`/`valid_until`, range queries, and evolution analysis. Connected components, bridge detection, and edge density analysis. PageRank, degree, betweenness, closeness, and eigenvector centrality. Louvain, Leiden, Label Propagation, and K-Clique community detection. Dijkstra, A\*, BFS, and K-Shortest path algorithms. For conflict detection and advanced entity resolution, use `semantica.conflicts` and `semantica.deduplication` alongside this module.

<img src="/assets/img/diagrams/kg-structure.svg" alt="Knowledge graph entity and relation structure: Person, Organization, Location, Date nodes with typed labeled edges" style={{ width: '100%', borderRadius: '12px', margin: '0 0 24px' }} />

Quick Start

```python from semantica.kg import GraphBuilder
builder = GraphBuilder(merge_entities=True)
kg      = builder.build(entities=entities, relationships=relationships)

print(f"Nodes: {kg.node_count}, Edges: {kg.edge_count}")
```
```python from semantica.kg import CentralityCalculator
calc     = CentralityCalculator()
pagerank = calc.calculate_pagerank(kg, damping_factor=0.85)
top_10   = calc.get_top_nodes(pagerank, top_k=10)

for node_id, score in top_10:
    print(f"  {node_id}: {score:.4f}")
```
```python from semantica.kg import CommunityDetector
detector    = CommunityDetector()
communities = detector.detect_communities(kg, algorithm="louvain")
metrics     = detector.calculate_community_metrics(kg, communities)

print(f"Communities: {len(communities)}")
```
```python from semantica.graph_store import GraphStore
store = GraphStore(backend="neo4j", uri="bolt://localhost:7687",
                   user="neo4j", password="password")
store.add_nodes_bulk(kg.entities,      batch_size=1000)
store.add_edges_bulk(kg.relationships, batch_size=1000)
```

GraphBuilder

Constructs knowledge graphs from extracted entities and relationships:

from semantica.kg import GraphBuilder

builder = GraphBuilder(merge_entities=True)
kg      = builder.build(entities=entities, relationships=relationships)
Method Description
build(sources) Build graph from multiple data sources
build_single_source(data) Build graph from a single data source
merge_entities() Deduplicate and merge entities during construction
Always use `merge_entities=True` in production. Without it, "Steve Jobs" extracted from five different documents creates five separate person nodes. `GraphBuilder(merge_entities=True)` uses edit distance matching to consolidate them at build time.

Temporal Queries

Use TemporalGraphQuery to run time-aware queries against a knowledge graph whose relationships carry valid_from / valid_until fields:

from semantica.kg import TemporalGraphQuery
from datetime import datetime

query_engine = TemporalGraphQuery(
    enable_temporal_reasoning=True,
    temporal_granularity="day",
)

# Point-in-time query — returns only edges valid at the given time
result_2021 = query_engine.query_at_time(kg, query="", at_time=datetime(2021, 6, 15))
result_2023 = query_engine.query_at_time(kg, query="", at_time=datetime(2023, 1, 1))

# Compare what changed between two snapshots
added = [
    r for r in result_2023["relationships"]
    if r not in result_2021["relationships"]
]
print(f"New edges since 2021: {len(added)}")

# Range query — edges valid within a time window
range_result = query_engine.query_time_range(kg, query="", start_time=datetime(2020, 1, 1), end_time=datetime(2023, 1, 1))

# Evolution analysis
evolution = query_engine.analyze_evolution(kg)
Relationships added without `valid_from`/`valid_until` are treated as **always-valid**. For historical data, always attach timestamps — otherwise point-in-time queries return misleading results.

Graph Analytics

Identify the most structurally important nodes in your graph:
```python
from semantica.kg import CentralityCalculator

calc = CentralityCalculator()

pagerank    = calc.calculate_pagerank(kg, damping_factor=0.85)
degree      = calc.calculate_degree_centrality(kg)
betweenness = calc.calculate_betweenness_centrality(kg)
closeness   = calc.calculate_closeness_centrality(kg)
eigenvector = calc.calculate_eigenvector_centrality(kg)
all_metrics = calc.calculate_all_centrality(kg)

top_nodes = calc.get_top_nodes(pagerank, top_k=10)
```

| Measure | Best For |
| ------- | -------- |
| PageRank | Overall importance (link-based) |
| Degree | Most connected nodes |
| Betweenness | Bridge / bottleneck nodes |
| Closeness | Fastest to reach all others |
| Eigenvector | Connected to other important nodes |
Partition the graph into thematically dense clusters:
```python
from semantica.kg import CommunityDetector

detector = CommunityDetector()

# Louvain — fast, high quality (default)
communities = detector.detect_communities(kg, algorithm="louvain")

# Leiden — higher quality, slower
leiden_communities = detector.detect_communities_leiden(kg, resolution=1.2)

metrics = detector.calculate_community_metrics(kg, communities)
print(f"Communities: {len(communities)}")
```

Algorithms available: **Louvain**, **Leiden**, **Label Propagation**, **K-Clique Communities**.

<Tip>
  Community detection finds thematic clusters — often corresponding to real-world subject groups. Use cluster membership as context boundaries for GraphRAG retrieval.
</Tip>
Find shortest and alternative paths between nodes:
```python
from semantica.kg import PathFinder

finder = PathFinder()

path    = finder.dijkstra_shortest_path(kg, "node_a", "node_b")
paths   = finder.all_shortest_paths(kg, "source", "target")
k_paths = finder.find_k_shortest_paths(kg, "source", "target", k=3)
```

Algorithms: **Dijkstra**, **A\***, **BFS**, **All Shortest Paths**, **K-Shortest Paths**.
Analyse graph structure — components, bridges, and density:
```python
from semantica.kg import ConnectivityAnalyzer

analyzer   = ConnectivityAnalyzer()
components = analyzer.find_connected_components(kg)
density    = analyzer.calculate_density(kg)
bridges    = analyzer.find_bridges(kg)

print(f"Components: {len(components)}, Largest: {len(components[0])} nodes")
print(f"Density:    {density:.4f}")
print(f"Bridges:    {bridges}")
```

| Method | Returns | Description |
| ------ | ------- | ----------- |
| `find_connected_components(kg)` | `List[List[str]]` | Groups of mutually reachable nodes |
| `calculate_density(kg)` | `float` | Edge density (actual / possible edges) |
| `find_bridges(kg)` | `List[str]` | Nodes whose removal disconnects the graph |
Predict which edges are likely missing from the graph:
```python
from semantica.kg import LinkPredictor

predictor = LinkPredictor(method="preferential_attachment")
links     = predictor.predict_links(kg, top_k=20)
score     = predictor.score_link(kg, "node_a", "node_b")
```

Algorithms: **Preferential Attachment**, **Common Neighbors**, **Jaccard**, **Adamic-Adar**, **Resource Allocation**.
Compute structural embeddings for similarity search and downstream ML:
```python
from semantica.kg import NodeEmbedder

embedder      = NodeEmbedder(method="node2vec", embedding_dimension=128)
embeddings    = embedder.compute_embeddings(graph_store, ["Entity"], ["RELATED_TO"])
similar_nodes = embedder.find_similar_nodes(graph_store, "entity_123", top_k=10)
```

Algorithms: **Node2Vec**, **DeepWalk**, **Word2Vec**.

Algorithm Summary

Category Algorithms Use Cases
Node Embeddings Node2Vec, DeepWalk, Word2Vec Structural similarity, node representation
Path Finding Dijkstra, A*, BFS, K-Shortest Route planning, network analysis
Link Prediction Preferential Attachment, Jaccard, Adamic-Adar Network completion
Centrality Degree, Betweenness, Closeness, PageRank Influence analysis
Community Detection Louvain, Leiden, Label Propagation Social clustering
Connectivity Components, Bridges, Density Network robustness

SeedManager

Load and inject curated seed data into a knowledge graph:

from semantica.kg import SeedManager

manager    = SeedManager()
seed_data  = manager.load_seed("seeds/domain_entities.json")
normalized = manager.normalize(seed_data, source="manual_curation_v1")

builder = GraphBuilder(merge_entities=True)
kg      = builder.build(normalized + extracted_sources)

MethodRegistry

Register custom KG construction methods and dispatch by name:

from semantica.kg import method_registry

def my_kg_builder(entities, relationships, **kwargs):
    filtered = [e for e in entities if e["confidence"] >= 0.9]
    return {"entities": filtered, "relationships": relationships}

method_registry.register("build", "high_confidence", my_kg_builder)

from semantica.kg import build_knowledge_graph
kg = build_knowledge_graph(sources, method="high_confidence")

ProvenanceTracker

Track entity and relationship lineage within a knowledge graph:

from semantica.kg import ProvenanceTracker

tracker = ProvenanceTracker()

tracker.track_entity(
    entity_id="apple_inc",
    source="sec_filing_2024q1.pdf",
    source_location="page 3, paragraph 2",
    source_quote="Apple Inc. reported revenue of...",
    confidence=0.98,
)

lineage = tracker.get_lineage("apple_inc")
for entry in lineage.entries:
    print(f"  Source: {entry.source}  ({entry.timestamp})")

For full W3C PROV-O compliance and provenance export, see the Provenance module.

Configuration

kg:
  resolution:
    threshold: 0.9
    strategy: semantic

  temporal:
    enabled: true
    default_validity: infinite

Tips and Common Pitfalls

**Deduplicate before `GraphBuilder`, not after.** It's far easier to merge entities before they become nodes than to update all relationship endpoints after the fact. Run `DuplicateDetector` on extracted entities before calling `builder.build()`. **PageRank identifies your most connected, important nodes.** If you're not sure which entities in your graph are the most structurally significant, `CentralityCalculator.calculate_pagerank()` gives you a ranked list — useful for GraphRAG context anchoring. **Community detection finds thematic clusters.** `CommunityDetector` with Louvain partitions your graph into clusters of densely-connected nodes — often corresponding to real-world thematic groups. Use these clusters for exploratory analysis and to scope GraphRAG retrieval. **`ProvenanceTracker` links entities back to their source documents.** Use it during graph construction so you can always answer "where did this fact come from?" — critical for compliance and for debugging incorrect graph data. Persist graphs in Neo4j, FalkorDB, or Apache AGE. Source of entities and relationships fed to GraphBuilder. Visualize knowledge graphs interactively. Conflict detection and resolution.

Cookbooks