Last active
December 22, 2015 16:28
-
-
Save joejag/6499202 to your computer and use it in GitHub Desktop.
Word Count in Ruby
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 Phrase | |
def initialize(words, frequency_counter=Frequencies.new, words_cleaner=WordsCleaner.new) | |
@words = words | |
@frequency_counter = frequency_counter | |
@words_cleaner = words_cleaner | |
end | |
def word_count | |
cleaned_words = @words_cleaner.clean @words | |
@frequency_counter.frequencies_for cleaned_words | |
end | |
end | |
class WordsCleaner | |
def clean words | |
alpha_numeric_only(words).downcase.split | |
end | |
def alpha_numeric_only words | |
words.gsub(/[^a-z0-9]/i, ' ') | |
end | |
end | |
class Frequencies | |
def frequencies_for tokens | |
tokens.group_by {|e| e }.each_with_object({}) do |(word, occurs), result| | |
result[word] = occurs.length | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment