Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created June 11, 2009 13:57
Show Gist options
  • Save yaotti/127903 to your computer and use it in GitHub Desktop.
Save yaotti/127903 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#coding: utf8
class Page:
"""
インスタンス変数
rank: PageRank
tolinks: そのページが指すページオブジェクトのリスト
クラス変数
tax: 修正用の重み
"""
tax = 0.85
def __init__(self, rank=0.0, tolinks=[]):
self.rank = rank
self.tolinks = tolinks
# そのページが指しているページを登録する
def addtolinks(self, tolinks):
self.tolinks = self.tolinks + tolinks
# 与えられたページ集合の各ページランクを計算
def calcrank(*pages):
tax = Page.tax
for topage in pages:
tmprank = 0.0
for frompage in pages:
if topage in frompage.tolinks:
tmprank += frompage.rank / len(frompage.tolinks)
topage.rank = 1 - tax + tax * tmprank
def displaypageranks(*pages):
for page in pages:
print page.rank
if __name__=="__main__":
import sys
try:
count = int(sys.argv[1])
except:
count = 10
a = Page()
b = Page()
c = Page()
d = Page()
a.addtolinks([b,c])
b.addtolinks([c])
c.addtolinks([a])
d.addtolinks([c])
for i in range(count):
calcrank(a,b,c,d)
displaypageranks(a,b,c,d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment