lolamontes69

Ch6 Ex4-Programming Collective Intelligence.

Jul 17th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. """ Chapter 6 Exercise 4: Arbitrary phrase length.
  2.  
  3.    This chapter showed you how to extract word pairs as well as individual words.
  4.    Make the feature extraction configurable to extract up to a specified number of words as a single feature.
  5.  
  6.    First with entryfeatures() from feedfilter.
  7.    Added to the function itself otherwise docclass.py needs this variable adding throughout.
  8.  
  9.    Second added to getwords() from emailfilter (ch6 ex3).
  10. """
  11. # +++++++++++++++++++
  12. # ++++++ FIRST ++++++
  13. # +++++++++++++++++++
  14.  
  15. def entryfeatures(entry):
  16.     wordnum=3                     # wordnum = number of words to add together as many words (eg 'I Love Lola')
  17.     splitter = re.compile('\\W*')
  18.     f = {}
  19.  
  20.     # Extract the title words and annotate
  21.     titlewords = [s.lower() for s in splitter.split(entry['title']) if len(s) > 2 and len(s) < 20]
  22.  
  23.     # Extract the summary words
  24.     summarywords = [s for s in splitter.split(entry['summary']) if len(s) > 2 and len(s) < 20]
  25.  
  26.     # Count uppercase words
  27.     uc = 0
  28.     for i in range(len(summarywords)):
  29.         w = summarywords[i]
  30.         if (w.isupper()):
  31.             uc += 1
  32.         f[w.lower()] = 1
  33.  
  34.         # Get word pairs in summary as features
  35.         if i < len(summarywords) - wordnum:
  36.             manywords = ' ' . join(summarywords[i:i+wordnum])
  37.             f[manywords.lower()] = 1
  38.  
  39.             # Keep creator and publisher whole
  40.             f['Publisher:'+ entry['publisher']] = 1
  41.  
  42.             # UPPERCASE is a virtual word flagging too much shouting
  43.             if (float(uc) / len(summarywords) > 0.3):
  44.                 f['UPPERCASE'] = 1
  45.  
  46.     return f
  47.  
  48. # ++++++++++++++++++++
  49. # ++++++ SECOND ++++++
  50. # ++++++++++++++++++++
  51.  
  52. def getwords(doc):
  53.     wordnum=4                     # wordnum = number of words to add together as many words (eg 'Lola loves cute guys')
  54.     splitter=re.compile('\\W*')
  55.     emails = re.findall("[\w.]+@[\w.]+", doc)
  56.     for a in range(len(emails)):  
  57.         if emails[a][-1] == '>':emails[a]= emails[a][:-1]
  58.     urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', doc)
  59.     for a in range(len(urls)):  
  60.         if urls[a][-1] == '>':urls[a]= urls[a][:-1]
  61.  
  62.     for a in urls:
  63.         doc = doc.replace(a," ")
  64.     for a in emails:
  65.         doc = doc.replace(a," ")
  66.  
  67.     doc = ''.join(i for i in doc if not i.isdigit())
  68.     words=[s.lower() for s in splitter.split(doc) if len(s)>2 and len(s)<20]
  69.  
  70.     for a in range(len(words)-wordnum+1):
  71.         phrases = ' '.join(words[a:a+wordnum])
  72.         words.append(phrases)
  73.  
  74.     words = words + urls + emails
  75.     # Return the unique set of words only
  76.     return dict([(w,1) for w in words])
Advertisement
Add Comment
Please, Sign In to add comment