-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
132 lines (102 loc) · 3.85 KB
/
Copy pathdictionary.py
File metadata and controls
132 lines (102 loc) · 3.85 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Simulate a Markov Chain Algorithm to generate text based on
preceding words.
"""
import random
def put_tuples_in_dictionary(alist, adict, n):
"""Takes a list of strings and a dictionary and the length that each
string tuple in the dictionary should be.
Parameters: alist is a list of strings.
adict is a dictionary that will have tuples as key values based on alist.
n is the length of each tuple that will be a key in adict from alist.
Returns: a dictionary after having been given key-value pairs."""
for i in range(0, len(alist) - n):
if tuple(alist[i : i + n]) not in adict:
adict[tuple(alist[i : i + n])] = []
adict[tuple(alist[i : i + n])].append(alist[i + n])
return adict
def create_tlist(num_words, new_list, adict, n):
"""Randomly generates text using Markov Chain Analysis where the words
that came before influence the words that will come directly
after it.
Parameters: num_words is the max number of words you want to generate.
new_list is the list that you want to add these words to.
adict is a dictionary of key-value tuple pairs that are words within
new_list.
n is an integer that represents how long each tuple used as a key in adict
is.
Returns: a list that has words added to it."""
i = 0
while len(new_list) < num_words:
markov = tuple(new_list[i : i + n])
if markov not in adict:
break
if markov in adict:
if len(adict[markov]) > 1:
new_list.append(adict[markov]\
[random.randint(0,len(adict[markov])- 1)])
elif len(adict[markov]) == 1:
new_list.append(adict[markov][0])
i += 1
return new_list
def add_NONWORD_to_list(alist, n, NONWORD):
"""Adds NONWORD to alist n times.
Parameters: alist is a list that you want to add to.
n is the number of times you will do so.
NONWORD is a string that you are going to append to alist.
Returns: a list that has NONWORD appended to it n times."""
for i in range(n):
alist.append(NONWORD)
return alist
def get_all_input_lines(file, alist):
"""Takes a file, splits its contents by spaces and adds them to a list.
Parameters: file is a read file object.
alist is a list that you are adding the file contents to.
Returns: a list that has all the file contents added to it."""
alist = []
for line in file:
line = line.split()
alist += line
return alist
def construct_sentence(new_list):
"""Takes a list and constructs a sentence by concatenating the contents
of the list to a string. Every 10 words, the newline marker is added
to signify a new line.
new_list is a list of words.
Returns: a string that is a result of adding the contens of new_list to
an empty string."""
i = 0
astring = ""
final_string = ""
for element in new_list:
if i == 9:
astring += element + "\n"
i = 0
final_string += astring
astring = ""
elif i != 9:
astring += element + " "
i += 1
if astring != "":
final_string += astring.strip()
return final_string.strip("\n")
def main():
SEED = 8
NONWORD = " "
random.seed(SEED)
file = open(input(), "r")
n = int(input())
num_words = int(input())
adict = {}
alist = []
alist2 = []
alist2 = add_NONWORD_to_list(alist2, n, NONWORD)
alist = get_all_input_lines(file, alist)
alist2 += alist
adict = put_tuples_in_dictionary(alist2, adict, n)
new_list = []
[new_list.append(alist[i]) for i in range(n)]
new_list = create_tlist(num_words, new_list, adict, n)
written = construct_sentence(new_list)
print(written)
main()