def nbest_centrality(G, metric, n=10, attr="centrality", **kwargs):
# Compute the centrality scores for each vertex
scores = metric(G, **kwargs)
# Set the score as a property on each node
nx.set_node_attributes(G, attr, scores)
# Filter scores (do not include in book)
ntypes = nx.get_node_attributes(G, 'type')
phrases = [
item for item in scores.items()
if ntypes.get(item[0], None) == "keyphrase"
]
# Find the top n scores and print them along with their index
topn = heapq.nlargest(n, phrases, key=itemgetter(1))
for idx, item in enumerate(topn):
print("{}. {}: {:0.4f}".format(idx+1, *item))
return G
评论列表
文章目录