Although KGGen does not have a built-in visualization feature, graphical presentation of plots can be achieved by the following methods:
1. Installation of visualization dependencies
Installation using pipnetworkxcap (a poem)matplotlib::
pip install networkx matplotlib
2. Creating Python scripts
newly builtvisualize.pyfile, write the following code:
import json
import networkx as nx
import matplotlib.pyplot as plt
# 加载KGGen输出
with open('graph.json', 'r') as f:
data = json.load(f)
# 构建有向图
G = nx.DiGraph()
for rel in data['relations']:
G.add_edge(rel['source'], rel['target'], label=rel['relation'])
# 布局与绘制
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_color='lightblue', font_size=10)
edge_labels = nx.get_edge_attributes(G, 'label')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.show()
3. Running scripts
Executed at the terminal:
python visualize.py
An interactive mapping window can be displayed, where:
- Nodes represent entities and are shown as light blue circles by default
- Edges with arrows indicate relationships, and the direction of the arrows reflect the
source→targetflow - Labels in the margins indicate the specific type of relationship (e.g., "contains", "develops").
For complex maps, adjustablespring_layoutparameters to optimize the node layout, or use libraries such as PyVis to generate web-interactive diagrams.
This answer comes from the articleKG Gen: an open source tool for automatic knowledge graph generation from plain textThe































