-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparank_reduce.py
More file actions
156 lines (126 loc) · 4.43 KB
/
Copy pathparank_reduce.py
File metadata and controls
156 lines (126 loc) · 4.43 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
# Reducer for PARank
# Imports
import sys
# Read command line arguments
alpha = float(sys.argv[1])
beta = float(sys.argv[2])
gamma = float(sys.argv[3])
dangling_parank = float(sys.argv[4])
num_nodes = float(sys.argv[5])
# Do initialisations
key = "initial_key"
prev_key = "initial_prev_key"
current_data = ""
parank = 0
current_data_dict = dict()
alpha_sum = 0
beta_sum = 0
gamma_sum = 0
total_reduce_scores = 0
total_attachment_weight = 0
total_year_weight = 0
val = 0
# Read lines and do calculations
for line in sys.stdin:
# Remove whitespace
line = line.strip()
# Get key and value
key, val = line.split()
# ---------------------------------------------------------------- #
# If we just get paper data, continue, but keep the data in the dictionary
if(val.startswith("<")):
current_data_dict[key] = val
continue
# ---------------------------------------------------------------- #
# If we are on the same key as previously, simply add the value where needed
if(key == prev_key):
# print "Same key"
coefficient, value = val.split("|")
if(coefficient == "alpha"):
alpha_sum += float(value)
elif(coefficient == "beta"):
beta_sum += float(value)
total_attachment_weight += float(value)
elif(coefficient == "gamma"):
gamma_sum += float(value)
total_year_weight += float(value)
# ---------------------------------------------------------------- #
# Else if key changed and we are not just starting, re-initialise variables
elif(prev_key != "initial_prev_key"):
# Create the line of the pub again
current_data = current_data_dict[prev_key]
current_data = current_data.replace("<", "")
current_data = current_data.replace(">", "")
current_data = current_data.split("|")
# Get data parts
outlinks = current_data[0]
outlink_num = current_data[1]
old_parank = current_data[2]
publication_year = current_data[3]
attachment_score = current_data[4]
year_weight_score = current_data[5]
# Add to each coefficient what is left
alpha_sum += dangling_parank
# Calculate as sum of the three vector scores
parank = alpha * alpha_sum + beta * float(beta_sum) + gamma * float(gamma_sum)
# Output Data
print prev_key + "\t" + outlinks + "|" + outlink_num + "|" + str(parank) + "\t" + old_parank + "\t" + publication_year + "\t" + attachment_score + "\t" + str(year_weight_score)
##################################
##################################
# POP KEY BEFORE CHANGING IT
current_data_dict.pop(prev_key, None)
# Re-initialisations
prev_key = key
alpha_sum = 0
beta_sum = 0
gamma_sum = 0
coefficient, value = val.split("|")
# Start calculations for new key
if(coefficient == "alpha"):
alpha_sum += float(value)
elif(coefficient == "beta"):
beta_sum += float(value)
total_attachment_weight += float(value)
elif(coefficient == "gamma"):
gamma_sum += float(value)
total_year_weight += float(value)
# ---------------------------------------------------------------- #
# Previous key == initial_prev_key
else:
#print >>sys.stderr, "At part 4"
prev_key = key
alpha_sum = 0
beta_sum = 0
gamma_sum = 0
coefficient, value = val.split("|")
if(coefficient == "alpha"):
alpha_sum += float(value)
elif(coefficient == "beta"):
beta_sum += float(value)
total_attachment_weight += float(value)
elif(coefficient == "gamma"):
gamma_sum += float(value)
total_year_weight += float(value)
# -------------------------------------------------------------------- #
# If we're done looping, check if we have an entry that hasn't been output
if(key == prev_key):
# Format latest data
current_data = current_data_dict[key]
current_data = current_data.replace("<", "")
current_data = current_data.replace(">", "")
current_data = current_data.split("|")
current_data_dict.pop(key, None)
# Get data parts
outlinks = current_data[0]
outlink_num = current_data[1]
old_parank = current_data[2]
publication_year = current_data[3]
density_score = current_data[-1]
year_weight_score = current_data[5]
# Add to each coefficient what is left
alpha_sum += dangling_parank
# Calculate as sum of the three vector scores
parank = alpha * alpha_sum + beta * float(beta_sum) + gamma * float(gamma_sum)
# Output Data
print prev_key + "\t" + outlinks + "|" + outlink_num + "|" + str(parank) + "\t" + old_parank + "\t" + publication_year + "\t" + attachment_score + "\t" + str(year_weight_score)