-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadnames.py
More file actions
83 lines (70 loc) · 2.13 KB
/
Copy pathreadnames.py
File metadata and controls
83 lines (70 loc) · 2.13 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
83
import random
from collections import Counter
vowels = "aeiou"
def choose(dictionary):
# Credit S.O. users Ned Batchelder and moooeeeep
choices = dictionary.items()
total = sum(w for c, w in choices)
r = random.uniform(0, total)
up_to = 0
for c, w in choices:
if up_to + w >= r:
return c
up_to += w
assert False, "Shouldn't get here"
def split_word(word):
word = word.lower()
out = [[]]
last_vowel_status = False
for char in word:
vowel_status = char in vowels
if last_vowel_status == vowel_status:
out[-1].append(char)
else:
last_vowel_status = vowel_status
out.append([char])
return ["".join(x) for x in out]
class TransitionCounter:
def __init__(self):
self.starts = Counter()
self.transitions = {}
self.ends = {}
def add_pair(self, first, second, is_final=False):
if is_final:
d = self.ends
else:
d = self.transitions
if first not in d:
d[first] = Counter()
d[first][second] += 1
def add_start(self, start):
self.starts[start] += 1
def random_start(self):
return choose(self.starts)
def next_from(self, piece, is_final=False):
if is_final:
d = self.ends
else:
d = self.transitions
if piece not in d:
return None
else:
return choose(d[piece])
if __name__ == "__main__":
tc = TransitionCounter()
with open("good.txt") as f:
for line in f:
pieces = split_word(line.strip())
tc.add_start(pieces[0])
for i in range(len(pieces)-2):
tc.add_pair(pieces[i], pieces[i+1])
if len(pieces) >= 2:
tc.add_pair(pieces[-2], pieces[-1], is_final=True)
for _ in range(10):
out = [tc.random_start()]
for i in range(random.randint(2, 5)):
next = tc.next_from(out[-1])
out.append(next)
out.append(tc.next_from(out[-1], is_final=True))
# prin(out)
# prin("".join(out).capitalize())