Posted By

cocomo on 09/27/09


Tagged

ruby


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

imm


Split String into Sentences


 / Published in: Ruby
 

A monkey patch splitting a string into sentences.

  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  

You need to login to post a comment.