Class: GenderGuessor::Detector
- Inherits:
-
Object
- Object
- GenderGuessor::Detector
- Defined in:
- lib/gender_guess.rb
Overview
Get gender by first name
Instance Method Summary collapse
-
#get_gender(name, country = nil) ⇒ Object
Returns best gender for the given name and country pair.
-
#get_names ⇒ Object
Retrieve all name dict.
-
#initialize(case_sensitive = true) ⇒ Detector
constructor
Creates a detector.py parsing given data file.
Constructor Details
#initialize(case_sensitive = true) ⇒ Detector
Creates a detector.py parsing given data file
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gender_guess.rb', line 32 def initialize(case_sensitive = true) # initializer for Detector class # # Arguments: # case_sensitive: (bool) @case_sensitive = case_sensitive # Opens data file and for each line @names = {} File.readlines(NAME_DICT_FILE, chomp: true).each do | line | # Parses one line of data file if line.start_with?("#") || line.start_with?("=") next end parts = line.split country_values = line[30...(line.length - 1)] # line[30...(line.length - 1)] || parts[2].strip name = parts[1].strip gender = parts[0].strip # lowercase the name if !case_sensitive name = name.downcase end # puts "#{name} #{gender} #{country_values}" # debug case gender when "M" gender_label = "male" when "1M", "?M" gender_label = "mostly_male" when "F" gender_label = "female" when "1F", "?F" gender_label = "mostly_female" when "?" # androgynous gender_label = "andy" else raise RuntimeError, "Not sure what to do with a sex of #{gender}" end set(name, gender_label, country_values) end end |
Instance Method Details
#get_gender(name, country = nil) ⇒ Object
Returns best gender for the given name and country pair
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gender_guess.rb', line 81 def get_gender(name, country=nil) # Guess gender from first name # Example: # detector.get_gender( "yourname" ) # => "Male" # # Arguments: # name: (str) # name to lower if !@case_sensitive name = name.downcase end if !@names.key?(name) # if no such name return "unknown" elsif country.nil? counter = ->(country_values) do values = country_values.scan(/\d/).map(&:ord) total = 0 values.each do |val| total += ((val > 64 && val - 55) || val - 48) end return [values.length, total] end return most_popular_gender(name, counter) elsif COUNTRIES.include?(country) index = COUNTRIES.find_index( country ) counter = ->(e) { [e[index].ord - 32, 0]} return most_popular_gender(name, counter) else raise ArgumentError, "No such country: #{country}" end end |
#get_names ⇒ Object
Retrieve all name dict
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gender_guess.rb', line 122 def get_names() # Retrieve all name dict # # Example # obj.get_names # => { "name": # {"male": " 1 6 2 "}, # ... # } @names end |