lolamontes69

Python: Regex to split paragraphs into sentences.

Jul 18th, 2013
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. """ To be adapted for extracting features for document classification.
  2.    Original from Stack Overflow;
  3. http://stackoverflow.com/questions/8465335/a-regex-for-extracting-sentence-from-a-paragraph-in-python
  4. """
  5.  
  6. import re as re
  7.  
  8. def splitParagraphIntoSentences(paragraph):
  9.     sentenceEnders = re.compile(r"""
  10.        # Split sentences on whitespace between them.
  11.        (?:               # Group for two positive lookbehinds.
  12.          (?<=[.!?])      # Either an end of sentence punct,
  13.        | (?<=[.!?]['"])  # or end of sentence punct and quote.
  14.        )                 # End group of two positive lookbehinds.
  15.        (?<!  Mr\.   )    # Don't end sentence on "Mr."
  16.        (?<!  Mrs\.  )
  17.        (?<!  Ms\.   )
  18.        (?<!  Jr\.   )
  19.        (?<!  Dr\.   )
  20.        (?<!  Prof\. )
  21.        (?<!  Sr\.   )
  22.        \s+               # Split on whitespace between sentences.
  23.        """,
  24.         re.IGNORECASE | re.VERBOSE)
  25.     return sentenceEnders.split(paragraph)
  26.  
  27. if __name__ == '__main__':
  28.     f = open("your_fav.txt", 'r')
  29.     text = f.read()
  30.     sentences = splitParagraphIntoSentences(text)
  31.     longsentences = 0
  32.     sentencecount = 0
  33.     totalwords = 0
  34.     for i in sentences:
  35.         print i.strip()       # Voila, a sentence
  36.         lst1 = i.split(' ')
  37.         print "sentence length",len(lst1)
  38.         if len(lst1) > 20: longsentences += 1
  39.         sentencecount += 1
  40.         totalwords += len(lst1)
  41.         print '------------------------------------------------------'
  42.     print "Number of sentences =",sentencecount
  43.     print "Number of words     =",totalwords
  44.     print "Long sentences      =",longsentences
  45.     print "Av sentence length  =",(totalwords+0.0)/sentencecount
Advertisement
Add Comment
Please, Sign In to add comment