-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr.py
52 lines (42 loc) · 1.26 KB
/
pr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
"""
TweetSearch.
Module: pr
Author: Wael Al-Sallami
"""
class PageRank:
"""Calculate PageRanks for Twitter users in the corpus"""
users = {}
def __init__(self, users):
self.users = users
def build(self, alpha=.9):
"""Build PageRanks for users graph"""
prev = dict.fromkeys(self.users, 1.0/len(self.users))
for i in range(100):
new = self.aggregate_ranks(prev)
if i == 0:
new = self.remove_danglers(new)
(new, diff) = self.teleport(new, prev, alpha)
if diff < 0.0000001: break
prev = new
return new
def aggregate_ranks(self, prev):
"""Aggregate ranks for each user"""
new = dict.fromkeys(self.users, 0)
for u in self.users:
L = len(self.users[u]['mentions'])
for m in self.users[u]['mentions']:
new[m] += prev[u]/L
return new
def remove_danglers(self, ranks):
"""Remove dangling nodes from ranks"""
ranks.update((k,v) for (k,v) in ranks.iteritems() if v > 0)
return ranks
def teleport(self, new, prev, alpha):
"""Add teleportation and calculate convergence"""
tele = (1.0 - alpha) / float(len(self.users))
diff = 0
for u in new:
new[u] = alpha * new[u] + tele
diff += abs(prev[u] - new[u])
return (new, diff)