File size: 1,792 Bytes
ee588cd |
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 |
import networkx as nx
import matplotlib.pyplot as plt
import pickle
# ---------- Load GraphML ----------
graph_path = "sample_999.graphml" # Replace with your file path
G = nx.read_graphml(graph_path)
# ---------- Load DCS Pickle ----------
class DCS:
def __init__(self, sent_id, sentence):
self.sent_id = sent_id
self.sentence = sentence
self.dcs_chunks = []
self.lemmas = []
self.cng = []
pkl_path = "DCS_999.p" # Replace with your matching pickle file
with open(pkl_path, "rb") as f:
gold = pickle.load(f, encoding='utf-8')
gold_words = set(gold.dcs_chunks)
# ---------- Prepare Node Labels and Colors ----------
node_labels = {}
node_colors = []
for node_id, data in G.nodes(data=True):
word = data.get("word", "")
node_labels[node_id] = word
if word in gold_words:
node_colors.append("green") # Gold-standard segment
else:
node_colors.append("red") # Candidate, not in gold
# ---------- Prepare Edge Labels ----------
edge_labels = {}
for u, v, data in G.edges(data=True):
etype = data.get("type", "")
edge_labels[(u, v)] = f"type={etype}"
# ---------- Draw the Graph ----------
plt.figure(figsize=(14, 10))
pos = nx.spring_layout(G, seed=42)
nx.draw_networkx_nodes(G, pos, node_color=node_colors, node_size=800, alpha=0.9)
nx.draw_networkx_edges(G, pos, arrows=True)
nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=10)
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color='gray', font_size=8)
plt.title(f"Segmentation Graph for Sentence:\n{gold.sentence}", fontsize=14)
plt.axis('off')
plt.tight_layout()
output_image_path = "graph_output.png"
plt.savefig(output_image_path, format="png", dpi=300)
print(f"Graph saved to {output_image_path}")
|