Spaces:
Sleeping
Sleeping
File size: 702 Bytes
0e365b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # backend/app/services/graph_builder.py
import networkx as nx
from typing import List, Dict
def build_graph_from_triplets(triplets: List[Dict]) -> nx.DiGraph:
G = nx.DiGraph()
for triplet in triplets:
subject = triplet["subject"]
predicate = triplet["predicate"]
obj = triplet["object"]
G.add_node(subject)
G.add_node(obj)
G.add_edge(subject, obj, label=predicate)
return G
def export_graph_as_edges(graph: nx.DiGraph) -> List[Dict]:
edges = []
for u, v, data in graph.edges(data=True):
edges.append({
"from": u,
"to": v,
"relation": data.get("label", "")
})
return edges
|