Overview
TheKnowledgeGraph 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
str | Path
required
Path to the graph JSON file (typically
graph_data.json)KnowledgeGraph instance
Instance Methods
add_entity
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
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 settingstr | None
default:"None"
Override the instance-level
confidence_aggregation methodbool - True if added successfully, False if source/target missing
get_entity
str
required
Entity identifier
dict[str, Any] | None - Entity data dict or None if not found
get_relations
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
list[dict[str, Any]] - List of relation dicts with source, target, and all edge attributes
save
str | Path
required
Output file path
export
bool
default:"True"
Include individual relation mentions (can be large for graphs with many duplicate relations)
dict[str, Any] with keys:
metadata: Graph metadata (entity count, relation count, timestamps, etc.)nodes: List of entity dictslinks: List of relation dicts
Properties
entity_count
relation_count
Complete Example
NetworkX Integration
The underlying NetworkX graph is accessible viakg.graph: