-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank.py
233 lines (193 loc) · 7.58 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
#!/usr/bin/python3
'''
This file calculates pagerank vectors for small-scale webgraphs.
'''
import math
import torch
import gzip
import csv
import gensim.downloader
import logging
class WebGraph():
def __init__(self, filename, max_nnz=None, filter_ratio=None):
self.url_dict = {}
indices = []
self.vectors = gensim.downloader.load('glove-twitter-25')
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
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):
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):
return self.index_dict[index]
def make_personalization_vector(self, query=None):
n = self.P.shape[0]
if query is None:
v = torch.ones(n)
else:
v = torch.zeros(n)
for url,i in self.url_dict.items():
if url_satisfies_query(url, query,self.vectors):
v[i] = 1
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):
with torch.no_grad():
n = self.P.shape[0]
#
nondangling_nodes = torch.sparse.sum(self.P,1).indices()
a = torch.ones([n,1])
a[nondangling_nodes] = 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)
# main loop
xprev = x0
x = xprev.detach().clone()
for i in range(max_iterations):
xprev = x.detach().clone()
q = (alpha*x.t()@a + (1-alpha)) * v.t()
x = torch.sparse.addmm(
q.t(),
self.P.t(),
x,
beta=1,
alpha=alpha
)
x /= torch.norm(x)
accuracy = torch.norm(x-xprev)
logging.debug('i='+str(i)+' accuracy='+str(accuracy))
if accuracy < epsilon:
break
return x.squeeze()
def search(self, pi, query='', max_results=10):
n = self.P.shape[0]
k = min(max_results,n)
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,self.vectors):
logging.info('rank='+str(matches)+' pagerank='+str(pagerank)+' url='+url)
matches += 1
def url_satisfies_query(url, query, vectors):
'''
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()
similar_terms = []
if len(terms) !=0 and terms[0][0] != '-':
for term in terms:
similar = vectors.most_similar(term)[1:5]
similar = [i[0] for i in similar]
similar_terms.extend(similar)
similar_terms.extend(terms)
# print(similar_terms)
num_terms=0
for term in similar_terms:
if term[0] != '-':
num_terms+=1
if term in url:
satisfies = True
if num_terms==0:
satisfies=True
for term in similar_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', default='')
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)