Skip to content

Instantly share code, notes, and snippets.

@IrakliJani
Created May 16, 2014 22:09
Show Gist options
  • Select an option

  • Save IrakliJani/813f4f81bed219b58512 to your computer and use it in GitHub Desktop.

Select an option

Save IrakliJani/813f4f81bed219b58512 to your computer and use it in GitHub Desktop.
def top_3_words(text)
text
.gsub(/[^A-Za-z ']+/, '')
.split(/\s+/)
.inject(Hash.new 0) { |v, w| v[w.downcase] += 1; v }
.sort_by { |w, c| c }
.reverse
.map { |v, k| v }
.reject(&:empty?)
.select { |e| e =~ /\w/i }
.first(3)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment