Class: GenderDetector
- Inherits:
-
Object
- Object
- GenderDetector
- Defined in:
- lib/gender_detector.rb,
lib/gender_detector/version.rb
Overview
Main class for interacting with the data file
Constant Summary collapse
- COUNTRIES =
%i[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 =
'2.0.0'.freeze
Instance Method Summary collapse
- #get_gender(name, country = nil) ⇒ Object
-
#initialize(opts = {}) ⇒ GenderDetector
constructor
A new instance of GenderDetector.
- #inspect ⇒ Object
- #knows_country?(country) ⇒ Boolean
- #name_exists?(name) ⇒ Boolean
- #parse(fname) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ GenderDetector
Returns a new instance of GenderDetector.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gender_detector.rb', line 39 def initialize(opts = {}) relpath = '../gender_detector/data/nam_dict.txt' opts = { filename: File.(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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gender_detector.rb', line 70 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('').reject { |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 |
#inspect ⇒ Object
88 89 90 91 |
# File 'lib/gender_detector.rb', line 88 def inspect "#<#{self.class.name} filename=\"#{@filename}\" " \ " case_sensitive=#{@case_sensitive} unknown_value=#{@unknown_value}>" end |
#knows_country?(country) ⇒ Boolean
61 62 63 |
# File 'lib/gender_detector.rb', line 61 def knows_country?(country) COUNTRIES.include?(country) || ISO_3166_MAPPING.include?(country) end |
#name_exists?(name) ⇒ Boolean
65 66 67 68 |
# File 'lib/gender_detector.rb', line 65 def name_exists?(name) name = downcase(name) unless @case_sensitive @names.key?(name) ? name : false end |
#parse(fname) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/gender_detector.rb', line 52 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 |