Mega Code Archive

 
Categories / Ruby / String
 

Get sentences from a paragraph

text = %q{ this is a test. This is another test. } sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|\!/) sentences_sorted = sentences.sort_by { |sentence| sentence.length } one_third = sentences_sorted.length / 3 ideal_sentences = sentences_sorted.slice(one_third, one_third + 1) ideal_sentences = ideal_sentences.select { |sentence| sentence =~ /is|are/ } puts ideal_sentences.join(". ")