ziggy.GraphReduce.PageRank_mapper

Package ziggy :: Package GraphReduce :: Module PageRank_mapper |
[hide private]
[frames] | no frames]

# Source Code for Module ziggy.GraphReduce.PageRank_mapper

     1  #! /usr/bin/env python
     2  '''
     3  Created on Aug 18, 2010
     4
     5  @author: dwmclary
     6  '''
     7  import sys
     8
     9 -class PRNode(object):
    10 -    def __init__(self, nID=None, rank_mass=None, adj=None):
    11          if nID != None:
    12              self.nID = nID
    13              self.page_rank = rank_mass
    14              self.adj = adj
    15              #self.path = path
    16
    17 -    def write(self):
    18          s = str(self.nID)+  " "+ " ".join(map(str,self.adj)) +\
    19           " pr: "+str(self.page_rank) #+ " path: " + " ".join(map(str,self.path))
    20          return s
    21
    22 -    def __str__(self):
    23          return self.write()
    24
    25
    26 -def read_adjacency(file, comment="#", sep=None,mass=None):
    27      for line in file:
    28          line = line.rstrip()
    29          if line[0] != comment:
    30              entry = line.split(sep)
    31              nid = entry[0]
    32              if "pr:" in entry:
    33                  d_index = entry.index("pr:")
    34                  rank_mass = float(entry[d_index+1])
    35  #                p_index = entry.index("path:")
    36  #                path = entry[p_index+1:]
    37                  adj = entry[1:d_index]
    38              else:
    39                  rank_mass = mass
    40                  path = []
    41                  adj = entry[1:]
    42
    43              node = PRNode(nid,rank_mass,adj)
    44              yield nid, node
    45
    46
    47 -def main(rank_mass):
    48      data = read_adjacency(sys.stdin, sep=None, mass=rank_mass)
    49      lost_mass = 0.0
    50      for nid, node in data:
    51          p = node.page_rank/len(node.adj)
    52
    53          print node
    54          for m in node.adj:
    55              print str(m) + " pagerank: " + str(p)
    56          if len(node.adj) == 0:
    57              lost_mass += node.page_rank
    58      print "#lost_mass: " + str(lost_mass)
    59
    60  if __name__ == "__main__":
    61      try:
    62          import psyco
    63          psyco.full()
    64      except:
    65          pass
    66      source = open("rank_mass").readlines()
    67      source = source[0].strip()
    68      main(float(source))
    69