I want to be able to have a lot of nodes have the same label— in my particular case, each node represents a news article, and they should be labelled with their news category. Ultimately, what I really want is a GML file with these labels.
Here's a small sample:
Gtest = nx.Graph()
nodes = [0, 1, 2, 3, 4]
labels = {0:"business", 1:"business",2:"sports", 3:"sports", 4:"politics"}
for node in nodes:
Gtest.add_node(node)
print Gtest.nodes(data=True)
"""
this prints:
[(0, {}), (1, {}), (2, {}), (3, {}), (4, {})]
Which is good, I want 5 nodes.
"""
Gtest = nx.relabel_nodes(Gtest, labels)
print Gtest.nodes(data=True)
"""this prints:
[('business', {}), ('politics', {}), ('sports', {})]
There are only 3 nodes.
"""
nx.write_gml(Gtest, "gml/stackoverflow_test", stringizer = None)
"""
This writes the GML file:
graph [
name "()"
node [
id 0
label "business"
]
node [
id 1
label "politics"
]
node [
id 2
label "sports"
]
]
"""
Ultimately, I'm trying to end up with the GML file:
graph [
name "()"
node [
id 0
label "business"
]
node [
id 1
label "business"
]
node [
id 2
label "sports"
]
node [
id 3
label "sports"
]
node [
id 4
label "politics"
]
]
Is it possible to have the same label for multiple nodes/to generate this output file?