Created
June 11, 2009 13:57
-
-
Save yaotti/127903 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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