File size: 8,424 Bytes
8f05ad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
Knowledge Graph Implementation

Graph-based knowledge representation using networkx.
"""

from typing import List, Dict, Optional, Any, Set, Tuple
import networkx as nx
import numpy as np
from datetime import datetime
import json
from pathlib import Path


class KnowledgeGraph:
    """Graph-based knowledge representation with entities and relationships."""

    def __init__(
        self,
        max_nodes: int = 10000,
        max_edges: int = 50000,
    ):
        """
        Initialize the knowledge graph.

        Args:
            max_nodes: Maximum number of nodes
            max_edges: Maximum number of edges
        """
        self.max_nodes = max_nodes
        self.max_edges = max_edges
        self.graph = nx.MultiDiGraph()
        self._node_attributes: Dict[str, Dict[str, Any]] = {}
        self._edge_attributes: Dict[Tuple[str, str], Dict[str, Any]] = {}

    def add_entity(
        self,
        entity_id: str,
        entity_type: str,
        properties: Optional[Dict[str, Any]] = None,
    ) -> bool:
        """
        Add an entity to the knowledge graph.

        Args:
            entity_id: Unique identifier for the entity
            entity_type: Type of entity (e.g., 'person', 'concept', 'code')
            properties: Additional properties

        Returns:
            True if added, False if limit reached
        """
        if self.graph.number_of_nodes() >= self.max_nodes:
            return False

        if entity_id not in self.graph:
            self.graph.add_node(entity_id, type=entity_type)

        self._node_attributes[entity_id] = {
            "type": entity_type,
            "created_at": datetime.now().isoformat(),
            "properties": properties or {},
        }

        return True

    def add_relationship(
        self,
        source_id: str,
        target_id: str,
        relationship_type: str,
        properties: Optional[Dict[str, Any]] = None,
    ) -> bool:
        """
        Add a relationship between entities.

        Args:
            source_id: Source entity ID
            target_id: Target entity ID
            relationship_type: Type of relationship
            properties: Additional properties

        Returns:
            True if added, False if limit reached or entities don't exist
        """
        if self.graph.number_of_edges() >= self.max_edges:
            return False

        # Ensure entities exist
        if source_id not in self.graph:
            self.add_entity(source_id, "unknown")
        if target_id not in self.graph:
            self.add_entity(target_id, "unknown")

        self.graph.add_edge(source_id, target_id, type=relationship_type)

        edge_key = (source_id, target_id)
        self._edge_attributes[edge_key] = {
            "type": relationship_type,
            "created_at": datetime.now().isoformat(),
            "properties": properties or {},
        }

        return True

    def get_entity(self, entity_id: str) -> Optional[Dict[str, Any]]:
        """Get entity information."""
        if entity_id not in self.graph:
            return None

        return {
            "id": entity_id,
            **self._node_attributes.get(entity_id, {}),
        }

    def get_relationships(
        self,
        entity_id: str,
        relationship_type: Optional[str] = None,
    ) -> List[Dict[str, Any]]:
        """Get relationships for an entity."""
        if entity_id not in self.graph:
            return []

        relationships = []
        for source, target, data in self.graph.edges(data=True):
            if source == entity_id or target == entity_id:
                rel_type = data.get("type", "unknown")
                if relationship_type and rel_type != relationship_type:
                    continue

                relationships.append({
                    "source": source,
                    "target": target,
                    "type": rel_type,
                })

        return relationships

    def find_similar_entities(
        self,
        entity_id: str,
        max_results: int = 5,
    ) -> List[Tuple[str, float]]:
        """
        Find similar entities using graph-based similarity.

        Args:
            entity_id: Entity to find similar
            max_results: Maximum number of results

        Returns:
            List of (entity_id, similarity_score) tuples
        """
        if entity_id not in self.graph:
            return []

        # Use common neighbors as simple similarity
        neighbors = set(self.graph.neighbors(entity_id))
        scores = []

        for node in self.graph.nodes():
            if node == entity_id:
                continue

            node_neighbors = set(self.graph.neighbors(node))
            common = len(neighbors & node_neighbors)

            if common > 0:
                # Jaccard-like similarity
                union = len(neighbors | node_neighbors)
                score = common / union if union > 0 else 0
                scores.append((node, score))

        scores.sort(key=lambda x: -x[1])
        return scores[:max_results]

    def search_entities(
        self,
        entity_type: Optional[str] = None,
        property_filter: Optional[Dict[str, Any]] = None,
    ) -> List[str]:
        """
        Search for entities.

        Args:
            entity_type: Filter by entity type
            property_filter: Filter by properties

        Returns:
            List of matching entity IDs
        """
        results = []

        for node in self.graph.nodes():
            attrs = self._node_attributes.get(node, {})

            # Check type filter
            if entity_type and attrs.get("type") != entity_type:
                continue

            # Check property filter
            if property_filter:
                props = attrs.get("properties", {})
                if not all(props.get(k) == v for k, v in property_filter.items()):
                    continue

            results.append(node)

        return results

    def get_subgraph(
        self,
        entity_ids: List[str],
        depth: int = 1,
    ) -> nx.MultiDiGraph:
        """
        Get a subgraph around specified entities.

        Args:
            entity_ids: Center entities
            depth: How many hops to include

        Returns:
            Subgraph
        """
        nodes = set(entity_ids)

        for _ in range(depth):
            for entity in list(nodes):
                nodes.update(self.graph.neighbors(entity))

        return self.graph.subgraph(nodes).copy()

    def export_json(self, filepath: str) -> None:
        """Export graph to JSON."""
        data = {
            "nodes": [
                {
                    "id": node,
                    **self._node_attributes.get(node, {}),
                }
                for node in self.graph.nodes()
            ],
            "edges": [
                {
                    "source": source,
                    "target": target,
                    "type": data.get("type", "unknown"),
                }
                for source, target, data in self.graph.edges(data=True)
            ],
        }

        Path(filepath).write_text(json.dumps(data, indent=2))

    def import_json(self, filepath: str) -> None:
        """Import graph from JSON."""
        data = json.loads(Path(filepath).read_text())

        for node_data in data.get("nodes", []):
            node_id = node_data.pop("id")
            self.add_entity(node_id, node_data.get("type", "unknown"), node_data.get("properties"))

        for edge_data in data.get("edges", []):
            self.add_relationship(
                edge_data["source"],
                edge_data["target"],
                edge_data.get("type", "unknown"),
            )

    def get_stats(self) -> Dict[str, Any]:
        """Get graph statistics."""
        return {
            "num_nodes": self.graph.number_of_nodes(),
            "num_edges": self.graph.number_of_edges(),
            "num_node_types": len(set(
                attrs.get("type") for attrs in self._node_attributes.values()
            )),
            "num_edge_types": len(set(
                data.get("type") for _, _, data in self.graph.edges(data=True)
            )),
        }

    def __repr__(self) -> str:
        stats = self.get_stats()
        return f"KnowledgeGraph(nodes={stats['num_nodes']}, edges={stats['num_edges']})"