Advertisement
VladikOtez

Search

May 22nd, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.25 KB | None | 0 0
  1. def top_3_words(text)
  2.   text
  3.   .split(/\s+/)
  4.   .inject(Hash.new(0)) { |key, val| key[val.downcase] += 1; key }
  5.   .sort_by { |key, val| val }
  6.   .collect! { |v, k| v }
  7.   .reject(&:empty?)
  8.   .select { |e| e =~ /\w/i }
  9.    .reverse
  10.    .first(3)
  11. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement