Module: NameGenderClassifier

Defined in:
lib/name_gender_classifier.rb,
lib/name_gender_classifier/database_manager.rb,
lib/name_gender_classifier/fallback_gender_detector.rb

Overview

Gender detector for first names.

Defined Under Namespace

Modules: DatabaseManager, FallbackGenderDetector

Class Method Summary collapse

Class Method Details

.classify(arg, options = {}) ⇒ String, ...

Return the gender(s) (probability) for the informed name(s). The result type will vary depending on the parameter type:

String, Symbol

the gender (String) is returned.

Array<String>

an array (Array<String>) with the genders is returned.

Array<Object>

an array (Array<Object>) with the same objects and the newly assigned genders is returned.

Parameters:

  • arg (String, Symbol, Array<String>, Array<Symbol>, Array<Object>)

    argument holding first name(s) information(s).

  • options (Hash) (defaults to: {})

    first_name_attribute: name of the method that returns the first name, gender_attribute: name of the method which will receive the gender assignment.

Returns:

  • (String, Array<String>, Array<Object>)

    the gender classification for the passed first names



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/name_gender_classifier.rb', line 18

def self.classify(arg, options = {})
  if arg.is_a?(String) || arg.is_a?(Symbol)
    most_probable_gender(arg)
  elsif arg.respond_to?(:[]) && arg.respond_to?(:each)
    # Assumes that all elements within the array are of the same type as the first.
    if arg[0].is_a?(String) || arg[0].is_a?(Symbol)
      classify_array(arg)
    else
      classify_objects(arg, options)
    end
  end
end

.classify_array(array) ⇒ Array<String>

Return the genders (probabilistically) for the informed names.

Parameters:

  • array (Array<String>, Array<Symbol>)

    see classify

Returns:



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

def self.classify_array(array)
  result = []
  DatabaseManager.gdbm do |db|
    array.each do |name|
      result << nil && next unless name

      result << most_probable_gender(name, db)
    end
  end

  result
end

.classify_objects(objects, options = {}) ⇒ Array<Object>

For each object in the array, it tries to classify the gender for object.first_name or object.name (or equivalent method) and save it on object.gender (or equivalent method).

Parameters:

  • objects (Array<Object>)

    see classify

  • options (defaults to: {})

    see classify

Returns:

  • (Array<Object>)

    the objects with the assigned genders



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/name_gender_classifier.rb', line 56

def self.classify_objects(objects, options = {})
  first_name_attribute = options.fetch(:first_name_attribute, nil) ||
                         (:first_name if defined?(objects[0].first_name)) ||
                         (:name if defined?(objects[0].name))

  if first_name_attribute.nil?
    puts 'The object doesn\'t have the methods \'name\' nor \'first_name\'. '\
        'Use #classify(arg, first_name_attribute: nil, gender_attribute: nil) '\
        'to inform which methods to lookup.'

    return objects
  end

  gender_attribute = options.fetch(:gender_attribute, 'gender')
  gender_attribute_assignment = "#{gender_attribute}="

  DatabaseManager.gdbm do |db|
    objects.each do |object|
      next unless name = object.public_send(first_name_attribute)

      object.public_send(gender_attribute_assignment, most_probable_gender(name, db))
    end
  end

  objects
end