forked from edooley7/dsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.py
executable file
·62 lines (44 loc) · 1.77 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
#!/usr/bin/env python
# Write a Markov text generator, [markov.py](python/markov.py). Your program should be called from the command line with two arguments: the name of a file containing text to read, and the number of words to generate. For example, if `chains.txt` contains the short story by Frigyes Karinthy, we could run:
# ```bash
# ./markov.py chains.txt 40
# ```
# A possible output would be:
# > show himself once more than the universe and what I often catch myself playing our well-connected game went on. Our friend was absolutely correct: nobody from the group needed this way. We never been as the Earth has the network of eternity.
# There are design choices to make; feel free to experiment and shape the program as you see fit. Jeff Atwood's [Markov and You](http://blog.codinghorror.com/markov-and-you/) is a fun place to get started learning about what you're trying to make.
import random
import sys
def read_file(file):
f = open(file, 'r')
text = f.read().strip('*** ')
return text
def create_word_probs(corpus):
word_probs = {}
word_list = corpus.split()
i = 0
while i < len(word_list) - 1:
if word_list[i] not in word_probs:
word_probs[word_list[i]] = [word_list[i+1]]
i+=1
else:
word_probs[word_list[i]].append(word_list[i+1])
i+=1
return word_probs
def generator(n, word_dict):
start_list = [ x for x in word_dict.keys() if x[0].isupper() ]
start = random.choice(start_list)
i = 0
output = start
while i < n:
next_word = random.choice(word_dict[start])
output += next_word + " "
start = next_word
i+=1
return output
if __name__ == '__main__':
# e.g. markov.py tom_sawyer.txt 20
wordgram = int(sys.argv[2])
fname = sys.argv[1]
corpus = read_file(fname)
probs = create_word_probs(corpus)
print(generator(wordgram, probs))