Skip to content

Instantly share code, notes, and snippets.

@ss2k
Created May 1, 2014 20:44
Show Gist options
  • Save ss2k/0eba635d796338414bf9 to your computer and use it in GitHub Desktop.
Save ss2k/0eba635d796338414bf9 to your computer and use it in GitHub Desktop.
class WordFrequency
def initialize(words)
@words = words.split(/\W+/)
end
def get_frequency
h = Hash.new(0)
@words.each do |w|
h[w] += 1
end
return h
end
def top_words(n)
get_frequency.sort_by { |key, value| value }.reverse[0..n-1]
end
end
require 'benchmark'
time = Benchmark.measure do
RANDOM_WORDS = %w[something absolutely computer what can watch understand lose water magic words believe structure really different help meaning cell chip driver cup wheel]
word_arrays = []
word_arays = 1500000.times { word_arrays << RANDOM_WORDS.sample }
words = word_arrays.join(" ")
freq = WordFrequency.new(words)
p freq.top_words(5)
end
puts time
# 2.070000 0.040000 2.110000 ( 2.123516)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment