ziggy.GraphReduce.PageRank_reducer

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

# Source Code for Module ziggy.GraphReduce.PageRank_reducer

     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 -def read_map_output(file, sep=None):
    26      for line in file:
    27          line = line.rstrip().split()
    28          nid = line[0]
    29
    30          if "#lost_mass:" in line:
    31              print " ".join(line)
    32          elif not "pagerank:" in line:
    33              type = "node"
    34              pr_index = line.index("pr:")+1
    35              rank = float(line[pr_index])
    36
    37              adj = line[1:pr_index-1]
    38              node = PRNode(nid, rank, adj)
    39              yield type, nid, node
    40          else:
    41              type = "pagerank"
    42              pr_index = line.index("pagerank:")
    43              rank = line[pr_index+1]
    44              yield type, nid, rank
    45
    46 -def main():
    47      M = {}
    48      pr = {}
    49      data = read_map_output(sys.stdin)
    50      for type, key, value in data:
    51          if type == "node":
    52              M[key] = value
    53              if key not in pr:
    54                  pr[key] = 0.0
    55          elif type == "pagerank":
    56              if key not in pr:
    57                  pr[key] = 0.0
    58              pr[key] += float(value)
    59      for key in M.keys():
    60          if key in pr:
    61              M[key].page_rank = pr[key]
    62          print M[key]
    63
    64  if __name__ == "__main__":
    65      try:
    66          import psyco
    67          psyco.full()
    68      except ImportError:
    69          pass
    70      main()
    71