-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.py
67 lines (53 loc) · 1.76 KB
/
markov.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class MarkovGenerator:
def __init__(self, data, order=2):
self.iteration = 0
self.order = order
self.data = {}
self.ending = {}
for item in data:
for i in xrange(len(item)-order-1):
key = item[i:i+order]
val = item[i+order]
if self.data.has_key(key):
self.data[key].add(val)
else:
self.data[key] = set([val])
key = item[-2]
val = item[-1]
if self.ending.has_key(key):
self.ending[key].add(val)
else:
self.ending[key] = set([val])
#endfor
for k, v in self.data.items():
self.data[k] = list(v)
for k, v in self.ending.items():
self.ending[k] = list(v)
#end ef
def generate(self, maxLen):
i = self.iteration
out = self.data.keys()[i%len(self.data)]
try:
while len(out) < maxLen - 1:
out += self.data[out[-self.order:]][i%len(self.data[out[-self.order:]])]
#i += 1
except KeyError:
pass
try:
out += self.ending[out[-1]][i%len(self.ending[out[-1]])]
#print '!!', out
except KeyError:
pass
self.iteration += 1
return out
#enddef
#endclass
data = ['aklabeth','brabenec','citus','deges','emanek','filuta','gymnazian',
'hrubec','ilonka','jenicek','kulma','liptakov','manana','natalie',
'otesanek','prdelnik','quido','raskolnikov','satanas','trabant',
'ulicnik','vegetak','wendigo','xaver','ypsylon','zabacek']
gen = MarkovGenerator(data)
print gen.data
print gen.ending
for i in xrange(40):
print gen.generate(8)