Spaces:
Running
Running
File size: 1,608 Bytes
f39939b |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# knowledge_graph.py
import json
from typing import Dict, List, Optional
from graphviz import Digraph
class QuantumKnowledgeGraph:
"""
Represents a dynamic, multi-modal knowledge graph.
"""
def __init__(self):
self.nodes: Dict[int, Dict[str, any]] = {}
self.relations: List[Dict[str, any]] = []
self.node_counter = 0
def create_node(self, content: Dict, node_type: str) -> int:
self.node_counter += 1
self.nodes[self.node_counter] = {
"id": self.node_counter,
"content": content,
"type": node_type,
"connections": []
}
return self.node_counter
def create_relation(self, source: int, target: int, rel_type: str, strength: float = 1.0) -> None:
self.relations.append({
"source": source,
"target": target,
"type": rel_type,
"strength": strength
})
self.nodes[source]["connections"].append(target)
def visualize_graph(self, focus_node: Optional[int] = None) -> str:
dot = Digraph(engine="neato")
for nid, node in self.nodes.items():
label = f"{node['type']}\n{self._truncate_content(node['content'])}"
dot.node(str(nid), label)
for rel in self.relations:
dot.edge(str(rel["source"]), str(rel["target"]), label=rel["type"])
if focus_node:
dot.node(str(focus_node), color="red", style="filled")
return dot.source
def _truncate_content(self, content: Dict) -> str:
return json.dumps(content)[:50] + "..."
|