/ Published in: Ruby
A monkey patch splitting a string into sentences.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class String def split_sentence ary = self.gsub(/\n/," ").split(/([^\.\?\!]+[\.\?\!])/) ary.delete("") sentences = Array.new str = "" for i in 0..ary.size-1 next if ary[i].size == 0 || ary[i] =~ /^\s*$/ str << ary[i] next if str =~ /Mr|Mrs|Ms|Dr|Mt|St\.$/ if (i < ary.size-1) next if ary[i] =~ /[A-Z]\.$/ next if ary[i+1] =~ /^\s*[a-z]/ end if ary[i+1] =~ /^\"/ str << '"' ary[i+1].sub!(/^\"/,"") elsif ary[i+1] =~ /^\)/ str << ')' ary[i+1].sub!(/^\)/,"") end sentences << str.sub(/^\s+/,"") str = "" end sentences end end