-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
52 lines (40 loc) · 1.26 KB
/
generate.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pickle
from namegen import markov
NAMELIST = 'names.txt'
def main():
order = input('Input the order of Markov chain (1 <= order <= 5, default is 2):')
try:
order = int(order)
if not 1 <= order <= 5:
raise ValueError
except ValueError:
order = 2
length = input('Input the max length of each name (default is 6):')
try:
length = int(length)
except ValueError:
length = 6
initial = input('Input the letter(s) that the name start with (length not exceed the order, default is random):')
num = input('Input the number of names to generate (1 <= n <= 100, default is 10):')
try:
num = int(num)
if not 1 <= num <= 100:
raise ValueError
except ValueError:
num = 10
filename = 'chain_%d' % order
try:
with open(filename, 'rb') as f:
chain = pickle.load(f)
print('Read from file successfully.')
except FileNotFoundError:
chain = markov.CharChain(order)
chain.train(NAMELIST)
with open(filename, 'wb') as f:
pickle.dump(chain, f)
for i in range(num):
print(chain.generate(length, initial))
if __name__ == '__main__':
main()