-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
60 lines (43 loc) · 1.57 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
#coding: utf-8
from random import choice
class MarkovChain:
def __init__(self, filename, chunksize):
self.filename = filename
self.chunksize = chunksize
self.markov_chain, self.starters = self.create_markov_chain()
# chunksize determines how close to the original the generated text is (the higher the value the closer it is)
def create_markov_chain(self):
with open(self.filename, 'r') as f:
all_data = f.readlines()
d = {}
starters = []
n = self.chunksize
for piece in all_data:
starters.append(piece[0:n])
# iterate through one line and add the chunk to the corresponding dictionary
for i in range(len(piece)-n-1):
chunk = piece[i:(i+n)]
if chunk not in d:
d[chunk] = []
d[chunk].append(piece[i+n])
return(d, starters)
def generate_text(self):
o = ''
end_of_sentence = ['.','!','?']
nex = choice(self.starters)
o = nex+choice(self.markov_chain[nex])
nex = o[(len(o)-self.chunksize):]
while True:
try:
o+=choice(self.markov_chain[nex])
if o[-1] == '\n':
break
nex = o[(len(o)-self.chunksize):]
except:
for e in end_of_sentence:
if e in o:
o = o[:o.index(e)+1]
break
break
return(o)
markov = MarkovChain