class WordCounters
Public Class Methods
new()
click to toggle source
# File lib/genderstat/word_counters.rb, line 4 def initialize @word_counters = [] # The word lists are two directories higher than this file, so that's # how we have to reference their locations this_file_path = File.dirname(__FILE__) word_list_relative_location = "../../*_words.yaml" word_list_location = File.join(this_file_path, word_list_relative_location) Dir.glob(word_list_location).each do |filename| @word_counters << WordCounter.new(filename) end end
Public Instance Methods
check(word)
click to toggle source
# File lib/genderstat/word_counters.rb, line 18 def check word @word_counters.each { |wc| wc.is_in_here? word } end
get_percentages(total_word_count)
click to toggle source
# File lib/genderstat/word_counters.rb, line 28 def get_percentages total_word_count @word_counters.each_with_object({}) do |wc, hash| hash[wc.name] = round( 100 * (wc.count.to_f / total_word_count)) end end
get_ratios()
click to toggle source
We get the ratios of counts among all word_lists This returns a hash with keys that are named “name_to_other_name” This loop does twice as much work as it needs to, since the ratios
are reciprocals of one another, but oh well! It's only division.
# File lib/genderstat/word_counters.rb, line 38 def get_ratios @word_counters.each_with_object({}) do |wc, hash| # We skip over the ratio of name_to_name, since it'll always be one @other_word_counters = @word_counters - [wc] @other_word_counters.each do |other_wc| ratio = round(( wc.count.to_f / other_wc.count.to_f) ) hash["#{wc.name}_to_#{other_wc.name}"] = ratio end end end
get_totals()
click to toggle source
# File lib/genderstat/word_counters.rb, line 22 def get_totals @word_counters.each_with_object({}) do |wc, hash| hash[wc.name] = wc.count end end
round(float)
click to toggle source
# File lib/genderstat/word_counters.rb, line 49 def round float float.round(Genderstat::DECIMAL_DIGITS_OF_PRECISION) end