semantica.kg transforms extracted entities and relationships into structured, queryable knowledge graphs:
  • Temporal nodes and edges with valid_from / valid_until windows and all 13 Allen interval relations
  • Full graph analytics suite: centrality, community detection, path finding, link prediction
  • Node2Vec structural embeddings for downstream ML and similarity scoring
  • OWL-Time export and versioned snapshots via TemporalVersionManager
  • Schema and constraint validation before persistence

Exported Classes

ClassRole
KnowledgeGraphCore graph data structure: nodes, edges, properties, temporal validity
GraphBuilderConstruct from entities + relationships; pass merge_entities=True to enable deduplication
GraphBuilderWithProvenanceWraps GraphBuilder with optional provenance tracking; pass provenance=True to enable
EntityResolverEntity deduplication and merging during graph construction
GraphAnalyzerUnified analytics wrapper: runs centrality, community detection, and connectivity in one call
ConnectivityAnalyzerConnected component detection, bridge identification, density, and degree statistics
TemporalGraphQueryPoint-in-time snapshots, temporal diffs, and all 13 Allen interval queries
CentralityCalculatorPageRank, degree, betweenness, closeness, eigenvector centrality
CommunityDetectorLouvain, Leiden, Label Propagation, and K-Clique community detection
PathFinderDijkstra, A*, BFS, and K-Shortest path algorithms
LinkPredictorPreferential Attachment, Jaccard, Adamic-Adar link prediction
NodeEmbedderNode2Vec structural embeddings for downstream ML
SimilarityCalculatorCosine, Euclidean, Manhattan, and correlation similarity scoring
GraphValidatorSchema and constraint validation before persistence
For conflict detection and advanced entity resolution, use semantica.conflicts and semantica.deduplication alongside this module.
Knowledge graph entity and relation structure: Person, Organization, Location, Date nodes with typed labeled edges

GraphBuilder

GraphBuilder constructs knowledge graphs from extracted entities and relationships. merge_entities defaults to False: pass True to enable entity deduplication during construction:
from semantica.kg import GraphBuilder

# Pass a dict with "entities" and "relationships" keys
builder = GraphBuilder(merge_entities=True)
kg = builder.build({"entities": entities, "relationships": relationships})
MethodReturnsDescription
build(sources)dictBuild graph from a dict, list of dicts, or list of entity/relation objects
build_single_source(data)dictBuild graph from a single data source dict

Temporal Knowledge Graphs (v0.4.0)

Use TemporalGraphQuery to attach valid_from/valid_until windows and query point-in-time snapshots of any graph:
from semantica.kg import GraphBuilder, TemporalGraphQuery, TemporalVersionManager
from datetime import datetime

# Build a time-aware graph
builder = GraphBuilder()
kg = builder.build(sources=[
    {
        "entities": [
            {"id": "alice",     "type": "Person"},
            {"id": "acme_corp", "type": "Organization"},
        ],
        "relationships": [
            {
                "source": "alice", "target": "acme_corp", "type": "ceo_of",
                "valid_from":  "2020-01-01",
                "valid_until": "2023-06-01",
            }
        ]
    }
])

# Point-in-time snapshot: TemporalGraphQuery takes no positional graph arg;
# pass the graph into each query method instead.
query         = TemporalGraphQuery()
snapshot_2021 = query.reconstruct_at_time(kg, "2021-06-15")
snapshot_2023 = query.reconstruct_at_time(kg, "2023-01-01")

# Relationships active within a date range
range_result = query.query_time_range(kg, "", "2020-01-01", "2023-01-01")
print(f"Relationships in range: {range_result['num_relationships']}")

# Versioned snapshots: author and description are required
versioner = TemporalVersionManager()
versioner.create_snapshot(kg, version_label="2024-Q1",
                          author="user@example.com",
                          description="Q1 2024 snapshot")
Supports all 13 Allen interval algebra relations:
  • before, after, meets, met_by
  • overlaps, overlapped_by
  • during, contains, starts, started_by, finishes, finished_by, equals
OWL-Time export available.

Similarity Scoring

SimilarityCalculator computes cosine, Euclidean, Manhattan, and correlation similarity between node embeddings:
from semantica.kg import SimilarityCalculator, NodeEmbedder

# First compute structural embeddings
embedder   = NodeEmbedder(method="node2vec", embedding_dimension=128)
embeddings = embedder.compute_embeddings(kg, ["Person", "Organization"], ["RELATED_TO"])

# Then compare nodes by embedding similarity
calc  = SimilarityCalculator()
score = calc.cosine_similarity(embeddings["Apple Inc."], embeddings["Google"])
print(f"Apple–Google structural similarity: {score:.3f}")

# Find structurally similar nodes: returns List[str] of node IDs
similar = embedder.find_similar_nodes(kg, "Apple Inc.", top_k=5)
for node_id in similar:
    print(node_id)

Graph Analytics

Measure node importance across five algorithms. Use calculate_all_centrality() to run them all at once.
from semantica.kg import CentralityCalculator

calculator = CentralityCalculator()

# Run all centrality measures at once
all_metrics = calculator.calculate_all_centrality(graph)

# Or run individually
pagerank    = calculator.calculate_pagerank(graph, damping_factor=0.85)
betweenness = calculator.calculate_betweenness_centrality(graph)
closeness   = calculator.calculate_closeness_centrality(graph)

# Get the top 10 most important nodes
top_nodes = calculator.get_top_nodes(pagerank, top_k=10)
MethodBest for
calculate_degree_centrality()Most-connected nodes
calculate_pagerank()Link-based influence (like Google PageRank)
calculate_betweenness_centrality()Bottleneck / bridge nodes
calculate_closeness_centrality()Nodes closest to all others
calculate_eigenvector_centrality()Nodes connected to other high-influence nodes

Algorithm Summary

CategoryAlgorithmsUse Cases
Node EmbeddingsNode2VecStructural similarity, node representation
SimilarityCosine, Euclidean, Manhattan, CorrelationNode matching, recommendation
Path FindingDijkstra, A*, BFS, K-ShortestRoute planning, network analysis
Link PredictionPreferential Attachment, Jaccard, Adamic-AdarNetwork completion
CentralityDegree, Betweenness, Closeness, PageRankInfluence analysis
Community DetectionLouvain, Leiden, Label PropagationSocial clustering
ConnectivityComponents, Bridges, DensityNetwork robustness

GraphValidator

Validates graph structure: checks required fields, duplicate IDs, dangling edges, and optionally detects cycles and orphan nodes:
from semantica.kg import GraphValidator

validator = GraphValidator()
result    = validator.validate(kg)   # accepts the dict returned by GraphBuilder.build()

if result.is_valid:
    print("Graph is valid")
else:
    for issue in result.issues:
        print(f"{issue.severity.value}: {issue.message}")
Pass strict=True to treat warnings as errors. Pass a schema dict with "entity_types" and "relationship_types" keys to validate against a known type vocabulary.

Configuration

kg:
  resolution:
    threshold: 0.9
    strategy: semantic

  temporal:
    enabled: true
    default_validity: infinite

Graph Store

Persist graphs in Neo4j, FalkorDB, or Apache AGE.

Semantic Extract

Source of entities and relationships fed to GraphBuilder.

Visualization

Visualize knowledge graphs interactively.

Conflicts

Conflict detection and resolution.

Cookbooks