-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamegen
executable file
·62 lines (46 loc) · 1.39 KB
/
namegen
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
#!/usr/bin/env python
import json
import os.path
import random
import sys
from os.path import expanduser
SETTINGSDIR = '.namelangs'
# algorithm borrowed from
# https://towardsdatascience.com/generating-startup-names-with-markov-chains-2a33030a4ac0
def select_random_item(items):
rnd = random.random() * sum(items.values())
for item in items:
rnd -= items[item]
if rnd < 0:
return item
def generate(chain):
tuple = select_random_item(chain['_initial'])
result = [tuple]
while True:
tuple = select_random_item(chain[tuple])
last_character = tuple[-1]
if last_character == '.':
break
result.append(last_character)
generated = ''.join(result)
if generated not in chain['_names']:
return generated
else:
return generate(chain)
if __name__ == '__main__':
if len(sys.argv) < 2:
print ("Syntax: buildlang [name] [n=10]")
sys.exit(1)
langname = sys.argv[1]
n = 10
if len(sys.argv) > 2:
n = int(sys.argv[2])
chainfile = os.path.join(expanduser('~'), SETTINGSDIR, langname)
if not os.path.exists(chainfile):
print ("Language %s does not exist." % langname)
sys.exit(1)
with open(chainfile) as f:
chain = json.loads(f.read())
for i in range(0, n):
name = generate(chain)
print (name)