Ruby/String/select

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

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



Remove the stop words

text = %q{this is a test.}
stop_words = %w{is a}
words = text.scan(/\w+/)
key_words = words.select { |word| !stop_words.include?(word) }
puts key_words.join(" ")