lolamontes69

Ch3 Ex2-Programming Collective Intelligence

Jun 11th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. import feedparser
  2. import re
  3.  
  4. # Returns title and dictionary of word counts for an RSS feed
  5. def getwordcounts(url):
  6.     # Parse the feed
  7.     d=feedparser.parse(url)
  8.     # Loop over all the entries
  9.     wc1 = {}
  10.     for e in d.entries:
  11.         wc={}
  12.         if 'summary' in e: summary=e.summary
  13.         else: summary=e.description
  14.         if 'updated' in e: updated=e.updated.split('T')[0]
  15.         elif 'published' in e: updated='do summit'
  16.         else: updated = 'I dunno when it was published'
  17.  
  18.         title = str(updated+'/'+d['feed']['title']+' - '+e.title)
  19.         # Extract a list of words
  20.         words=getwords(e.title+' '+summary)
  21.         for word in words:
  22.             wc.setdefault(word,0)
  23.             wc[word]+=1
  24.         wc1[title] = wc
  25.     return wc1
  26.  
  27. def getwords(html):
  28.     # Remove all the HTML tags
  29.     txt=re.compile(r'<[^>]+>').sub('',html)
  30.  
  31.     # Split words by all non-alpha characters
  32.     words=re.compile(r'[^A-Z^a-z]+').split(txt)
  33.  
  34.     # Convert to lowercase
  35.     return [word.lower() for word in words if word!='']
  36.  
  37. apcount={}
  38. wordcounts={}
  39. wordlist=[]
  40. feedlist=[line for line in file('feedlist.txt')]
  41. for feedurl in feedlist:
  42.     try:
  43.         wc1 = getwordcounts(feedurl.strip())
  44.         if len(wc1) <= 0: pass
  45.         else:
  46.             for title in wc1:
  47.                 wordcounts[title] = wc1[title]
  48.                 for word,count in wc1[title].items():
  49.                     apcount.setdefault(word,0)
  50.                     if count>1:
  51.                         apcount[word]+=1
  52.             for w,bc in apcount.items():
  53.                 frac=float(bc)/len(wc1)
  54.                 if frac>0.1 and frac<0.5:
  55.                     wordlist.append(w)
  56.     except:
  57.         print 'Failed to parse feed %s' % feedurl
  58.  
  59. # Create a textfile containing matrix of all wordcounts from all blogs
  60. out=file('blogdata.txt','w')
  61. out.write('Blog')
  62. for word in wordlist: out.write('\t%s' % word)
  63. out.write('\n')
  64. for blog,wc in wordcounts.items():
  65.   print blog
  66.   out.write(blog.encode('utf8'))
  67.   for word in wordlist:
  68.     if word in wc: out.write('\t%d' % wc[word])
  69.     else: out.write('\t0')
  70.   out.write('\n')
  71.  
  72. """
  73.  Using a smaller feedlist, the original generated blogdata was 32M.
  74.  
  75.  ************************
  76.  Printing out the cluster
  77.  ************************
  78.  import clusters as clusters
  79.  blogentries, words, data = clusters.readfile('blogdata5.txt')
  80.  coords = clusters.scaledown(data)
  81.  clusters.draw2d(coords, blogentries, jpeg='blog_entries.jpg')
  82. "
Advertisement
Add Comment
Please, Sign In to add comment