-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd-letters.py
More file actions
82 lines (68 loc) · 2.14 KB
/
Copy pathcd-letters.py
File metadata and controls
82 lines (68 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/python
import random
import time
#An exponent used to weight the frequencies. The frequencies of certain letters
#in the result is too high otherwise
exponent = 0.65
#English letter frequencies. Copied from wikipedia
freq = {
'a': 8.167 ** exponent,
'b': 1.492 ** exponent,
'c': 2.782 ** exponent,
'd': 4.253 ** exponent,
'e': 12.702 ** exponent,
'f': 2.228 ** exponent,
'g': 2.015 ** exponent,
'h': 6.094 ** exponent,
'i': 6.966 ** exponent,
'j': 0.153 ** exponent,
'k': 0.772 ** exponent,
'l': 4.025 ** exponent,
'm': 2.406 ** exponent,
'n': 6.749 ** exponent,
'o': 7.507 ** exponent,
'p': 1.929 ** exponent,
'q': 0.095 ** exponent,
'r': 5.987 ** exponent,
's': 6.327 ** exponent,
't': 9.056 ** exponent,
'u': 2.758 ** exponent,
'v': 0.978 ** exponent,
'w': 2.360 ** exponent,
'x': 0.150 ** exponent,
'y': 1.974 ** exponent,
'z': 0.074 ** exponent
}
#Randomly generates n letters from the urn. A dictionary of letter
#frequencies should be passed in so that the letters can be drawn
#in correspondence with their frequency in the english language
# n -- Number of letters to gen
# urn -- List of letters to draw from
# freqs -- List of letter frequencies
# Returns a list of letters
#Does not seed the random number generator
def genLetters( n, urn, freqs ):
outList = []
#Create a dictionary with just the letters
relFreqs = dict( ( x, freqs[x] ) for x in urn )
totalProb = sum( relFreqs.values() )
for i in range( n ):
randNum = random.random() * totalProb
found = False
for key, value in relFreqs.iteritems():
if found == False and randNum < value:
found = True
outList.append( key )
else:
randNum = randNum - value
return outList
random.seed( time.time() )
numVowels = int( raw_input( "How many vowels would you like?" ) )
numCons = 9 - numVowels
if numVowels > 5 or numVowels < 3:
print "Must have 3-5 vowels and 4-6 consonants"
#Exit
else:
gens = genLetters( numVowels, [ 'a', 'e', 'i', 'o', 'u' ], freq )
gens = gens + genLetters( numCons, [ 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' ], freq )
print gens