-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
112 lines (70 loc) · 2.47 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""Generate Markov text from text files."""
from random import choice
import sys
def open_and_read_file(file_path):
"""Take file path as string; return text as string.
Takes a string that is a file path, opens the file, and turns
the file's contents as one string of text.
"""
# your code goes here
f = open(file_path, "r")
text = f.read()
return text
def make_chains(text_string):
"""Take input text as string; return dictionary of Markov chains.
A chain will be a key that consists of a tuple of (word1, word2)
and the value would be a list of the word(s) that follow those two
words in the input text.
For example:
>>> chains = make_chains("hi there mary hi there juanita")
Each bigram (except the last) will be a key in chains:
>>> sorted(chains.keys())
[('hi', 'there'), ('mary', 'hi'), ('there', 'mary')]
Each item in chains is a list of all possible following words:
>>> chains[('hi', 'there')]
['mary', 'juanita']
>>> chains[('there','juanita')]
[None]
"""
chains = {}
text_list = text_string.split()
index = 0
while index < (len(text_list) - 2):
# create a variable to hold the current chain key
chain_key = (text_list[index], text_list[index+1])
# create a variable to hold the dictionary value
new_value = text_list[index+2]
if chain_key not in chains:
chains[chain_key] = []
chains[chain_key].append(new_value)
index = index + 1
# your code goes here
return chains
def get_new_word(key, chains):
"""Returns a new word as a random choice from the key's values. """
values = chains[key]
return choice(values)
def get_new_key(key, word):
"""Returns a new key based on the previous key and word. """
return (key[1], word)
def make_text(chains):
"""Return text from chains."""
key = choice(list(chains.keys()))
words = []
while key in chains:
word = get_new_word(key, chains)
words.append(word)
key = get_new_key(key, word)
return " ".join(words)
#input_path = "gettysburg.txt"
filename = sys.argv[1] # first real argument
# Open the file and turn it into one long string
input_text = open_and_read_file(filename)
# print(input_text)
# print(type(input_text))
# Get a Markov chain
chains = make_chains(input_text)
# print(chains)
# Produce random text
random_text = make_text(chains)
print(random_text)