【问题标题】:Calculating pageRank in a searchEngine在搜索引擎中计算 pageRank
【发布时间】:2014-05-30 17:27:03
【问题描述】:

有人能解释一下为什么这个函数不计算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

【问题讨论】:

    标签: pagerank


    【解决方案1】:

    这看起来像 Python 代码。我会说这是行:

    pr += 0.85 * (linkingpr/linkingcount)`
    

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

    如果这是问题,您可以通过强制其中一个整数为浮点数来解决它,例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 2011-12-13
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2016-12-31
      相关资源
      最近更新 更多