class Compute(object): """Compute PageRank per node on the object/user/tag/etc graph.""" def __init__(self): self.graphs = Graphs() def pagerank(self, edge_weights={}, context=None, context_weight=10): G = self.graphs.unify(edge_weights) if not context: return nx.pagerank(G) else: weights = {} for k in G.nodes(): weights[k] = 1 weights[context] = context_weight return nx.pagerank(G, personalization=weights) def personalized_pageranks(self, edge_weights={}, context_weight=10): G = self.graphs.unify(edge_weights) result = {} for k in G.nodes(): result[k] = self.pagerank(edge_weights, k, context_weight) return result
class TestGraph(unittest.TestCase): layer = PLONEINTRANET_PAGERANK_INTEGRATION def setUp(self): self.app = self.layer['app'] self.portal = self.layer['portal'] self.graphs = Graphs() def test_social_following(self): """Get the social graph from plonesocial.network """ self.assertEqual(set(self.graphs.social_following().edges()), config.SOCIAL_GRAPH) def test_content_tree(self): """Get the object containment tree """ self.assertEqual( set(self.graphs.content_tree().edges()), set([('path:/plone/public', 'path:/plone/public/d1'), ('path:/plone/public/d1', 'path:/plone/public')])) def test_content_authors(self): """Get the object authorships """ self.assertEqual( set(self.graphs.content_authors().edges()), set([('path:/plone/public', 'user:admin'), ('path:/plone/public/d1', 'user:admin'), ('user:admin', 'path:/plone/public'), ('user:admin', 'path:/plone/public/d1')])) def test_content_tags(self): """Get the object authorships """ self.assertEqual(set(self.graphs.content_tags().edges()), config.CONTENT_TAGS) def test_unify_following(self): G = self.graphs.unify(config.EDGE_WEIGHTS) edges = G.edges(data=True) expect = ('neil_wichmann', 'christian_stoney', {'weight': 2}) self.assertTrue(expect in edges) def test_unify_tree(self): G = self.graphs.unify(config.EDGE_WEIGHTS) edges = G.edges(data=True) expect = ('path:/plone/public', 'path:/plone/public/d1', {'weight': 1}) self.assertTrue(expect in edges) def test_unify_authors(self): G = self.graphs.unify(config.EDGE_WEIGHTS) edges = G.edges(data=True) expect = ('path:/plone/public/d1', 'user:admin', {'weight': 3}) self.assertTrue(expect in edges) def test_unify_tags(self): G = self.graphs.unify(config.EDGE_WEIGHTS) edges = G.edges(data=True) expect = ('path:/plone/public/d1', 'tag:nix', {'weight': 4}) self.assertTrue(expect in edges)