-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
118 lines (76 loc) · 3.01 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
113
114
115
116
117
118
"""Generate Markov text from text files."""
import sys
input_path = sys.argv[1]
from random import choice
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
contents = open(file_path).read()
return contents
input_text = open_and_read_file(input_path)
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]
"""
n= int(input("How many n-grams do you want?"))
words = text_string.split()
chains = {}
n_list=[]
for i in range(0, len(words)-n):
n_list.append(words[i])
key = (n_list[0:n-1])
#key = ([0:n-1]
value= words[i + 2]
if key in chains:
chains[key].append(value)
else:
chains[key] = [value]
#print (chains)
return chains
#print("$$$$$$$$$$$$$$$$$$")
make_chains(open_and_read_file(input_path))
def make_text(chains):
"""Return text from chains."""
words = []#create empty list
random_key = choice(list(chains.keys()))# creating random key from dictionaru
words.extend(list(random_key))#append the random value of random key to word_list
#random_value = choice(list(chains[random_key]))
#random_value = choice(list(chains[random_key]))
#print("***********")
#print(random_key)
#print(random_value)
while random_key in chains:#loop through all random keys and produce value
random_value = choice(chains[random_key])
words.append(random_value)#append the random value of random key to word_list
next_key = (random_key[1], random_value)
random_key = next_key
# if next_key in chains:
# words.append(str(next_key))#append the random key into word_list
# words.append(random_value)#append the random value of random key to word_list
# #print(words)
#print(next_key)
#words.append(str(next_key))#append the random key into word_list
#words.append(random_value)#append the random value of random key to word_list
return " ".join(words)
print(open_and_read_file(input_path))
# Open the file and turn it into one long string
# Get a Markov chain
chains = make_chains(input_text)
# Produce random text
random_text = make_text(chains)
print(random_text)