-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov
executable file
·59 lines (53 loc) · 1.46 KB
/
markov
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
#!/usr/bin/env python
import fileinput
import random
import re
from itertools import *
from collections import Counter
def words():
for line in fileinput.input():
words = re.split(" +", line)
for word in words:
yield word
def pairwise(iterable):
a,b = tee(iterable)
next(b, None)
for m in zip_longest(a,b):
yield m
def sample_counter(counter):
total = sum(counter.values())
r = random.randint(1, total)
for value, count in counter.items():
if r <= count:
return value
r -= count
assert(False)
def create_markov1(words):
c = Counter(pairwise(words))
markov = {}
for (a,b) in c:
markov[a] = markov.get(a, {})
markov[a][b] = c[(a,b)]
for a in markov:
markov[a] = Counter(markov[a])
return markov
def next_word(last_word, markov):
return sample_counter(markov[last_word])
def sample_markov(initial, markov, length):
last, next_ = None, initial
while next_ and length:
yield next_
last = next_
next_ = next_word(last, markov)
length -= 1
def main():
markov = create_markov1(words())
initials = Counter({a : sum(markov[a].values()) for a in markov})
length = 100
sequence = []
while len(sequence) < length:
initial = sample_counter(initials)
sequence.extend(sample_markov(initial, markov, length-len(sequence)))
print(' '.join(sequence))
if __name__=='__main__':
main()