-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
markov.py
executable file
·58 lines (46 loc) · 1.08 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
#!/usr/bin/env python
import sys
from random import choice
from sys import argv
script, filename, max_words = argv
def build_dict(words):
d = {}
for i, word in enumerate(words):
try:
first, second = words[i], words[i + 1]
except IndexError:
break
key = first
if key not in d:
d[key] = []
d[key].append(second)
return d
def generate_sentence(d):
word_count = 0
s = [key for key in d.keys() if key[0][0].isupper()]
key = choice(s)
s = []
first = key
s.append(first)
word_count = word_count + 1
while True:
try:
second = choice(d[key])
except KeyError:
break
s.append(second)
word_count = word_count + 1
if word_count >= int(max_words):
break
key = second
first = key
return ' '.join(s)
def main():
fname = sys.argv[1]
with open(fname, "rt") as f:
text = f.read()
words = text.split()
d = build_dict(words)
sent = generate_sentence(d)
print(sent)
main()