-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank-reduce.py
executable file
·106 lines (73 loc) · 1.9 KB
/
pagerank-reduce.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
import sys
keyword_list = ["IGNORE", "ITERATION"]
#
# This program simply represents the identity function.
#
class PR_Reduce_Struct:
def __init__(self, node):
"""@todo: Docstring for __init__
:returns: @todo
"""
self.sum = 0.0
self.id = node
self.innodes = []
self.previous_rank = 0.0
def set_prev_rank(self, pr):
"""@todo: Docstring for set_prev_rank
:pr: @todo
:returns: @todo
"""
self.previous_rank = pr
def contribute(self, ct):
"""@todo: Docstring for contribute
:ct: @todo
:returns: @todo
"""
self.sum += ct
def add_in(self, node_id):
"""@todo: Docstring for add_in
:node_id: @todo
:returns: @todo
"""
self.innodes.append(node_id)
def __str__(self):
"""@todo: Docstring for __str__
:returns: @todo
"""
return str(self.id) + '\t' + str(self.sum) + '\t' + \
str(self.innodes) + '\t' + str(self.previous_rank) + '\n'
def processMap(line):
"""
process a line printed by processMap into a list as follows:
[node j, contribution/node k to j, node k, previous rank of
node j]
:line: line given by pagerank-map
:returns: list
"""
line = line.strip()
line = line.split('\t')
if (int(line[0]) == -10):
return [int(line[0]), int(line[1])]
return [int(line[0]), float(line[1]), int(line[2]), float(line[3])]
last_node = -1
list_of_structs = []
counter = -1
for line in sys.stdin:
old_line = line
line = processMap(line)
if (line[0] == -10):
sys.stdout.write(old_line)
continue
if line[0] != last_node:
if last_node != -1:
sys.stdout.write(str(list_of_structs[counter]))
list_of_structs.append(PR_Reduce_Struct(line[0]))
counter += 1
last_node = line[0]
list_of_structs[counter].contribute(line[1])
if line[2] != -11 and line[2] != -1:
list_of_structs[counter].add_in(line[2])
if line[2] == -1:
list_of_structs[counter].set_prev_rank(line[3])
sys.stdout.write(str(list_of_structs[counter]))