Class: GenderDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/gender_detector.rb,
lib/gender_detector/version.rb

Overview

Main class for interacting with the data file

Constant Summary collapse

COUNTRIES =
[:great_britain, :ireland, :usa, :italy, :malta, :portugal,
:spain, :france, :belgium, :luxembourg, :the_netherlands,
:east_frisia, :germany, :austria, :swiss, :iceland, :denmark,
:norway, :sweden, :finland, :estonia, :latvia, :lithuania,
:poland, :czech_republic, :slovakia, :hungary, :romania,
:bulgaria, :bosniaand, :croatia, :kosovo, :macedonia,
:montenegro, :serbia, :slovenia, :albania, :greece, :russia,
:belarus, :moldova, :ukraine, :armenia, :azerbaijan, :georgia,
:the_stans, :turkey, :arabia, :israel, :china, :india, :japan,
:korea, :vietnam, :other_countries].freeze
ISO_3166_MAPPING =
{
  'AE' => :arabia, 'AL' => :albania, 'AM' => :armenia, 'AT' => :austria,
  'AU' => :usa, 'AZ' => :azerbaijan, 'BA' => :bosniaand, 'BE' => :belgium,
  'BG' => :bulgaria, 'BH' => :arabia, 'BY' => :belarus, 'CA' => :usa,
  'CH' => :swiss, 'CN' => :china, 'CZ' => :czech_republic, 'DE' => :germany,
  'DK' => :denmark, 'EE' => :estonia, 'EG' => :arabia, 'ES' => :spain,
  'FI' => :finland, 'FR' => :france, 'GB' => :great_britain, 'GE' => :georgia,
  'GR' => :greece, 'HK' => :china, 'HR' => :croatia, 'HU' => :hungary,
  'IE' => :ireland, 'IL' => :israel, 'IN' => :india, 'IS' => :iceland,
  'IT' => :italy, 'JP' => :japan, 'KP' => :korea, 'KR' => :korea,
  'KZ' => :the_stans, 'LT' => :lithuania, 'LU' => :luxembourg,
  'LV' => :latvia, 'MD' => :moldova, 'ME' => :montenegro, 'MK' => :macedonia,
  'MT' => :malta, 'NL' => :the_netherlands, 'NO' => :norway, 'PL' => :poland,
  'PT' => :portugal, 'QA' => :arabia, 'RO' => :romania, 'RS' => :serbia,
  'RU' => :russia, 'SA' => :arabia, 'SE' => :sweden, 'SI' => :slovenia,
  'SK' => :slovakia, 'TR' => :turkey, 'TW' => :china, 'UA' => :ukraine,
  'US' => :usa, 'UZ' => :the_stans, 'VN' => :vietnam
}.freeze
VERSION =
'1.0.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GenderDetector

Returns a new instance of GenderDetector.



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

def initialize(opts = {})
  relpath = '../gender_detector/data/nam_dict.txt'
  opts = {
    filename: File.expand_path(relpath, __FILE__),
    case_sensitive: true,
    unknown_value: :andy
  }.merge(opts)
  @filename = opts[:filename]
  @case_sensitive = opts[:case_sensitive]
  @unknown_value = opts[:unknown_value]
  parse opts[:filename]
end

Instance Method Details

#get_gender(name, country = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gender_detector.rb', line 66

def get_gender(name, country = nil)
  name = downcase(name) unless @case_sensitive

  if !name_exists?(name)
    @unknown_value
  elsif country.nil?
    most_popular_gender(name) do |country_values|
      country_values.split('').select { |l| l.strip != '' }.length
    end
  elsif COUNTRIES.include?(country)
    most_popular_gender_in_country(name, country)
  elsif ISO_3166_MAPPING.include?(country)
    most_popular_gender_in_country(name, ISO_3166_MAPPING[country])
  else
    raise "No such country: #{country}"
  end
end

#inspectObject



84
85
86
87
# File 'lib/gender_detector.rb', line 84

def inspect
  "#<#{self.class.name} filename=\"#{@filename}\" " \
    " case_sensitive=#{@case_sensitive} unknown_value=#{@unknown_value}>"
end

#knows_country?(country) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/gender_detector.rb', line 57

def knows_country?(country)
  COUNTRIES.include?(country) || ISO_3166_MAPPING.include?(country)
end

#name_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/gender_detector.rb', line 61

def name_exists?(name)
  name = downcase(name) unless @case_sensitive
  @names.key?(name) ? name : false
end

#parse(fname) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/gender_detector.rb', line 48

def parse(fname)
  @names = {}
  open(fname, 'r:iso8859-1:utf-8') do |f|
    f.each_line do |line|
      eat_name_line line
    end
  end
end