-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank.py
320 lines (259 loc) · 10.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/python3
'''
This file calculates pagerank vectors for small-scale webgraphs.
See the README.md for example usage.
'''
import math
import torch
import gzip
import csv
import gensim.downloader
import logging
WORD_VECTORS = gensim.downloader.load('glove-wiki-gigaword-200')
P_HYPERPARAMETER = 45
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.FloatTensor(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 each index in the personalization vector:
for i, element in enumerate(v):
# get the url for the index
url = self._index_to_url(i)
# check if the url satisfies the input query
# if so, set the corresponding index to one
if url_satisfies_query(url, query):
v[i] = 1
# normalize the vector
v = torch.squeeze(v)
torch.norm(v)
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)
v = torch.unsqueeze(v,1)
v /= torch.norm(v)
if x0 is None:
x0 = torch.Tensor([1/(math.sqrt(n))]*n)
x0 = torch.unsqueeze(x0,1)
x0 /= torch.norm(x0)
# compute $a$ vector
a = torch.zeros(n)
row_sums = torch.sparse.sum(self.P, 1)
for i, row in enumerate(row_sums):
if row.item() == 0:
a[i] = 1
# main loop
xprev = x0
x = xprev.detach().clone()
for i in range(max_iterations):
xprev = x.detach().clone()
beta = (alpha * torch.matmul(torch.t(xprev),a)) + (1-alpha)
x = torch.sparse.addmm(v.reshape(n,1), torch.t(self.P), xprev, beta=beta.item(), alpha=alpha)
# 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)
urls = [self._index_to_url(index.item()) for index in indices]
pageranks = [val.item() for val in vals]
scores = []
similar_words = []
for term in query.split():
if term[0] != '-':
similar_words += get_similar_terms(term, with_score=True)
if query == '':
scores = pageranks
else:
for i, url in enumerate(urls):
score = 0
for word_vector in similar_words:
word = word_vector[0]
word_similarity = word_vector[1]
# find number of times words are in the url
word_count = url.count(word)
score += word_count * (word_similarity**P_HYPERPARAMETER)
ranking = pageranks[i] * score
scores.append(ranking)
# zip the urls and scores together
url_score = list(zip(urls, scores))
url_score.sort(key=lambda a: a[1], reverse=True)
# after you have the scores, print out the max result
matches = 0
for i in range(n):
if matches >= max_results:
break
url = url_score[i][0]
if url_satisfies_query(url,query):
ranking = url_score[i][1]
logging.info(f'rank={matches} ranking={ranking:0.4e} url={url}')
matches += 1
def get_similar_terms(term, n=5, with_score=False):
'''
Returns a list of the n most similar word vectors.
'''
similar_terms_vectors = WORD_VECTORS.most_similar(term)[:n]
similar_terms = []
if not with_score:
for similar_term_vector in similar_terms_vectors:
# select only the word from the vector
similar_terms.append(similar_term_vector[0])
else:
return similar_terms_vectors
return similar_terms
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
similar_terms = get_similar_terms(term)
if term in url:
satisfies = True
for similar_term in similar_terms:
if similar_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)