/word_frequency.rb Secret
Created
May 1, 2014 20:44
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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