Class: WordCounters

Inherits:
Object
  • Object
show all
Defined in:
lib/genderstat/word_counters.rb

Instance Method Summary collapse

Constructor Details

#initializeWordCounters

Returns a new instance of WordCounters.



4
5
6
7
8
9
10
11
12
13
14
15
# 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

Instance Method Details

#check(word) ⇒ Object



18
19
20
# 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) ⇒ Object



28
29
30
31
32
# 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_ratiosObject

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.


38
39
40
41
42
43
44
45
46
47
# 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_totalsObject



22
23
24
25
26
# 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) ⇒ Object



49
50
51
# File 'lib/genderstat/word_counters.rb', line 49

def round float
  float.round(Genderstat::DECIMAL_DIGITS_OF_PRECISION)
end