pageRank---python实现

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

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

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()
            
原文地址:https://www.cnblogs.com/csxf/p/3784275.html