<｜begin▁of▁sentence｜>### Instruction:
Given the Political books Graph that can be read from a GML file named polbooks.gml, can you perform community detection using cdlib's greedy_modularity function, then evaluate the purity of the detected communities assuming each book node has a label denoting its genre (Politics, History, Economics) randomly assigned?
### Response:
from cdlib import algorithms, evaluation
import networkx as nx
import random

# Read the polbooks graph
G = nx.read_gml('polbooks.gml')

# Apply a community detection algorithm (e.g., greedy_modularity)
communities = algorithms.greedy_modularity(G)

# Suppose we have predefined labels for the books (dummy example)
labels = {node: {'genre': random.choice(['Politics', 'History', 'Economics'])} for node in G.nodes()}

# Evaluate the purity of the detected communities
pur = evaluation.purity(communities, labels)

print(pur.score)
<|EOT|>
