Class: Teebo::Name

Inherits:
TeeboGenerator show all
Defined in:
lib/teebo/name.rb

Overview

Generates names in accordance with their frequency in the United States population.

Constant Summary collapse

GIVEN_NAMES_TABLE =
'given_names'
SURNAMES_TABLE =
'surnames'

Class Method Summary collapse

Methods inherited from TeeboGenerator

db_connection, yaml_mapping

Class Method Details

.generate_full_name(sex = nil) ⇒ Object

Generates a normal full name, including a middle name.

For now, this is fairly sloppy, as the probability that a middle name will simply be another given name of the same gender is almost certainly less than 100%.



29
30
31
32
33
34
35
# File 'lib/teebo/name.rb', line 29

def self.generate_full_name(sex=nil)
  # TODO: Take into account different probabilities of different types of middle names.
  if sex.nil?
    sex = %w(M F).sample
  end
  generate_given_name(sex) + ' ' + generate_given_name(sex) + ' ' + generate_surname
end

.generate_given_name(sex) ⇒ Object

Selects a random (weighted) given name from the database.



47
48
49
50
51
52
# File 'lib/teebo/name.rb', line 47

def self.generate_given_name(sex)
  count = sum_count(sex)
  selection = rand(count)
  db_connection.get_row_for_count(GIVEN_NAMES_TABLE, 'count_to', selection,
                            {column: 'sex', condition: sex})['name']
end

.generate_name(sex = nil) ⇒ Object

Picks a random first & last name, selecting a random gender if it’s not specified.



15
16
17
18
19
20
# File 'lib/teebo/name.rb', line 15

def self.generate_name (sex=nil)
  if sex.nil?
    sex = %w(M F).sample
  end
  generate_given_name(sex) + ' ' + generate_surname
end

.generate_surnameObject

Selects a random (weighted) surname from the database.



57
58
59
60
61
# File 'lib/teebo/name.rb', line 57

def self.generate_surname
  count = db_connection.get_sum(SURNAMES_TABLE, 'count')
  selection = rand(count)
  db_connection.get_row_for_count(SURNAMES_TABLE, 'count_to', selection)['name']
end

.sum_count(sex) ⇒ Object

Finds the total count for the number of names in the database.



40
41
42
# File 'lib/teebo/name.rb', line 40

def self.sum_count(sex)
  db_connection.get_sum(GIVEN_NAMES_TABLE, 'count', {column: 'sex', condition: sex})
end