-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_knowledge_graph.py
More file actions
209 lines (164 loc) · 6.47 KB
/
build_knowledge_graph.py
File metadata and controls
209 lines (164 loc) · 6.47 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
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
"""
Build Knowledge Graph - Full Integration Script
Processes documents, extracts entities and relationships, and builds Neo4j graph
"""
import sys
from pathlib import Path
import json
from typing import List, Dict
import time
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from backend.core.document_processor import DocumentProcessor
from backend.core.nlp_processor import NLPProcessor
from backend.core.relationship_extractor import RelationshipExtractor
from backend.database.neo4j_manager import Neo4jManager
from loguru import logger
def build_knowledge_graph(
sample_dir: str = "data/sample/txt",
clear_database: bool = True
):
"""
Build complete knowledge graph from sample documents
Args:
sample_dir: Directory containing sample documents
clear_database: Whether to clear existing database
"""
print("\n" + "="*80)
print("BUILDING KNOWLEDGE GRAPH")
print("="*80)
# Initialize processors
print("\n[1/6] Initializing processors...")
doc_processor = DocumentProcessor()
nlp_processor = NLPProcessor()
rel_extractor = RelationshipExtractor(nlp=nlp_processor.nlp)
neo4j_manager = Neo4jManager()
print("[OK] All processors initialized")
# Clear database if requested
if clear_database:
print("\n[2/6] Clearing existing database...")
neo4j_manager.clear_database()
print("[OK] Database cleared")
else:
print("\n[2/6] Keeping existing database content")
# Get all sample files
sample_path = Path(sample_dir)
if not sample_path.exists():
print(f"[ERROR] Sample directory not found: {sample_dir}")
return
sample_files = list(sample_path.glob("*.txt"))
if not sample_files:
print(f"[ERROR] No .txt files found in {sample_dir}")
return
print(f"\n[3/6] Processing {len(sample_files)} documents...")
all_entities = []
all_relationships = []
# Process each document
for i, file_path in enumerate(sample_files, 1):
print(f"\n [{i}/{len(sample_files)}] {file_path.name}")
# Extract text
doc_result = doc_processor.process_document(str(file_path))
if not doc_result.get('success'):
print(f" [ERROR] Failed to extract text")
continue
text = doc_result['text']
print(f" - Extracted {len(text)} characters")
# Extract entities
doc_entities = nlp_processor.process_document(
text=text,
document_id=f"doc_{i}",
filename=file_path.name
)
entities = doc_entities.entities
print(f" - Extracted {len(entities)} entities")
# Extract relationships
relationships = rel_extractor.extract_relationships(text, entities)
print(f" - Extracted {len(relationships)} relationships")
all_entities.extend(entities)
all_relationships.extend(relationships)
print(f"\n[OK] Processed all documents")
print(f" Total entities: {len(all_entities)}")
print(f" Total relationships: {len(all_relationships)}")
# Create entity nodes in Neo4j
print(f"\n[4/6] Creating entity nodes in Neo4j...")
start_time = time.time()
created_entities = neo4j_manager.batch_create_entities(all_entities)
elapsed = time.time() - start_time
print(f"[OK] Created {created_entities} entity nodes in {elapsed:.2f}s")
# Create relationships in Neo4j
print(f"\n[5/6] Creating relationships in Neo4j...")
start_time = time.time()
created_rels = neo4j_manager.batch_create_relationships(all_relationships)
elapsed = time.time() - start_time
print(f"[OK] Created {created_rels} relationships in {elapsed:.2f}s")
# Get final statistics
print(f"\n[6/6] Knowledge Graph Statistics:")
stats = neo4j_manager.get_graph_stats()
print(f"\n Nodes:")
print(f" Total: {stats['total_nodes']}")
for node_type, count in stats['nodes_by_type'].items():
print(f" - {node_type}: {count}")
print(f"\n Relationships:")
print(f" Total: {stats['total_relationships']}")
for rel_type, count in stats['relationships_by_type'].items():
print(f" - {rel_type}: {count}")
# Save summary to JSON
output_dir = Path("data/processed")
output_dir.mkdir(parents=True, exist_ok=True)
summary_file = output_dir / "knowledge_graph_summary.json"
with open(summary_file, 'w', encoding='utf-8') as f:
json.dump({
"stats": stats,
"documents_processed": len(sample_files),
"entities_created": created_entities,
"relationships_created": created_rels,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}, f, indent=2)
print(f"\n[OK] Summary saved to: {summary_file}")
# Close Neo4j connection
neo4j_manager.close()
print("\n" + "="*80)
print("[OK] KNOWLEDGE GRAPH BUILD COMPLETE!")
print("="*80)
print("\nNext steps:")
print("1. Open Neo4j Browser: http://localhost:7474")
print("2. Run query: MATCH (n) RETURN n LIMIT 100")
print("3. Explore the graph visually!")
print("4. Try queries like:")
print(" - MATCH (p:Person)-[r:WORKS_FOR]->(o:Organization) RETURN p, r, o")
print(" - MATCH (o:Organization)-[r:LOCATED_IN]->(l:Location) RETURN o, r, l")
print("\n")
def query_example_paths():
"""
Example: Query some interesting paths in the graph
"""
print("\n" + "="*80)
print("EXAMPLE GRAPH QUERIES")
print("="*80)
neo4j_manager = Neo4jManager()
# Example queries
examples = [
("Maria Weber", "Munich"),
("Helsing", "NATO"),
("ARX Robotics", "Munich"),
]
print("\nFinding shortest paths:")
for source, target in examples:
print(f"\n Path from '{source}' to '{target}':")
path = neo4j_manager.find_shortest_path(source, target)
if path:
print(f" Length: {path['length']} hops")
print(f" Nodes: {' -> '.join(n['name'] for n in path['nodes'])}")
else:
print(f" No path found")
neo4j_manager.close()
print("\n" + "="*80)
if __name__ == "__main__":
# Build the knowledge graph
build_knowledge_graph(
sample_dir="data/sample/txt",
clear_database=True
)
# Show example queries
# query_example_paths()