在搜索引擎中计算pageRank

时间:2014-05-30 17:27:04

标签: pagerank

有人可以解释为什么这个函数不会计算Pagerank但是为每个人分配0.15吗?

def calculatepagerank(self, iterations=20):
  # clear out the current PageRank tables
  self.con.execute('drop table if exists pagerank')
  self.con.execute('create table pagerank(urlid primary key, score)')

  # initialize every url with a PageRank of 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

        # Loop through all the pages that link to this one
        for (linker,) in self.con.execute('select distinct fromid from link where toid=%d' % urlid):
           # Get the PageRank of the linker
           linkingpr = self.con.execute('select score from pagerank where urlid = %d' % linker).fetchone()[0]
           # Get the total number of links from the linker
           linkingcount = self.con.execute('select count(*) from link where fromid = %d' % linker).fetchone()[0]
           pr += 0.85 * (linkingpr/linkingcount)
        self.con.execute('update pagerank set score = %f where urlid = %d' % (pr, urlid))
     self.dbcommit()

默认值为1则应分配0.15 + 0.85 *(....),但每个人保持固定0.15

1 个答案:

答案 0 :(得分:0)

这看起来像Python代码。我会说它就是这条线:

pr += 0.85 * (linkingpr/linkingcount)`

在Python中,如果将整数除以整数,则结果也是整数。发生这种情况是因为您使用1初始化每个页面,因此linkingpr是一个整数,1。linkingcount也将是一个整数,因为您不能拥有一小部分链接。

如果这是问题,你可以通过强制其中一个整数成为一个浮点数来修复它,例如:

pr += 0.85 * (float(linkingpr)/linkingcount)