Class: StaticGenderizer::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/static_genderizer/loader.rb

Overview

Loads per-language CSVs into in-memory lookup tables.

Expected CSV format: headers name,gender

  • if gender is present and equals ‘M’ or ‘F’ (case-insensitive) -> treat as first name entry

  • if gender empty -> treat as last name entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Loader

Returns a new instance of Loader.



16
17
18
19
20
21
22
23
# File 'lib/static_genderizer/loader.rb', line 16

def initialize(config)
  @config = config
  @languages = []
  # first_names[lang] => { normalized_name => [genders...] }
  @first_names = Hash.new { |h, k| h[k] = {} }
  # last_names[lang] => Set of normalized last names
  @last_names = Hash.new { |h, k| h[k] = Set.new }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/static_genderizer/loader.rb', line 14

def config
  @config
end

#first_namesObject (readonly)

Returns the value of attribute first_names.



14
15
16
# File 'lib/static_genderizer/loader.rb', line 14

def first_names
  @first_names
end

#languagesObject (readonly)

Returns the value of attribute languages.



14
15
16
# File 'lib/static_genderizer/loader.rb', line 14

def languages
  @languages
end

#last_namesObject (readonly)

Returns the value of attribute last_names.



14
15
16
# File 'lib/static_genderizer/loader.rb', line 14

def last_names
  @last_names
end

Instance Method Details

#load_all_languagesObject

Load all configured languages (config.languages). Normalizes languages to symbols.



26
27
28
29
30
31
# File 'lib/static_genderizer/loader.rb', line 26

def load_all_languages
  @languages = Array(config.languages).map { |l| l.to_s.downcase.to_sym }
  @languages.each do |lang|
    load_language(lang)
  end
end

#load_language(lang) ⇒ Object

Loads a specific language (by symbol or string)



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/static_genderizer/loader.rb', line 34

def load_language(lang)
  lang = lang.to_s.downcase.to_sym
  return unless config.languages.map { |l| l.to_s.downcase.to_sym }.include?(lang)

  base = File.join(config.data_path, lang.to_s)
  csv_file = base + ".csv"

  if File.exist?(csv_file)
    load_names_csv(lang, csv_file)
  else
    warn "StaticGenderizer: CSV not found for language #{lang} at #{csv_file}"
  end
end