Return to Snippet

Revision: 18291
at September 27, 2009 07:43 by cocomo


Updated Code
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

Revision: 18290
at September 27, 2009 07:13 by cocomo


Initial Code
class String
  def split_sentence
    ary = self.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

Initial URL

                                

Initial Description
A monkey patch splitting a string into sentences.

Initial Title
Split String into Sentences

Initial Tags
ruby

Initial Language
Ruby