Module: NameGenderClassifier::DatabaseManager

Defined in:
lib/name_gender_classifier/database_manager.rb

Overview

Uses GDBM database to retrieve gender classification from DB_NAME.

Constant Summary collapse

DB_NAME =

Returns the database location (which holds the classified names).

Returns:

  • (String)

    the database location (which holds the classified names)

"#{Gem.loaded_specs['name_gender_classifier'].gem_dir}/lib/"\
'name_gender_classifier/classified_names_pt-br.db'

Class Method Summary collapse

Class Method Details

.find(key) ⇒ Float

Find in the database the value for a previously saved key. The key holds the first name and the value the gender probability.

Parameters:

  • key (String, Symbol)

    a key to be searched in the database

Returns:

  • (Float)

    the gender probability (value between 0 and 1, where 0 <= male < 0.5 <= female <= 1)



18
19
20
21
22
23
24
# File 'lib/name_gender_classifier/database_manager.rb', line 18

def self.find(key)
  value = gdbm[key.to_s]
  gdbm.close
  @gdbm = nil

  value ? value.to_f : nil
end

.gdbmGDBM?

With a block { |db| … } allow to read multiple records with a single database open request, or return the database instance for a single read request.

Returns:

  • (GDBM, nil)

    the GDBM database instance or nil if used with a block



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/name_gender_classifier/database_manager.rb', line 31

def self.gdbm
  @gdbm ||= GDBM.new(DB_NAME)

  if block_given?
    yield(@gdbm)

    @gdbm.close
    @gdbm = nil
  else
    @gdbm
  end
end