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
-
.classify(arg, options = {}) ⇒ String, ...
Return the gender(s) (probability) for the informed name(s).
-
.classify_array(array) ⇒ Array<String>
Return the genders (probabilistically) for the informed names.
-
.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).
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.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/name_gender_classifier.rb', line 18 def self.classify(arg, = {}) 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, ) end end end |
.classify_array(array) ⇒ Array<String>
Return the genders (probabilistically) for the informed names.
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).
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, = {}) first_name_attribute = .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 = .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 |