Based on the question I asked last time: Applying PageRank to a topic hierarchy tree(using SPARQL query extracted from DBpedia)
As I currently got the PageRank value against the Regulated concept map. Toward the concept "Machine_learning", my currently code is below:
from SPARQLWrapper import SPARQLWrapper, N3
from rdflib import Graph, URIRef, Literal
import networkx as nx
from networkx.readwrite import json_graph
from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
from rdflib.namespace import Namespace, RDFS, FOAF
import matplotlib.pyplot as plt
#SPARQL query for Regulated SPARQL Query Strategy
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""construct { ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child }
where {
{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
UNION
{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
}
""")
sparql.setReturnFormat(N3)
results = sparql.query().convert()
g = Graph()
g.parse(data=results, format="n3")
#Undirected graphs will be converted to a directed graph with two directed edges for each undirected edge.
dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s,p,o:{})
#Draw regulated concept map
nx.draw(dg)
plt.draw()
#PageRank calculation
p1 = nx.pagerank(dg, alpha=0.85)
#p1 to pr(dict to list)
pr = sorted(p1.items(), key=lambda x:x[1],reverse=True)[:10]
#print sorted ranking
for key,val in pr:
print(key,val)
There are several questions:
- How can I highlight nodes in draw_networkx visualization according to the SPARQL query? For example, I would like to assign the nodes from this query
{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
in green and{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
in red. - Is it possible for me to adjust the nodes size and assign another color for those node according to the PageRank value that calculated above?
#Draw regulated concept map
# nx.draw(dg,pos=nx.spring_layout(dg),node_color='red') # use spring layout
# edges = nx.draw_networkx_edges(dg,pos=nx.spring_layout(dg))
pos = nx.spring_layout(dg)
source_node=copy.copy(pos)
print(source_node)
source_node_list = list(source_node.keys())
# print(source_node_list[0] in nx.spring_layout(dg))
# print(source_node_list)
options = {"node_size": 25, "alpha": 0.85}
graph=nx.draw_networkx_edges(dg, pos=pos, width=1.0, alpha=0.5)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[1]], node_color="r", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[0],], node_color="b", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=source_node_list[2:len(source_node_list)-1], node_color="g", **options)
# nx.draw(graph)
# plt.draw()
# nx.draw_networkx_edges(
# dg,
# pos,
# edgelist=[source_node_list[1]],
# width=8,
# alpha=0.5,
# edge_color="r",
# )
# nx.draw_networkx_edges(
# dg,
# pos,
# edgelist=[source_node_list[0]],
# width=8,
# alpha=0.5,
# edge_color="b"
# )
# nx.draw_networkx(dg, pos=nx.spring_layout(dg), node_color='blue',with_labels = False)
# labels=nx.draw_networkx_labels(dg,pos=nx.spring_layout(dg))
# nodes = nx.draw_networkx_nodes(dg,pos=nx.spring_layout(dg))
# nx.draw(dg)
# plt.draw()
Thank you very much in advance.