lolamontes69

Python/Ten most reviewed books in the Book Crossing Dataset

May 16th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. def unescape(s):
  2.     s = s.replace("&lt;", "<")
  3.     s = s.replace("&gt;", ">")
  4.     # this has to be last:
  5.     s = s.replace("&amp;", "&")
  6.     return s
  7.  
  8. # {bookId: {userId: rating,...
  9. dic = {}
  10. fin = open('/root/Downloads/bx/BX-Dump/BX-Book-Ratings.csv')
  11. for line in fin:
  12.     book1 = unescape(line.strip())
  13.     book = book1.split('";"')
  14.     if book[1].strip('"') not in dic:
  15.         dic[book[1].strip('"')] = {}
  16.         dic[book[1].strip('"')][book[0].strip('"')] = book[2].strip('"')
  17.     else:
  18.         dic[book[1].strip('"')][book[0].strip('"')] = book[2].strip('"')
  19.  
  20.  
  21. #  {Number of reviews: bookId
  22. dic1 = {}
  23. for a in dic:
  24.     b = len(dic[str(a)])
  25.     try:
  26.         if b not in dic1:
  27.            dic1[b] = [a]
  28.         else: dic1[b].append(a)
  29.     except:
  30.         pass
  31.  
  32. # [number of reviews,...
  33. popularbooks = dic1.keys()
  34. popularbooks.sort()
  35. popularbooks.reverse()
  36.  
  37. # {bookId: title
  38. dic2 = {}
  39. fin = open('/root/Downloads/bx/BX-Dump/BX-Books.csv')
  40. for line in fin:
  41.     book1 = unescape(line.strip())
  42.     book = book1.split('";"')
  43.     dic2[book[0].strip('"')] = book[1].strip('"')
  44.  
  45. # {userId: age,...
  46. useragedic = {}
  47. fin = open('/root/Downloads/bx/BX-Dump/BX-Users.csv')
  48. for line in fin:
  49.     frogs = line.replace('"','').strip().split(";")
  50.     if frogs[0] not in useragedic:
  51.         try:
  52.             useragedic[frogs[0]]  = int(frogs[2])
  53.         except:
  54.             useragedic[frogs[0]]  = int(0)
  55.  
  56. count = 0
  57. total = 0
  58. print "\n---------------------------------------------\n"
  59. for a in popularbooks:
  60.     for b in dic1[a]:
  61.         if count == 70:
  62.             continue
  63.         else:
  64.             try:
  65.                 print a,"Reviews\nBookTitle:", dic2[str(b)],"\nBookId:", str(b)
  66.                 count += 1
  67.                 count1 = 0
  68.                 total = 0
  69.                 for c in dic[str(b)]:
  70.                     # skip if user age is unspecified
  71.                     if useragedic[c] == 0:
  72.                         pass
  73.                     else:
  74.                         total += int(useragedic[c])
  75.                         count1 += 1
  76.                 avageofrev = float(total/count1)
  77.                 print "Average age of reviewer =",avageofrev,"\n\n"
  78.             except:
  79.                 print "error with value",str(b),"\n"
  80.  
  81.  
  82. """ find the ten most popular books to review
  83.  
  84. reviews      title
  85. 2502      Wild Animus
  86. 1295      The Lovely Bones: A Novel
  87. 883       The Da Vinci Code
  88. 732       Divine Secrets of the Ya-Ya Sisterhood: A Novel
  89. 723       The Red Tent (Bestselling Backlist)
  90. 647       A Painted House
  91. 639 error with value 0679781587
  92. 615       The Secret Life of Bees
  93. 614       Snow Falling on Cedars
  94. 586       Angels & Demons
  95. 585       Where the Heart Is (Oprah's Book Club (Paperback))
  96.  
  97. concerning '639 error with value 0679781587'
  98. >>>for a in dic2:
  99. >>>   if '79587' in a: print a
  100. 0674795873
  101. 0915795876
  102. 0840795874
  103. 0671795872
  104.  
  105. conclusion :
  106.    0679781587 is a value in    BX-Book-Ratings.csv
  107.       that doesnt appear in    BX-Books.csv
  108.  
  109. """
Advertisement
Add Comment
Please, Sign In to add comment