Class: SexMachine::Detector
- Inherits:
-
Object
- Object
- SexMachine::Detector
- Defined in:
- lib/sexmachine/detector.rb
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 ]
- 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 }
Instance Method Summary collapse
- #get_gender(name, country = nil) ⇒ Object
-
#initialize(opts = {}) ⇒ Detector
constructor
A new instance of Detector.
- #inspect ⇒ Object
- #knows_country?(country) ⇒ Boolean
- #name_exists?(name) ⇒ Boolean
- #parse(fname) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Detector
Returns a new instance of Detector.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sexmachine/detector.rb', line 31 def initialize(opts = {}) opts = { :filename => File.('../data/nam_dict.txt', __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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sexmachine/detector.rb', line 61 def get_gender(name, country = nil) name = UnicodeUtils.downcase(name) unless @case_sensitive if not name_exists?(name) @unknown_value elsif country.nil? most_popular_gender(name) { |country_values| country_values.split("").select { |l| l.strip != "" }.length } 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
79 80 81 |
# File 'lib/sexmachine/detector.rb', line 79 def inspect "#<#{self.class.name} filename=\"#{@filename}\" case_sensitive=#{@case_sensitive} unknown_value=#{@unknown_value}>" end |
#knows_country?(country) ⇒ Boolean
52 53 54 |
# File 'lib/sexmachine/detector.rb', line 52 def knows_country?(country) COUNTRIES.include?(country) or ISO_3166_MAPPING.include?(country) end |
#name_exists?(name) ⇒ Boolean
56 57 58 59 |
# File 'lib/sexmachine/detector.rb', line 56 def name_exists?(name) name = UnicodeUtils.downcase(name) unless @case_sensitive @names.has_key?(name) ? name : false end |
#parse(fname) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/sexmachine/detector.rb', line 43 def parse(fname) @names = {} open(fname, "r:iso8859-1:utf-8") { |f| f.each_line { |line| eat_name_line line } } end |