-
Notifications
You must be signed in to change notification settings - Fork 616
/
Copy pathmarkov.py
executable file
·48 lines (42 loc) · 1.33 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
import codecs
import random
MAX_LETTERS = 2000
def readFile(f, mp, k):
seed = ''
mostFreqSeed = seed
mostFreq = 1
for line in f:
for ch in line:
seed, mostFreq, mostFreqSeed = processSeed(seed, mostFreqSeed, mostFreq, ch, k, mp)
return mostFreqSeed
def processSeed(seed, mostFreqSeed, mostFreq, ch, k, mp):
seed += ch
if len(seed) == k+1:
oldSeed = seed[:-1]
mp.setdefault(oldSeed, []).append(ch)
if mostFreq < len(mp[oldSeed]):
mostFreq, mostFreqSeed = len(mp[oldSeed]), oldSeed
seed = seed[1:]
return seed, mostFreq, mostFreqSeed
def generateText(mp, mostFreqSeed):
text, curSeed = mostFreqSeed, mostFreqSeed
while (len(text) < MAX_LETTERS):
ch = random.choice(mp[curSeed])
text, curSeed = text+ch, curSeed+ch
curSeed = curSeed[1:]
return text
def main():
fileName = input("Enter the file name: ") + ".txt"
f = codecs.open(fileName, encoding='utf-8')
k = int(input("Enter the Markov order [1-10]: "))
assert (k >= 1 and k <= 10)
mp = {}
mostFreqSeed = readFile(f, mp, k)
f.close()
text = generateText(mp, mostFreqSeed)
print(text)
result = codecs.open("result.txt", "w", encoding='utf-8')
result.write(text)
result.close()
if __name__ == "__main__":
main()