-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledge_Graph_Navigator.py
More file actions
34 lines (29 loc) · 1.21 KB
/
Copy pathKnowledge_Graph_Navigator.py
File metadata and controls
34 lines (29 loc) · 1.21 KB
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
from neo4j import GraphDatabase
import logging
class KnowledgeGraphNavigator:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def get_semantic_context(self, terms: list):
"""
Path A: Map tokens to the Etymological Graph.
Returns the 'Core Concept Map' for the Reasoning Bridge.
"""
context_map = {}
with self.driver.session() as session:
for term in terms:
# Optimized Cypher using the execute_read pattern for safety
query = """
MATCH (n:EtymologicalRoot {name: $term})
OPTIONAL MATCH (n)-[:DEFINES|RELATED_TO]-(related)
RETURN n.name as root, collect(related.name) as relations
"""
result = session.run(query, term=term.lower())
record = result.single()
if record:
context_map[record["root"]] = record["relations"]
return context_map
if __name__ == "__main__":
# Integration into RootAI Flow
kg_navigator = KnowledgeGraphNavigator("bolt://localhost:7687", "neo4j", "rootai")