This is a random word generator I wrote in Python 3 today, I don't know how many Python implemetations of this exist, but this is my first try and it is completely working.
It returns a random word like string that is pronounceable (most likely pronounceable), I used some English letter frequency statistics data to ensure the letter frequencies in the result would follow English letter distribution patterns, and shuffled that data to make the result truly random;
And then I used some English letter following rules (what letters can follow a letter) to make the result pronounceable.
Beware this script is feature incomplete, new features will be added tomorrow, but it is now completely stable and achieved like 70% of the intended ends, anyway here is the code:
import random
from collections import Counter
SAMPLE = Counter({
'e': 1202,
't': 910,
'a': 812,
'o': 768,
'i': 731,
'n': 695,
's': 628,
'r': 602,
'h': 592,
'd': 432,
'l': 398,
'u': 288,
'c': 271,
'm': 261,
'f': 230,
'y': 211,
'w': 209,
'g': 203,
'p': 182,
'b': 149,
'v': 111,
'k': 69,
'x': 17,
'q': 11,
'j': 10,
'z': 8
})
pool = list(SAMPLE.elements())
randpool = []
while len(pool) > 0:
elem = random.choice(pool)
randpool.append(elem)
pool.remove(elem)
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
tails = {
'a': LETTERS,
'b': 'aeioubjlry',
'c': 'aeiouchjklry',
'd': 'aeioudgjwy',
'e': LETTERS,
'f': 'aeioufjlry',
'g': 'aeioughjlrwy',
'h': 'aeiouy',
'i': LETTERS,
'j': 'aeiouy',
'k': 'aeiouhklrvwy',
'l': 'aeioulvwy',
'm': 'aeioucmy',
'n': 'aeiougny',
'o': LETTERS,
'p': 'aeioufhlprsty',
'q': 'aeiou',
'r': 'aeiouhrwy',
's': 'aeiouchjklmnpqrstvwy',
't': 'aeiouhjrstwy',
'u': LETTERS,
'v': 'aeioulvy',
'w': 'aeiouhry',
'x': 'aeiouhy',
'y': 'aeiousvwy',
'z': 'aeiouhlmvwy'
}
def randomword():
count = random.randint(1, 6)
heads = [random.choice(randpool) for i in range(count)]
i = 0
segments = []
while count > 0:
h = heads[i]
w = h
while True:
r = random.choice(randpool)
if r in tails[h]:
if i == 0 and r == h: continue
else: break
w += r
while True:
f = r
r = random.choice(randpool)
if r in tails[f]:
w += r
if random.randint(0, 9999) % 2 == 0:
segments.append(w)
count -= 1
break
i += 1
return ''.join(segments)
if __name__ == '__main__':
print(randomword())
Please share your thoughts on my script, about how it can be improved, about what new rules I need to add to make the result resemble real words more.
I currently am thinking about making consecutive vowels less than three and no more than three consecutive consonants without a vowel, I am able to implement these, but they are all I can think of now.
Please share your thoughts on possible improvements, any help is welcome!