lolamontes69

newsfeatures.py for Ch10 Programming Collective Intelligence

Sep 4th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.77 KB | None | 0 0
  1. """ I updated the feeds and added a stoplist - lolamontes69 """
  2. import feedparser as feedparser
  3. import re as re
  4. from numpy import *
  5.  
  6. feedlist = ['http://feeds.reuters.com/rss/topNews',
  7.             'http://feeds.reuters.com/rss/domesticNews',
  8.             'http://feeds.reuters.com/rss/worldNews',
  9.             'http://hosted2.ap.org/atom/APDEFAULT/3d281c11a96b4ad082fe88aa0db04305',
  10.             'http://hosted2.ap.org/atom/APDEFAULT/386c25518f464186bf7a2ac026580ce7',
  11.             'http://hosted2.ap.org/atom/APDEFAULT/cae69a7523db45408eeb2b3a98c0c9c5',
  12.             'http://hosted2.ap.org/atom/APDEFAULT/89ae8247abe8493fae24405546e9a1aa',
  13.             'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml',
  14.             'http://www.nytimes.com/services/xml/rss/nyt/International.xml',
  15.             'http://news.google.com/?output=rss',
  16.             'http://feeds.salon.com/salon/news',
  17.             'http://www.foxnews.com/xmlfeed/rss/0,4313,0,00.rss',
  18.             'http://www.foxnews.com/xmlfeed/rss/0,4313,80,00.rss',
  19.             'http://rss.cnn.com/rss/edition.rss',
  20.             'http://rss.cnn.com/rss/edition_world.rss',
  21.             "http://www.npr.org/rss/rss.php?id=1003",
  22.             "http://www.npr.org/rss/rss.php?id=1004",
  23.             "http://rss.cbc.ca/lineup/world.xml",
  24.             "http://feeds.feedburner.com/newscomauworldnewsndm",
  25.             "http://www.abc.net.au/news/feed/52278/rss.xml",
  26.             "http://news.sky.com/feeds/rss/world.xml",
  27.             "http://feeds.feedburner.com/ndtv/TqgX",
  28.             "http://timesofindia.feedsportal.com/c/33039/f/533917/index.rss",
  29.             "http://feeds.abcnews.com/abcnews/internationalheadlines",
  30.             "http://www.usnews.com/rss/news",
  31.             "http://www.un.org/apps/news/rss/rss_top.asp",
  32.             "http://feeds.washingtonpost.com/rss/world",
  33.             'http://rss.cnn.com/rss/edition_us.rss']
  34.  
  35. def stripHTML(h):
  36.     p=''
  37.     s=0
  38.     for c in h:
  39.         if c=='<': s=1
  40.         elif c=='>':
  41.             s=0
  42.             p+=' '
  43.         elif s==0: p+=c
  44.     return p
  45.  
  46. def separatewords(text):
  47.     ignore_list = ['a', 'about', 'above', 'after', 'again', 'all', 'also', 'am', 'an', 'and', 'any', 'are', "aren't", 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', "can't", 'cannot', 'could', "couldn't", 'did', "didn't", 'do', 'does', "doesn't", 'doing', "don't", 'down', 'during', 'each', "even", 'every', 'few', 'for', 'from', 'further', 'had', "hadn't", 'has', "hasn't", 'have', "haven't", 'having', 'he', "he'd", "he'll", "he's", 'her', 'here', "here's", 'hers', 'herself', 'him', 'himself', 'his', 'how', "how's", 'i', "i'd", "i'll", "i'm", "i've", 'if', 'in', 'into', 'is', "isn't", 'it', "it's", 'its', 'itself', 'laquo', 'lsaquo', 'ldquo', "let's", 'mdash', 'me', 'more', 'most', "mustn't", 'my', 'myself', "nbsp", 'no', 'nor', 'not', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'ought', 'our', 'ours', 'out', 'over', 'own', 'quot', 'raquo', 'rsaquo', 'rsquo', 'said', 'same', 'say', 'says',"shan't", 'she', "she'd", "she'll", "she's", 'should', "shouldn't", 'so', 'some', 'still', 'such', 'than', 'that', "that's", 'the', 'their', 'theirs', 'them', 'then', 'there', "there's", 'these', 'they', "they'd", "they'll", "they're", "they've", 'this', 'those', 'through', 'to', 'too', 'under', 'until', 'up', 'very', 'was', "wasn't", 'we', "we'd", "we'll", "we're", "we've", 'were', "weren't", 'what', "what's", 'when', "when's", 'where', "where's", 'which', 'while', 'who', "who's", 'whom', 'why', "why's", "will", "with", "won't", 'would', "wouldn't", 'you', "you'd", "you'll", "you're", "you've", 'your', 'yours']
  48.     splitter=re.compile('\\W*')
  49.     return [s.lower() for s in splitter.split(text) if len(s)>3 and s.lower() not in ignore_list]
  50.  
  51. def getarticlewords():
  52.     allwords={}
  53.     articlewords=[]
  54.     articletitles=[]
  55.     ec=0
  56.     # Loop over every feed
  57.     for feed in feedlist:
  58.         f=feedparser.parse(feed)
  59.  
  60.         # Loop over every article
  61.         for e in f.entries:
  62.             # Ignore identical articles
  63.             if e.title in articletitles: continue
  64.  
  65.             # Extract the words
  66.             txt=e.title.encode('utf8')+stripHTML(e.description.encode('utf8'))
  67.             words=separatewords(txt)
  68.             articlewords.append({})
  69.             articletitles.append(e.title)
  70.  
  71.             # Increase the counts for this word in allwords and in articlewords
  72.             for word in words:
  73.                 allwords.setdefault(word,0)
  74.                 allwords[word]+=1
  75.                 articlewords[ec].setdefault(word,0)
  76.                 articlewords[ec][word]+=1
  77.             ec+=1
  78.     return allwords, articlewords, articletitles
  79.  
  80. def makematrix(allw,articlew):
  81.     wordvec=[]
  82.  
  83.     # Only take words that are common but not too common
  84.     for w,c in allw.items():
  85.         if c>3 and c<len(articlew)*0.6:
  86.             wordvec.append(w)
  87.  
  88.     # Create the word matrix
  89.     l1=[[(word in f and f[word] or 0) for word in wordvec] for f in articlew]
  90.     return l1,wordvec
  91.  
  92. def showfeatures(w,h,titles,wordvec,out='features.txt'):
  93.     outfile=file(out,'w')
  94.     pc,wc=shape(h)
  95.     toppatterns=[[] for i in range(len(titles))]
  96.     patternnames=[]
  97.  
  98.     # Loop over all the features
  99.     for i in range(pc):
  100.         slist=[]
  101.         # Create a list of words and their weights
  102.         for j in range(wc):
  103.             slist.append((h[i,j],wordvec[j]))
  104.         # Reverse sort the wordlist
  105.         slist.sort()
  106.         slist.reverse()
  107.         # Print the first six elements
  108.         n=[s[1] for s in slist[0:6]]
  109.         outfile.write(str(n)+'\n')
  110.         patternnames.append(n)
  111.  
  112.         # Create a list of articles for this feature
  113.         flist=[]
  114.         for j in range(len(titles)):
  115.             # Add the article with its weight
  116.             flist.append((w[j,i],titles[j]))
  117.             toppatterns[j].append((w[j,i],i,titles[j]))
  118.  
  119.         # Reverse sort the list
  120.         flist.sort()
  121.         flist.reverse()
  122.  
  123.         # Show the top three articles
  124.         for f in flist[0:3]:
  125.             outfile.write(str(f)+'\n')
  126.         outfile.write('\n')
  127.  
  128.     outfile.close()
  129.     # Return the pattern names for later use
  130.     return toppatterns, patternnames
  131.  
  132. def showarticles(titles,toppatterns,patternnames,out='articles.txt'):
  133.     outfile=file(out,'w')
  134.  
  135.     # Loop over all the articles
  136.     for j in range(len(titles)):
  137.         outfile.write(titles[j].encode('utf8')+'\n')
  138.  
  139.         # Get the top features for this article and reverse sort them
  140.         toppatterns[j].sort()
  141.         toppatterns[j].reverse()
  142.  
  143.         # Print the top three patterns
  144.         for i in range(3):
  145.             outfile.write(str(toppatterns[j][i][0])+' '+
  146.                           str(patternnames[toppatterns[j][i][1]])+'\n')
  147.         outfile.write('\n')
  148.  
  149.     outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment