Skip to main content

Overview

The KnowledgeGraph class is the core data structure in sift-kg. It’s a wrapper around NetworkX’s MultiDiGraph with specialized methods for entity and relation management.

Constructor

Parameters

bool
default:"True"
If True, repeated mentions of the same source/relation/target triple are merged into one canonical edge. The confidence becomes an aggregation of all mentions.
str
default:"product_complement"
How to aggregate confidence scores across multiple mentions:
  • "product_complement": Independent weak signals reinforce (1 - ∏(1 - c))
  • "mean": Average confidence across all mentions
  • "max": Take highest confidence

Example


Class Methods

load

Load a knowledge graph from a JSON file.
str | Path
required
Path to the graph JSON file (typically graph_data.json)
Returns: KnowledgeGraph instance

Instance Methods

add_entity

Add or update an entity node. If the entity already exists, it merges the new data (takes higher confidence, extends source documents).
str
required
Unique entity identifier (e.g. "person:alice", "org:acme_corp")
str
required
Entity type (e.g. "PERSON", "ORGANIZATION", "LOCATION")
str
required
Display name for the entity
float
default:"0.5"
Extraction confidence (0.0 to 1.0)
list[str] | None
default:"None"
List of source document IDs where this entity was found
Any
Additional attributes (e.g. context, attributes dict, custom fields)

add_relation

Add a relation edge between two entities. Returns False if source or target entity doesn’t exist.
str
required
Unique relation identifier
str
required
Source entity ID
str
required
Target entity ID
str
required
Relation type (e.g. "WORKS_FOR", "LOCATED_IN", "COLLABORATES_WITH")
float
default:"0.5"
Extraction confidence (0.0 to 1.0)
str
default:""
Text evidence supporting this relation
str
default:""
Document ID where this relation was found
bool | None
default:"None"
Override the instance-level canonicalize_relations setting
str | None
default:"None"
Override the instance-level confidence_aggregation method
Returns: bool - True if added successfully, False if source/target missing

get_entity

Get entity data by ID.
str
required
Entity identifier
Returns: dict[str, Any] | None - Entity data dict or None if not found

get_relations

Get all relations for an entity.
str
required
Entity identifier
str
default:"both"
Direction to query:
  • "in": Incoming edges (entity is target)
  • "out": Outgoing edges (entity is source)
  • "both": All edges
Returns: list[dict[str, Any]] - List of relation dicts with source, target, and all edge attributes

save

Save the knowledge graph to a JSON file.
str | Path
required
Output file path

export

Export graph as a JSON-serializable dictionary.
bool
default:"True"
Include individual relation mentions (can be large for graphs with many duplicate relations)
Returns: dict[str, Any] with keys:
  • metadata: Graph metadata (entity count, relation count, timestamps, etc.)
  • nodes: List of entity dicts
  • links: List of relation dicts

Properties

entity_count

Number of entities in the graph.

relation_count

Number of relations in the graph.

Complete Example


NetworkX Integration

The underlying NetworkX graph is accessible via kg.graph: