码迷,mamicode.com
首页 > 编程语言 > 详细

pageRank---python实现

时间:2014-06-13 19:06:38      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

预先为每个URL计算好PageRank值,并将计算结果保存到数据表中,该函数会在每次执行期间重新计算所有的pageRank值

该函数最初将每个网页的PageRank值设为1,然后遍历每个URL,并针对每个外部回指链接,得到其pagerank值与链接的总数,并以粗体显示代码行给出的应用与每个外部链接的计算公式

bubuko.com,布布扣
def calculatepagerank(self,iterations=20):
    #清除当前的PageRank表
    self.con.execute(‘drop table if exists pagerank‘)
    self.con.execute(‘create table pagerank(urlid primary key,score)‘)
    
    #初始化每个url,令其pagerank值为1
    self.con.execute(‘insert into pagerank select rowid, 1.0 from urllist‘)
    self.dbcommit()
    for i in range(iterations):
        print ‘Iteration%d‘%(i)
        for (urlid,) in self.con.execute(‘select rowid from urllist‘):
            pr=0.15
            #循环遍历指向当前网页的所有其他网页
            for (linker,) in self.con.execute(‘select distinct fromid from link            where toid=%d‘%urlid):
                linkingpr=self.con.execute(‘select score from pagerank where urlid=%d‘%linker).fetchone()[0]
                pr+=0.85*(linkingpr/linkingcount)
                self.con.execute(‘update pagerank set score=%f where urlid=%d‘%(pr,urlid))
            self.dbcommit()
            
bubuko.com,布布扣

 

pageRank---python实现,布布扣,bubuko.com

pageRank---python实现

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/csxf/p/3784275.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!