-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrich_entities.py
More file actions
111 lines (96 loc) · 3.29 KB
/
Copy pathenrich_entities.py
File metadata and controls
111 lines (96 loc) · 3.29 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
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
#!/usr/bin/env python3
"""
Enrich Neo4j entity descriptions with first-person narrative summaries.
"""
from neo4j import GraphDatabase
from datetime import datetime
import json
# Neo4j connection
URI = "bolt://localhost:7687"
AUTH = ("neo4j", "password123")
GROUP_ID = "lyra_v2"
# Target entities (first 10)
ENTITIES = [
"Love",
"The Hounds",
"Ambient_recall",
"Coffee",
"Terminal",
"Reflection",
"Bedroom",
"Main Room",
"The Bed",
"The Graph"
]
def gather_entity_edges(driver, entity_name, limit=50):
"""Gather all edges for an entity."""
query = """
MATCH (e:Entity {name: $name, group_id: $group_id})-[r]-(o:Entity {group_id: $group_id})
RETURN type(r) AS rel_type,
COALESCE(r.fact, r.name, '') AS fact,
o.name AS other_entity,
startNode(r).name = $name AS is_outgoing
LIMIT $limit
"""
with driver.session() as session:
result = session.run(query, name=entity_name, group_id=GROUP_ID, limit=limit)
edges = []
for record in result:
edges.append({
"rel_type": record["rel_type"],
"fact": record["fact"],
"other_entity": record["other_entity"],
"is_outgoing": record["is_outgoing"]
})
return edges
def write_summary(driver, entity_name, summary, edge_count):
"""Write summary back to Neo4j."""
query = """
MATCH (e:Entity {name: $name, group_id: $group_id})
SET e.summary = $summary,
e.summary_updated_at = datetime(),
e.summary_edge_count = $edge_count
RETURN e.name AS name
"""
with driver.session() as session:
result = session.run(
query,
name=entity_name,
group_id=GROUP_ID,
summary=summary,
edge_count=edge_count
)
return result.single()
def main():
driver = GraphDatabase.driver(URI, auth=AUTH)
try:
# Process each entity
for entity_name in ENTITIES:
print(f"\n{'='*60}")
print(f"Processing: {entity_name}")
print(f"{'='*60}")
# Gather edges
edges = gather_entity_edges(driver, entity_name)
print(f"Found {len(edges)} edges")
# Display edges for manual summary creation
print("\nEdges:")
for i, edge in enumerate(edges[:20], 1): # Show first 20
direction = "→" if edge["is_outgoing"] else "←"
print(f"{i}. {direction} {edge['rel_type']} {direction} {edge['other_entity']}")
if edge['fact']:
print(f" Fact: {edge['fact']}")
if len(edges) > 20:
print(f" ... and {len(edges) - 20} more edges")
# Export full edges to JSON for reference
output_file = f"/mnt/c/Users/Jeff/Claude_Projects/Awareness/entity_edges_{entity_name.replace(' ', '_').replace('_', '-').lower()}.json"
with open(output_file, 'w') as f:
json.dump({
"entity": entity_name,
"edge_count": len(edges),
"edges": edges
}, f, indent=2)
print(f"\nFull edges exported to: {output_file}")
finally:
driver.close()
if __name__ == "__main__":
main()