class Teebo::Name

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

Constants

GIVEN_NAMES_TABLE
SURNAMES_TABLE

Public Class Methods

generate_full_name(sex=nil) click to toggle source

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%.

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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_surname() click to toggle source

Selects a random (weighted) surname from the database.

# 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) click to toggle source

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

# 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