Skip to content

Instantly share code, notes, and snippets.

@mowat27
Forked from joejag/word_count.rb
Last active December 22, 2015 16:49
Show Gist options
  • Save mowat27/6502220 to your computer and use it in GitHub Desktop.
Save mowat27/6502220 to your computer and use it in GitHub Desktop.
Made the relationship between words cleaner and frequencies more cohesive. I would look at the naming of WordsCleaner now but I don't want to mess with the code too much and fail to demonstrate the core point - perhaps Tokeniser would be more descriptive though
class Phrase
def initialize(words)
words_cleaner = WordsCleaner.new(words)
@frequency_counter = Frequencies.new(words_cleaner)
end
def word_count
@frequency_counter.frequencies
end
end
class WordsCleaner
def initialize(words)
@words = words
end
def clean
alpha_numeric_only(@words).downcase.split
end
private
def alpha_numeric_only words
words.gsub(/[^a-z0-9]/i, ' ')
end
end
class Frequencies
def initialize(words_cleaner)
@words_cleaner = words_cleaner
end
def frequencies
tokens = @words_cleaner.clean
tokens.group_by {|e| e }.each_with_object({}) do |(word, occurs), result|
result[word] = occurs.length
end
end
end
@joejag
Copy link

joejag commented Sep 9, 2013

Yeah, I see the point now. Having what was in lines 8-9 in 33 keeps the data inside the Frequencies class.

I like that it drops the method args needed too.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment