-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank.py
273 lines (224 loc) · 9.15 KB
/
pagerank.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python3
'''
This file calculates pagerank vectors for small-scale webgraphs.
See the README.md for example usage.
'''
import math
import torch
from torch import sparse
import gzip
import csv
import logging
from torch import Tensor
class WebGraph():
def __init__(self, filename, max_nnz=None, filter_ratio=None):
'''
Initializes the WebGraph from a file.
The file should be a gzipped csv file.
Each line contains two entries: the source and target corresponding to a single web link.
This code assumes that the file is sorted on the source column.
'''
self.url_dict = {}
indices = []
from collections import defaultdict
target_counts = defaultdict(lambda: 0)
# loop through filename to extract the indices
logging.debug('computing indices')
with gzip.open(filename,newline='',mode='rt') as f:
for i,row in enumerate(csv.DictReader(f)):
if max_nnz is not None and i>max_nnz:
break
import re
regex = re.compile(r'.*((/$)|(/.*/)).*')
if regex.match(row['source']) or regex.match(row['target']):
continue
source = self._url_to_index(row['source'])
target = self._url_to_index(row['target'])
target_counts[target] += 1
indices.append([source,target])
# remove urls with too many in-links
if filter_ratio is not None:
new_indices = []
for source,target in indices:
if target_counts[target] < filter_ratio*len(self.url_dict):
new_indices.append([source,target])
indices = new_indices
# compute the values that correspond to the indices variable
logging.debug('computing values')
values = []
last_source = indices[0][0]
last_i = 0
for i,(source,target) in enumerate(indices+[(None,None)]):
if source==last_source:
pass
else:
total_links = i-last_i
values.extend([1/total_links]*total_links)
last_source = source
last_i = i
# generate the sparse matrix
i = torch.LongTensor(indices).t()
v = torch.FloatTensor(values)
n = len(self.url_dict)
self.P = torch.sparse_coo_tensor(i, v, torch.Size([n,n]))
self.index_dict = {v: k for k, v in self.url_dict.items()}
def _url_to_index(self, url):
'''
given a url, returns the row/col index into the self.P matrix
'''
if url not in self.url_dict:
self.url_dict[url] = len(self.url_dict)
return self.url_dict[url]
def _index_to_url(self, index):
'''
given a row/col index into the self.P matrix, returns the corresponding url
'''
return self.index_dict[index]
def make_personalization_vector(self, query=None):
'''
If query is None, returns the vector of 1s.
If query contains a string,
then each url satisfying the query has the vector entry set to 1;
all other entries are set to 0.
'''
n = self.P.shape[0]
if query is None:
v = torch.ones(n)
else:
v = torch.zeros(n)
for i in range(n):
url = self._index_to_url(i)
if url_satisfies_query(url, query):
v[i] = 1
if torch.norm(v) != 0:
v /= torch.norm(v)
return v
# FIXME: implement Task 2
v_sum = torch.sum(v)
assert(v_sum>0)
v /= v_sum
return v
def power_method(self, v=None, x0=None, alpha=0.85, max_iterations=1000, epsilon=1e-6):
'''
This function implements the power method for computing the PageRank.
The self.P variable stores the P matrix.
You will have to compute the a vector and implement Equation 5.1 from "Deeper Inside Pagerank."
'''
with (torch.no_grad()):
n = self.P.shape[0]
# create variables if none given
if v is None:
v = torch.Tensor([1 / n] * n).unsqueeze(1)
v = v.view(n,1)
# Shape [n, 1]
v /= torch.norm(v)
if x0 is None:
x0 = torch.Tensor([1 / (math.sqrt(n))] * n).unsqueeze(1) # Shape [n, 1]
x0 = x0.view(n, 1)
x0 /= torch.norm(x0)
# Row sums of the matrix P to identify dangling nodes
row_sums = sparse.sum(self.P, dim=1)
dangling_nodes = row_sums.to_dense() == 0
a = torch.zeros(n, 1)
a[dangling_nodes] = 1
# main loop
xprev = x0
x = xprev.detach().clone()
for i in range(max_iterations):
xprev = x.detach().clone()
first_term = alpha * torch.t(torch.matmul(torch.t(self.P), xprev.squeeze()))
r = alpha * torch.matmul(torch.t(xprev).squeeze(), a)
second_term = r + (1 - alpha)
second_term_full = second_term * v
x = torch.add(first_term, second_term_full)
x /= torch.norm(x)
# compute the new x vector using Eq (5.1)
# FIXME: Task 1
# HINT: this can be done with a single call to the `torch.sparse.addmm` function,
# but you'll have to read the code above to figure out what variables should get passed to that function
# and what pre/post processing needs to be done to them
# output debug information
residual = torch.norm(x-xprev)
logging.debug(f'i={i} residual={residual}')
# early stop when sufficient accuracy reached
if residual < epsilon:
break
#x = x0.squeeze()
return x.squeeze()
def search(self, pi, query='', max_results=10):
'''
Logs all urls that match the query.
Results are displayed in sorted order according to the pagerank vector pi.
'''
n = self.P.shape[0]
vals,indices = torch.topk(pi,n)
matches = 0
for i in range(n):
if matches >= max_results:
break
index = indices[i].item()
url = self._index_to_url(index)
pagerank = vals[i].item()
if url_satisfies_query(url,query):
logging.info(f'rank={matches} pagerank={pagerank:0.4e} url={url}')
matches += 1
def url_satisfies_query(url, query):
'''
This functions supports a moderately sophisticated syntax for searching urls for a query string.
The function returns True if any word in the query string is present in the url.
But, if a word is preceded by the negation sign `-`,
then the function returns False if that word is present in the url,
even if it would otherwise return True.
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', 'covid')
True
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', 'coronavirus covid')
True
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', 'coronavirus')
False
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', 'covid -speech')
False
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', 'covid -corona')
True
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', '-speech')
False
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', '-corona')
True
>>> url_satisfies_query('www.lawfareblog.com/covid-19-speech', '')
True
'''
satisfies = False
terms = query.split()
num_terms=0
for term in terms:
if term[0] != '-':
num_terms+=1
if term in url:
satisfies = True
if num_terms==0:
satisfies=True
for term in terms:
if term[0] == '-':
if term[1:] in url:
return False
return satisfies
if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--data', required=True)
parser.add_argument('--personalization_vector_query')
parser.add_argument('--search_query', default='')
parser.add_argument('--filter_ratio', type=float, default=None)
parser.add_argument('--alpha', type=float, default=0.85)
parser.add_argument('--max_iterations', type=int, default=1000)
parser.add_argument('--epsilon', type=float, default=1e-6)
parser.add_argument('--max_results', type=int, default=10)
parser.add_argument('--verbose', action='store_true')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
g = WebGraph(args.data, filter_ratio=args.filter_ratio)
v = g.make_personalization_vector(args.personalization_vector_query)
pi = g.power_method(v, alpha=args.alpha, max_iterations=args.max_iterations, epsilon=args.epsilon)
g.search(pi, query=args.search_query, max_results=args.max_results)