Last active
March 24, 2016 00:16
-
-
Save MollsReis/c59746dee146e70bfc51 to your computer and use it in GitHub Desktop.
Name generation using Markov chains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example list of names generated from http://listofrandomnames.com/ | |
# tweakable values | |
PREFIX = 3 | |
MAX_LENGTH = 30 | |
MIN_WORDS = 2 | |
MAX_WORDS = 3 | |
# create dictionary from corpus of names | |
dict = Hash.new { |h,k| h[k] = [] } | |
DATA.readlines.each do |name| | |
letters = "^#{name.strip.downcase}$".chars | |
while letters.length > PREFIX do | |
dict[letters.first(PREFIX).join] << letters[PREFIX] | |
letters.shift | |
end | |
end | |
# generate the name | |
name = '' | |
while name[-1] != '$' | |
if dict.include?(name[-PREFIX..-1]) | |
name += dict[name[-PREFIX..-1]].sample | |
else | |
name = dict.keys.select { |k| k[0] == '^' }.sample | |
end | |
if name.length == MAX_LENGTH + 1 | |
name += '$' unless name[-1] == '$' | |
elsif name.split.length < MIN_WORDS and name[-1] == '$' | |
name[-1] = ' ' | |
elsif name.split.length == MAX_WORDS and name[-1] == ' ' | |
name[-1] = '$' | |
end | |
end | |
puts name[1...-1].split.map(&:capitalize).join(' ') | |
__END__ | |
Moon Rockefeller | |
Renata Pasha | |
Grant Gin | |
Echo Pinkett | |
Nakesha Hey | |
Onita Troxel | |
Alesha Macias | |
Marlene Marlin | |
Dana Olds | |
Roosevelt Standish | |
Ursula Bast | |
Mallie Holdaway | |
Quinton Marceau | |
Mariana Daye | |
King Lewis | |
Kesha Edmonson | |
Magen Criger | |
Ricarda Woelfel | |
Adalberto Cressey | |
Gidget Restrepo | |
Mistie Penaflor | |
Serafina Neidert | |
Altha Doggett | |
Ashly Iman | |
Christiane Sae | |
Madalene Costigan | |
Gabrielle Linen | |
Milda Thornsberry | |
Viola Sessions | |
Laureen Pagano | |
Pearly Nunnery | |
Obdulia Seman | |
Rolando Soderman | |
Cassaundra Rowser | |
Jimmy Hazelton | |
Dusty Newbill | |
Cliff Reel | |
Emma Tessman | |
Gwenda Hadnott | |
Gregorio Eller | |
Kevin Eargle | |
Hye Hardee | |
Iliana Betterton | |
Carmelita Mccallion | |
Martine Cashen | |
Clorinda Hajek | |
Vennie Moffatt | |
Armida Goudy | |
Mckenzie Shake | |
Lois Lacey |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment