shivrajanand's picture
Upload folder using huggingface_hub
ee588cd verified
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}")