-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_knowledge_graph.py
More file actions
137 lines (110 loc) · 4.96 KB
/
generate_knowledge_graph.py
File metadata and controls
137 lines (110 loc) · 4.96 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
import json
import networkx as nx
from pathlib import Path
# === CONFIG ===
INPUT_FILE = "output/merged_threads.json"
OUTPUT_JSON = "output/knowledge_graph.json"
OUTPUT_GRAPHML = "output/knowledge_graph.graphml"
# === STRUCTURES ===
nodes = {}
edges = []
G = nx.DiGraph()
def add_node(node_id, node_type, label):
if node_id not in nodes:
nodes[node_id] = {"id": node_id, "type": node_type, "label": label}
G.add_node(node_id, type=node_type, label=label)
def add_edge(src, dst, rel_type):
edges.append({"from": src, "to": dst, "type": rel_type})
G.add_edge(src, dst, type=rel_type)
def trim(text, limit=80):
return text.strip().replace("\n", " ")[:limit] + "..." if len(text) > limit else text.strip()
# === MAIN ===
def build_knowledge_graph():
with open(INPUT_FILE, "r", encoding="utf-8") as f:
threads = json.load(f)
for thread in threads:
thread_id = f"T_{thread['thread_id']}"
add_node(thread_id, "Thread", thread["title"])
for tag in thread.get("tags", []):
tag_id = f"Tag_{tag}"
add_node(tag_id, "Tag", tag)
add_edge(thread_id, tag_id, "HAS_TAG")
# Firmware version (naive match)
firmware_version = None
for post in thread["posts"]:
if "PX4 version" in post["raw"]:
for line in post["raw"].split("\n"):
if "PX4 version" in line:
firmware_version = line.strip()
break
if firmware_version:
break
if firmware_version:
fw_id = f"FW_{firmware_version}"
add_node(fw_id, "FirmwareVersion", firmware_version)
add_edge(thread_id, fw_id, "RUNS_VERSION")
# Posts and relationships
for post in thread["posts"]:
post_id = f"P_{post['post_id']}"
label = trim(post["raw"])
add_node(post_id, "Post", label)
add_edge(thread_id, post_id, "HAS_POST")
if post.get("is_solution"):
sol_id = f"SOL_{post['post_id']}"
add_node(sol_id, "Solution", f"Solution to: {label[:60]}")
add_edge(post_id, sol_id, "IS_SOLUTION")
for line in post["raw"].split("\n"):
if line.strip().startswith(("make ", "./", "sudo ", "pip", "git")):
cmd = line.strip()
cmd_id = f"CMD_{hash(cmd)}"
add_node(cmd_id, "ActionCommand", cmd)
add_edge(post_id, cmd_id, "INVOKES_ACTION")
if "ModuleNotFoundError" in post["raw"]:
err_id = "ERR_ModuleNotFoundError"
add_node(err_id, "ErrorCode", "ModuleNotFoundError")
add_edge(post_id, err_id, "MENTIONS_ERROR_CODE")
if "No such file or directory" in post["raw"]:
err_id = "ERR_FileNotFoundError"
add_node(err_id, "ErrorCode", "FileNotFoundError")
add_edge(post_id, err_id, "MENTIONS_ERROR_CODE")
if "WSL" in post["raw"] or "Ubuntu" in post["raw"]:
env_id = "ENV_WSL_Ubuntu"
add_node(env_id, "Environment", "WSL + Ubuntu")
add_edge(post_id, env_id, "DESCRIBES_ENVIRONMENT")
for word in ["kconfiglib", "future", "menuconfig", "ELRS", "Cubepilot"]:
if word in post["raw"]:
if word.lower() in ["elrs", "cubepilot"]:
mod_id = f"HW_{word}"
add_node(mod_id, "HardwareModule", word)
add_edge(post_id, mod_id, "MENTIONS_MODULE")
else:
mod_id = f"SW_{word}"
add_node(mod_id, "SoftwareModule", word)
add_edge(post_id, mod_id, "MENTIONS_MODULE")
for token in post["raw"].split():
if token.isupper() and "_" in token:
param_id = f"PARAM_{token}"
add_node(param_id, "Parameter", token)
add_edge(post_id, param_id, "MENTIONS_PARAM")
# Theme from title
theme_label = thread["title"]
theme_id = f"Theme_{hash(theme_label)}"
add_node(theme_id, "Theme", theme_label)
add_edge(thread_id, theme_id, "FOCUSES_ON")
for post in thread["posts"]:
post_id = f"P_{post['post_id']}"
add_edge(theme_id, post_id, "DESCRIBED_BY")
if post.get("is_solution"):
add_edge(theme_id, post_id, "ADDRESSED_BY")
# Output directory
Path("output").mkdir(exist_ok=True)
# Save JSON
with open(OUTPUT_JSON, "w", encoding="utf-8") as f:
json.dump({"nodes": list(nodes.values()), "edges": edges}, f, indent=2)
print(f"✅ Saved knowledge graph to {OUTPUT_JSON}")
# Save GraphML
nx.write_graphml(G, OUTPUT_GRAPHML)
print(f"✅ Saved GraphML for Neo4j/Gephi to {OUTPUT_GRAPHML}")
# Run the builder
if __name__ == "__main__":
build_knowledge_graph()