sa9arr
commited on
Commit
·
fa6baed
1
Parent(s):
9cd52bc
Fix: Correct GraphML to JSON mapping in xml_to_json function
Browse files- examples/graph_visual_with_neo4j.py +61 -1
- lightrag/utils.py +0 -1
examples/graph_visual_with_neo4j.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
-
|
4 |
from neo4j import GraphDatabase
|
5 |
|
6 |
# Constants
|
@@ -13,6 +13,66 @@ NEO4J_URI = "bolt://localhost:7687"
|
|
13 |
NEO4J_USERNAME = "neo4j"
|
14 |
NEO4J_PASSWORD = "your_password"
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def convert_xml_to_json(xml_path, output_path):
|
18 |
"""Converts XML file to JSON and saves the output."""
|
|
|
1 |
import os
|
2 |
import json
|
3 |
+
import xml.etree.ElementTree as ET
|
4 |
from neo4j import GraphDatabase
|
5 |
|
6 |
# Constants
|
|
|
13 |
NEO4J_USERNAME = "neo4j"
|
14 |
NEO4J_PASSWORD = "your_password"
|
15 |
|
16 |
+
def xml_to_json(xml_file):
|
17 |
+
try:
|
18 |
+
tree = ET.parse(xml_file)
|
19 |
+
root = tree.getroot()
|
20 |
+
|
21 |
+
# Print the root element's tag and attributes to confirm the file has been correctly loaded
|
22 |
+
print(f"Root element: {root.tag}")
|
23 |
+
print(f"Root attributes: {root.attrib}")
|
24 |
+
|
25 |
+
data = {"nodes": [], "edges": []}
|
26 |
+
|
27 |
+
# Use namespace
|
28 |
+
namespace = {"": "http://graphml.graphdrawing.org/xmlns"}
|
29 |
+
|
30 |
+
for node in root.findall(".//node", namespace):
|
31 |
+
node_data = {
|
32 |
+
"id": node.get("id").strip('"'),
|
33 |
+
"entity_type": node.find("./data[@key='d1']", namespace).text.strip('"')
|
34 |
+
if node.find("./data[@key='d1']", namespace) is not None
|
35 |
+
else "",
|
36 |
+
"description": node.find("./data[@key='d2']", namespace).text
|
37 |
+
if node.find("./data[@key='d2']", namespace) is not None
|
38 |
+
else "",
|
39 |
+
"source_id": node.find("./data[@key='d3']", namespace).text
|
40 |
+
if node.find("./data[@key='d3']", namespace) is not None
|
41 |
+
else "",
|
42 |
+
}
|
43 |
+
data["nodes"].append(node_data)
|
44 |
+
|
45 |
+
for edge in root.findall(".//edge", namespace):
|
46 |
+
edge_data = {
|
47 |
+
"source": edge.get("source").strip('"'),
|
48 |
+
"target": edge.get("target").strip('"'),
|
49 |
+
"weight": float(edge.find("./data[@key='d5']", namespace).text)
|
50 |
+
if edge.find("./data[@key='d5']", namespace) is not None
|
51 |
+
else 0.0,
|
52 |
+
"description": edge.find("./data[@key='d6']", namespace).text
|
53 |
+
if edge.find("./data[@key='d6']", namespace) is not None
|
54 |
+
else "",
|
55 |
+
"keywords": edge.find("./data[@key='d7']", namespace).text
|
56 |
+
if edge.find("./data[@key='d7']", namespace) is not None
|
57 |
+
else "",
|
58 |
+
"source_id": edge.find("./data[@key='d8']", namespace).text
|
59 |
+
if edge.find("./data[@key='d8']", namespace) is not None
|
60 |
+
else "",
|
61 |
+
}
|
62 |
+
data["edges"].append(edge_data)
|
63 |
+
|
64 |
+
|
65 |
+
# Print the number of nodes and edges found
|
66 |
+
print(f"Found {len(data['nodes'])} nodes and {len(data['edges'])} edges")
|
67 |
+
|
68 |
+
return data
|
69 |
+
except ET.ParseError as e:
|
70 |
+
print(f"Error parsing XML file: {e}")
|
71 |
+
return None
|
72 |
+
except Exception as e:
|
73 |
+
print(f"An error occurred: {e}")
|
74 |
+
return None
|
75 |
+
|
76 |
|
77 |
def convert_xml_to_json(xml_path, output_path):
|
78 |
"""Converts XML file to JSON and saves the output."""
|
lightrag/utils.py
CHANGED
@@ -13,7 +13,6 @@ from dataclasses import dataclass
|
|
13 |
from functools import wraps
|
14 |
from hashlib import md5
|
15 |
from typing import Any, Protocol, Callable, TYPE_CHECKING, List
|
16 |
-
import xml.etree.ElementTree as ET
|
17 |
import numpy as np
|
18 |
from lightrag.prompt import PROMPTS
|
19 |
from dotenv import load_dotenv
|
|
|
13 |
from functools import wraps
|
14 |
from hashlib import md5
|
15 |
from typing import Any, Protocol, Callable, TYPE_CHECKING, List
|
|
|
16 |
import numpy as np
|
17 |
from lightrag.prompt import PROMPTS
|
18 |
from dotenv import load_dotenv
|