Get sentences from a paragraph : select « String « Ruby






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(". ")

 








Related examples in the same category

1.Remove the stop words