Last active
May 1, 2018 21:33
-
-
Save DCubix/97313a3f46360b639d66d3f2ff8270c3 to your computer and use it in GitHub Desktop.
Name generation in Python
This file contains hidden or 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
import random | |
from datetime import datetime | |
random.seed(datetime.now()) | |
CONSONANTS = list("bcdfghjklmnpqrstvwyxz") | |
VOWELS = list("aeiou") | |
ALPHABET = CONSONANTS + VOWELS | |
THIRD_LETTER_PROB = 0.15 | |
ACCEPTABLE = [ | |
"am", "ar", "ap", "ay", "ah", | |
"ba", "be", "bl", "bru", | |
"ca", "co", "car", "ce", | |
"da", "de", "di", "do", "du", | |
"ed", "el", "et", "ex", | |
"ga", "go", "gu", | |
"he", "ha", | |
"ian", | |
"ja", "je", "jo", "jay", | |
"ka", "kay", "kl", | |
"la", "le", "li", "lo", "lay", "lan", "lu", | |
"ma", "me", "mo", "may", "mat", "man", | |
"na", "ne", "no", "nu", "nay", "nat", | |
"or", "ow", | |
"pa", "pe", "po", "pu", "pat", | |
"ra", "re", "ri", "ro", | |
"sa", "se", "sir", | |
"ta", "te", "to", "tu", "ty", | |
"va", "ve", "vi", "vo", "vay", "vit" | |
"way", "we", | |
"zak", "zac", "za" | |
] | |
UNACCEPTABLE = [ | |
"qa", "qe", "qi", "qo", "qu", "qy", | |
"aq", "eq", "iq", "oq", "uq", "yq", | |
"aj", "ej", "ij", "oj", "uj", | |
"bl", "iv", "ij", "ik", "cm", "fl", | |
"kl", "ux", "ox", "ix" | |
] | |
ACCEPTABLE_PROB = 0.76 | |
def gen_syllable(): | |
# Pick a random first letter | |
first = random.choice(ALPHABET) | |
# If first is a consonant, second will have to be a vowel | |
if first in CONSONANTS: | |
second = random.choice(VOWELS) | |
else: | |
second = random.choice(CONSONANTS) | |
third = None | |
# Optionally, add a third letter | |
if random.random() <= THIRD_LETTER_PROB: | |
third = random.choice(ALPHABET) | |
final = first + second | |
if third: final += third | |
return final | |
def gen_name(): | |
name = "" | |
sz = random.randint(2, 3) | |
last_si = " " | |
i = 0 | |
while sz > 0: | |
si = gen_syllable() | |
if random.random() <= ACCEPTABLE_PROB: | |
if si not in ACCEPTABLE: | |
si = random.choice(ACCEPTABLE) | |
while last_si[-1] in CONSONANTS and si[0] in CONSONANTS: | |
si = random.choice(ACCEPTABLE) | |
while si in UNACCEPTABLE: | |
si = random.choice(ACCEPTABLE) | |
name += si | |
last_si = si | |
sz -= 1 | |
i += 1 | |
return name.capitalize() | |
names = [] | |
for i in range(20): | |
name = "%s %s" % (gen_name(), gen_name()) | |
names.append(name) | |
names.sort() | |
for name in names: | |
print(name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment