lolamontes69

Ch6 Ex6-Programming Collective Intelligence.

Jul 19th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. """ Chapter 6 Exercise 6: Other virtual features.
  2.  
  3.    There are many virtual features like UPPERCASE that can be useful in classifying documents. Documents of excessive length or with a preponderance of long words may also be clues. Implement these as features. Can you think of any others?
  4.  
  5.    I implemented this in feedfilter.entryfeatures() for summarywords > 100 and 37 words in doc longet than 7 letters long.
  6.    I also implemented a feature for short or long average sentence length.
  7.  
  8. """
  9.  
  10. def splitParagraphIntoSentences(paragraph):
  11.     sentenceEnders = re.compile(r"""
  12.        (?:  (?<=[.!?]) | (?<=[.!?]['"]) )(?<!  Mr\.   )(?<!  Mrs\.  )(?<!  Ms\.   )
  13.        (?<!  Jr\.   )(?<!  Dr\.   )(?<!  Prof\. )(?<!  Sr\.   )\s+""",re.IGNORECASE | re.VERBOSE)
  14.     return sentenceEnders.split(paragraph)
  15.  
  16. def entryfeatures(entry):
  17.     splitter = re.compile('\\W*')
  18.     f = {}
  19.  
  20.     # Check for SHORTSENTENCES or LONGSENTENCES
  21.     sentences = splitParagraphIntoSentences(entry['summary'])
  22.     totalwords = 0
  23.     for i in sentences:
  24.         lst1 = i.split(' ')
  25.         totalwords += len(lst1)
  26.     if (totalwords+0.0)/len(sentences) < 5:
  27.         f['SHORTSENTENCES'] = 1
  28.     elif (totalwords+0.0)/len(sentences) > 25:
  29.         f['LONGSENTENCES'] = 1
  30.  
  31.  
  32.     titlewords = [s.lower() for s in splitter.split(entry['title']) if len(s) > 2 and len(s) < 20]
  33.     summarywords = [s for s in splitter.split(entry['summary']) if len(s) > 2 and len(s) < 20]
  34.  
  35.     if len(summarywords) > 100: f['LONGDOC'] = 1  # Add a LONGDOC entry if more than 100 summarywords in document
  36.  
  37.     # Count uppercase words and all longwords
  38.     uc = 0
  39.     longwords = 0
  40.     for i in range(len(summarywords)):
  41.         w = summarywords[i]
  42.  
  43.         if len(w) > 7: longwords += 1             # Count words longer than 7 chars as longwords
  44.  
  45.         if (w.isupper()):
  46.             uc += 1
  47.         f[w.lower()] = 1
  48.  
  49.         # Get word pairs in summary as features
  50.         if i < len(summarywords) - 1:
  51.             twowords = ' ' . join(summarywords[i:i+1])
  52.             f[twowords.lower()] = 1
  53.             f['Publisher:'+ entry['publisher']] = 1
  54.             if (float(uc) / len(summarywords) > 0.3):
  55.                 f['UPPERCASE'] = 1
  56.  
  57.     if longwords > 37: f['LONGWORDS'] = 1      # Add a LONGWORDS entry if more than 37 longwords in document
  58.     return f
  59.  
  60.  
  61. """
  62.    #########
  63.    # USAGE #
  64.    #########
  65.  
  66.    Once this is added to feedfilter it is used in the normal way.
  67.  
  68. import docclass as docclass
  69. import feedfilter as feedfilter
  70. cl=docclass.fisherclassifier(feedfilter.entryfeatures)
  71. cl.setdb('python_feed.db')
  72.  
  73. feedfilter.readentryfeatures('python_search.xml',cl)
  74.  
  75. ###############################################################################
  76.    +++++++++++++++++++++++
  77.    Possible other features
  78.    +++++++++++++++++++++++
  79.  
  80.    Word combos that appear in the title of the document that also appear in the summary or body of the document could be given higher assumed probabilities.
  81.    Overuse of certain forms of punctuation (?!+ etc.)
  82.    Whitelisting/Blacklisting of phrases and words (including email addys.)
  83.    Overuse of adjectives and superlatives (specified in a list or dictionary initialized at startup.)
  84.    Word repetition of non-common words (ie not 'the','it','or','and' etc.)
  85.    Percentage of numbers to letters.
  86.    Percentage of punctuation to letters.
  87.    Words appearing in certain sentences together.
  88.    The comma count in a sentence.........
  89.    +++++++++++++++++++++++
  90. """
Advertisement
Add Comment
Please, Sign In to add comment