Skip to content

Instantly share code, notes, and snippets.

@wy36101299
Created November 18, 2014 08:47
Show Gist options
  • Save wy36101299/9876403feb74e545efb6 to your computer and use it in GitHub Desktop.
Save wy36101299/9876403feb74e545efb6 to your computer and use it in GitHub Desktop.
HITS(authority&hub)
import copy
with open('/Users/wy/Desktop/hw2dataset/graph_3.txt','r') as f:
tl=[]
f = f.readlines()
for s in f:
s=s.rstrip()
s=s.split(',')
tl.append(s[0])
tl.append(s[1])
tl=set(tl)
setsum = len(tl)
class ini(object):
def __init__(self):
self.d ={}
self.d['au']=1
self.d['in']=[]
self.d['out']=[]
self.d['hub']=1
dd={}
for a in tl:
dd[a]= ini()
for s in f:
s=s.rstrip()
s=s.split(',')
dd[s[0]].d['out'].append(s[1])
dd[s[1]].d['in'].append(s[0])
def hits():
aul=[]
hubl=[]
for p,v in dd.items():
score=0
prein = copy.deepcopy(v.d['in'])
if len(prein)!=0:
for r in range( len(prein) ):
vv = dd[prein.pop()]
score += float(vv.d['hub'])
v.d['au']= score
aul.append(v.d['au'])
score=0
aftout = copy.deepcopy(v.d['out'])
if len(aftout)!=0:
for r in range( len(aftout) ):
vv = dd[aftout.pop()]
score += float(vv.d['au'])
v.d['hub']= score
hubl.append(v.d['hub'])
maxau=float(max(aul))
maxhub=float(max(hubl))
for p,v in dd.items():
v.d['au']= v.d['au']/maxau
v.d['hub']= v.d['hub']/maxhub
return(aul)
ri=[]
ro = hits()
while ri != ro:
ri=ro
ro = hits()
y1=[]
y2=[]
y3=[]
for p,v in dd.items():
print(p)
y1.append(p)
y2.append(v.d['au'])
y2.append(v.d['hub'])
print(v.d['au'])
print(v.d['hub'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment