0

Can someone explain why this function does not calculate the pagerank but assigns 0.15 to everyone?

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()

The default value is 1 then it should assign 0.15 + 0.85 * (....) but remains fixed 0.15 for everyone

1 Answers1

0

This looks like Python code. I would say it is the line:

pr += 0.85 * (linkingpr/linkingcount)`

In Python, if you divide an integer by an integer the result is also an integer. This is happening because you initialise each page with 1 so linkingpr is an integer, 1. linkingcount will also be an integer because you can't have a fraction of a link.

If that is the problem you can fix it by forcing one of the integers to be a float e.g.:

pr += 0.85 * (float(linkingpr)/linkingcount)
jksnw
  • 648
  • 1
  • 7
  • 19