Split String into Sentences


/ Published in: Ruby
Save to your folder(s)

A monkey patch splitting a string into sentences.


Copy this code and paste it in your HTML
  1. class String
  2. def split_sentence
  3. ary = self.gsub(/\n/," ").split(/([^\.\?\!]+[\.\?\!])/)
  4. ary.delete("")
  5. sentences = Array.new
  6. str = ""
  7. for i in 0..ary.size-1
  8. next if ary[i].size == 0 || ary[i] =~ /^\s*$/
  9. str << ary[i]
  10. next if str =~ /Mr|Mrs|Ms|Dr|Mt|St\.$/
  11. if (i < ary.size-1)
  12. next if ary[i] =~ /[A-Z]\.$/
  13. next if ary[i+1] =~ /^\s*[a-z]/
  14. end
  15. if ary[i+1] =~ /^\"/
  16. str << '"'
  17. ary[i+1].sub!(/^\"/,"")
  18. elsif ary[i+1] =~ /^\)/
  19. str << ')'
  20. ary[i+1].sub!(/^\)/,"")
  21. end
  22. sentences << str.sub(/^\s+/,"")
  23. str = ""
  24. end
  25. sentences
  26. end
  27. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.