-
Notifications
You must be signed in to change notification settings - Fork 0
/
hits.py
184 lines (152 loc) · 6.05 KB
/
hits.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
#!/usr/bin/env python
__author__ = "Yuetian Li"
import copy
import json
from elasticsearch.helpers import bulk
from tqdm import tqdm
import os
from elasticsearch import Elasticsearch
import math
from collections import defaultdict
import random
def get_inlinks(path):
inlinks = defaultdict(list)
with open(path, 'r') as f:
inlinks = json.load(f)
return inlinks
def get_outlinks(path):
outlinks = defaultdict(list)
with open(path, 'r') as f:
outlinks = json.load(f)
return outlinks
def get_all_pages(path):
all_pages = []
with open(path, 'r') as f:
all_pages = json.load(f)
return all_pages
def authority_hub_init(all_pages):
authority = defaultdict(float)
hub = defaultdict(float)
for page in all_pages:
authority[page] = 1
hub[page] = 1
return authority, hub
def hits_iter(root_set, base_set, authority, hub, inlinks, outlinks):
new_authority = defaultdict(float)
new_hub = defaultdict(float)
# Calculate the scores
for page in root_set:
if page in inlinks:
new_authority[page] = 0
for inlink in inlinks[page]:
if inlink in authority:
new_authority[page] += hub[inlink]
else:
new_authority[page] = 0
for page in base_set:
if page in outlinks:
new_hub[page] = 0
for outlink in outlinks[page]:
if outlink in hub:
new_hub[page] += authority[outlink]
else:
new_hub[page] = 0
# Normalization
auth_norm = math.sqrt( sum([ ( new_authority[page] ) ** 2 for page in new_authority ]) )
hub_norm = math.sqrt( sum([ ( new_hub[page] ) ** 2 for page in new_hub ]) )
for page in new_authority:
new_authority[page] = new_authority[page] / auth_norm
for page in new_hub:
new_hub[page] = new_hub[page] / hub_norm
return new_authority, new_hub
def update_auth_hub(authority, hub, new_authority, new_hub):
for page in new_authority:
if page in authority:
authority[page] = new_authority[page]
for page in new_hub:
if page in hub:
hub[page] = new_hub[page]
return authority, hub
def check_convergence(authority, hub, new_authority, new_hub, epsilon_auth, epsilon_hub):
authority_total = 0
new_authority_total = 0
for count, page in enumerate(new_authority):
authority_total += authority[page]
new_authority_total += new_authority[page]
hub_total = 0
new_hub_total = 0
for count, page in enumerate(new_hub):
hub_total += hub[page]
new_hub_total += new_hub[page]
print( 'Authority gap:', math.fabs(authority_total - new_authority_total ) )
print( 'Hub gap:', math.fabs(hub_total - new_hub_total ) )
if ( math.fabs(authority_total - new_authority_total ) < epsilon_auth ) and \
( math.fabs(hub_total - new_hub_total ) < epsilon_hub ):
return True
return False
def run_hits(root_set, base_set, all_pages, inlinks, outlinks):
epsilon_auth = 10 ** -9
epsilon_hub = 10 ** -9
authority, hub = authority_hub_init(all_pages)
new_authority, new_hub = hits_iter(root_set, base_set, authority, hub, inlinks, outlinks)
iter_count = 0
complete_converge_count = 0
while True:
if iter_count > 100000:
print('Exceeded iteration limit of', str(iter_count))
break
if check_convergence(authority, hub, new_authority, new_hub, epsilon_auth, epsilon_hub):
complete_converge_count += 1
if complete_converge_count == 4:
print('HITS score converged.')
break
else:
complete_converge_count = 0
authority, hub = update_auth_hub(authority, hub, new_authority, new_hub)
new_authority, new_hub = hits_iter(root_set, base_set, authority, hub, inlinks, outlinks)
iter_count += 1
return new_authority, new_hub
if __name__ == "__main__":
D = 200
print('Loading link graph...')
inlinks = get_inlinks('./info/inlinks.json')
print('inlinks loaded')
outlinks = get_outlinks('./info/outlinks.json')
print('outlinks loaded')
all_pages = get_all_pages('./info/docno_list.json')
print('url loaded')
with open('./info/root_set.json', 'r') as f:
root_set = json.load(f)
print('A: Get root set finish')
with open('./info/base_set.json', 'r') as f:
base_set = json.load(f)
print('B: Get base set finish')
print('C: Start to run HITS')
authority, hub = run_hits(root_set, base_set, all_pages, inlinks, outlinks)
# Dump result
sorted_authority = sorted(authority.items(), key=lambda x: x[1], reverse=True)
sorted_hub = sorted(hub.items(), key=lambda x: x[1], reverse=True)
sorted_authority_all = []
sorted_hub_all = []
for page in sorted_authority:
if ( page[0] in outlinks ) and ( page[0] in inlinks ):
sorted_authority_all.append( [ page[0], page[1], len(outlinks[page[0]]), len(inlinks[page[0]]) ] )
elif ( page[0] in outlinks ):
sorted_authority_all.append( [ page[0], page[1], len(outlinks[page[0]]), 0 ] )
elif ( page[0] in inlinks ):
sorted_authority_all.append( [ page[0], page[1], 0, len(inlinks[page[0]]) ] )
else:
sorted_authority_all.append( [ page[0], page[1], 0, 0 ] )
for page in sorted_hub:
if ( page[0] in outlinks ) and ( page[0] in inlinks ):
sorted_hub_all.append( [ page[0], page[1], len(outlinks[page[0]]), len(inlinks[page[0]]) ] )
elif ( page[0] in outlinks ):
sorted_hub_all.append( [ page[0], page[1], len(outlinks[page[0]]), 0 ] )
elif (page[0] in inlinks):
sorted_hub_all.append( [ page[0], page[1], 0, len(inlinks[page[0]]) ] )
else:
sorted_hub_all.append( [ page[0], page[1], 0, 0 ] )
with open('./result/authority.json', 'w') as f:
json.dump(sorted_authority_all[:1000], f)
with open('./result/hub.json', 'w') as f:
json.dump(sorted_hub_all[:1000], f)