Skip to content

Instantly share code, notes, and snippets.

@miou-gh
Last active March 19, 2017 22:20
Show Gist options
  • Save miou-gh/360bf0aac72d313792e1a810141befc1 to your computer and use it in GitHub Desktop.
Save miou-gh/360bf0aac72d313792e1a810141befc1 to your computer and use it in GitHub Desktop.
from random import shuffle
consonants = [
# single consonants. without 'q' since it's often awkward in words
"b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "r", "s", "t", "v", "w", "x", "z",
# possible combinations excluding those which cannot start a word
"pt", "gl", "gr", "ch", "ph", "ps", "sh", "st", "th", "wh",
]
consonantsuf = [
# these consonants are to be avoided in the beginning of words
"ck", "cm", "dr", "ds", "ft", "gh", "gn", "kr", "ks", "ls",
"lt", "lr", "mp", "mt", "ms", "ng", "ns", "rd", "rg", "rs",
"rt", "ss", "ts", "tch",
]
vowels = [
"a", "e", "i", "o", "u", "y", # singular vowels
"ee", "oa", "oo", # vowel combinations your language allows
]
current_sound = 'consonant'
def r_shuffle(collection):
shuffle(collection)
return collection
def generate(length):
current_sound = r_shuffle(['consonant', 'vowel'])[0]
current_word = ''
while len(current_word) < length:
rnd = ''
# random sign from either consonant or vowel list
if current_sound == 'consonant':
rnd = r_shuffle((consonants + consonantsuf))[0] if len(current_word) > 1 else r_shuffle(consonants)[0]
elif current_sound == 'vowel':
rnd = r_shuffle(vowels)[0]
# check if the random sign fits in the word length
if (len(current_word) + len(rnd)) <= length:
current_word += rnd
# alternate the sounds
current_sound = 'consonant' if current_sound == 'vowel' else 'vowel'
return current_word
for i in range(20):
word = generate(6)
print(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment