lolamontes69

feedfilter.py for Ch6 Programming Collective Intelligence

Jul 11th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. """ entryfeatures() is corrected so it actually flags UPPERCASE's
  2.    and readentryfeatures() is the version of read() corrected for
  3.  
  4.         cl=docclass.fisherclassifier(feedfilter.entryfeatures)
  5.         cl.setdb('python_feed1.db')
  6.         feedfilter.readentryfeatures('mini_python_search.xml',cl)
  7. """
  8.  
  9. import feedparser as feedparser
  10. import re as re
  11.  
  12. # Takes a filename or URL of a blog feed and classifies the entries
  13. def read(feed,classifier):
  14.     # Get feed entries and loop over them
  15.     f=feedparser.parse(feed)
  16.     for entry in f['entries']:
  17.         print
  18.         print '-----'
  19.         # Print the contents of the entry
  20.         print 'Title:     '+entry['title'].encode('utf-8')
  21.         print 'Publisher  '+entry['publisher'].encode('utf-8')
  22.         print
  23.         print entry['summary'].encode('utf-8')
  24.  
  25.         # Combine all the text to create one item for the classifier
  26.         fulltext='%s\n%s\n%s' % (entry['title'],entry['publisher'],entry['summary'])
  27.  
  28.         # Print the best guess at the current category
  29.         print 'Guess: '+str(classifier.classify(fulltext))
  30.  
  31.         # Ask the user to specify the correct category and train on that
  32.         cl=raw_input('Enter category: ')
  33.         classifier.train(fulltext,cl)
  34.  
  35. def readentryfeatures(feed,classifier):
  36.     # Get feed entries and loop over them
  37.     f=feedparser.parse(feed)
  38.     for entry in f['entries']:
  39.         print
  40.         print '-----'
  41.         # Print the contents of the entry
  42.         print 'Title:     '+entry['title'].encode('utf-8')
  43.         print 'Publisher  '+entry['publisher'].encode('utf-8')
  44.         print
  45.         print entry['summary'].encode('utf-8')
  46.  
  47.         # Combine all the text to create one item for the classifier
  48.         fulltext='%s\n%s\n%s' % (entry['title'],entry['publisher'],entry['summary'])
  49.  
  50.         # Print the best guess at the current category
  51.         print 'Guess: '+str(classifier.classify(entry))
  52.  
  53.         # Ask the user to specify the correct category and train on that
  54.         cl=raw_input('Enter category: ')
  55.         classifier.train(entry,cl)
  56.  
  57.  
  58. def entryfeatures(entry):
  59.     splitter = re.compile('\\W*')
  60.     f = {}
  61.  
  62.     # Extract the title words and annotate
  63.     titlewords = [s.lower() for s in splitter.split(entry['title']) if len(s) > 2 and len(s) < 20]
  64.  
  65.     # Extract the summary words
  66.     summarywords = [s for s in splitter.split(entry['summary']) if len(s) > 2 and len(s) < 20]
  67.  
  68.     # Count uppercase words
  69.     uc = 0
  70.     for i in range(len(summarywords)):
  71.         w = summarywords[i]
  72.  
  73.         if (w.isupper()):
  74.             uc += 1
  75.  
  76.         f[w.lower()] = 1
  77.  
  78.         # Get word pairs in summary as features
  79.         if i < len(summarywords) - 1:
  80.             twowords = ' ' . join(summarywords[i:i+1])
  81.             f[twowords.lower()] = 1
  82.  
  83.             # Keep creator and publisher whole
  84.             f['Publisher:'+ entry['publisher']] = 1
  85.  
  86.             # UPPERCASE is a virtual word flagging too much shouting (if percentage of upper >30%)
  87.             if (float(uc) / len(summarywords) > 0.3):
  88.                 f['UPPERCASE'] = 1
  89.  
  90.     return f
Advertisement
Add Comment
Please, Sign In to add comment