-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_text.py
More file actions
37 lines (32 loc) · 920 Bytes
/
generate_text.py
File metadata and controls
37 lines (32 loc) · 920 Bytes
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
# File: generate_text.py
import sys
from text_stats import suceedingwords
from text_stats import fileopen
from text_stats import texttowords
import random
def main():
try:
script = sys.argv[0]
filename = sys.argv[1]
cur_word = sys.argv[2]
max_words = sys.argv[3]
message = cur_word
max_words = int(max_words)
inputtext = fileopen(filename)
text_words = texttowords(inputtext)
l = 0
while(l < max_words):
next_words = suceedingwords(cur_word,text_words)
if len(next_words) == 0:
break
else:
choice = random.choices(tuple(next_words.keys()), tuple(next_words.values()), k=1)
message = " ".join((message, str(choice[0])))
l = l+1
print(message)
except IndexError:
print("Please provide an Input text file, current word and length of text to be generated as inputs")
except FileNotFoundError:
print("The file does not Exist")
if __name__ == '__main__':
main()