-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGen.py
More file actions
355 lines (324 loc) · 14.9 KB
/
TextGen.py
File metadata and controls
355 lines (324 loc) · 14.9 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import random as rd
import numpy as np
import getpass
import time
import random as rd
import numpy as np
def is_punctuation(input):
if input in ",.?;:\()!'" or input == '"':
return 1
return 0
def decapitalize(word):
return ''.join([word[:1].lower(), word[1:]])
def capitalize(word):
return ''.join([word[:1].upper(), word[1:]])
def split_input(filepath):
file = open(filepath, "r")
training_text = file.read()
training_set = training_text.split()
new_set = []
for word in training_set:
if word[0].isupper():
word = decapitalize(word)
if is_punctuation(word[0]) and len(word) > 1:
new_set.append(word[0])
word = word[1 :]
if is_punctuation(word[-1]):
new_set.append(word[0 : -1])
new_set.append(word[-1]) # a se comenta pt generare fara semne de punctuatie
else:
new_set.append(word)
file.close()
return new_set
def unique(words):
result = []
for word in words:
if word not in result:
result.append(word)
result.sort()
return result
def create_seq(words, k=3):
seqs = []
for i in range(len(words) + 1 - k):
seq = words[i]
for j in range(1, k):
if is_punctuation(words[i + j]):
seq = seq + words[i + j]
else:
seq = seq + ' ' + words[i + j]
seqs.append(seq)
return seqs
def create_dict(items):
result = {}
count = 0
for itm in items:
result[itm] = count
count = count + 1
return result
def stochastic(seq_unq, word_unq, seqs, words, k = 3):
result = np.zeros((len(seq_unq), len(word_unq)), dtype=float)
seq_dict = create_dict(seq_unq)
word_dict = create_dict(word_unq)
for i in range(len(seqs) - 1):
seq = seqs[i]
word = words[i + k]
result[seq_dict[seq], word_dict[word]] += 1
return result
def sample_next_word(text_file, word_dict, seq_dict, stoch, k=3):
text_words = split_input(text_file)
result = []
if len(text_words) < k:
raise TypeError
else:
final_seq = create_seq(text_words[len(text_words) - k : len(text_words)], k)
if final_seq != "END~"and final_seq[0] not in seq_dict.keys():
raise KeyError("S-a ajuns la o secventa care nu exista.")
else:
if final_seq == "END~":
raise ValueError
else:
return stoch[seq_dict[final_seq[0]]]
def sample_n_words(text_file, word_dict, seq_dict, stoch, words_unq, n, k):
for i in range(n):
with open(text_file, 'r') as file:
for line in file:
pass
last_line = line
new_line = 0
if len(last_line) > 100:
new_line = 1
with open(text_file, 'a') as file:
file.write("\n")
words = split_input(text_file)
if words[len(words) - 1] == "END~":
break
try:
probs = sample_next_word(text_file, word_dict, seq_dict, stoch, k)
except TypeError:
print("\nTextul de input este prea scurt. Accuracy-ul trebuie sa fie mai mic sau egal cu lungimea șirului.")
return 0
except KeyError:
print("S-a ajuns la o secvență inexistentă în setul de antrenament (imens ghinion)")
error_input = input("Reîncercăm cu o secvență aleatorie? Y/N ")
if error_input == 'N':
with open(text_file, 'a') as file:
file.write('\n\t\t~THE END~\n')
return 0
else:
if error_input == 'Y':
word = '.'
while is_punctuation(word):
random_idx = int(rd.uniform(0, len(words) - 1))
word = words[random_idx]
with open(text_file, 'a') as file:
if new_line:
new_line = 0
file.write(word)
else:
file.write(' ' + word)
except ValueError:
return 0
else:
if array_sum(probs) == 0:
print("S-a ajuns la o ultima secvență din setul de antrenament, iar aceasta este unică (imens ghinion)")
error_input = input("Reîncercăm cu o secvență aleatorie? Y/N ")
if error_input == 'N':
with open(text_file, 'a') as file:
file.write('.\n\t\t~THE END~\n')
return 0
else:
if error_input == 'Y':
word = '.'
while is_punctuation(word):
random_idx = int(rd.uniform(0, len(words) - 1))
word = words[random_idx]
with open(text_file, 'a') as file:
file.write(' >>> ' + word)
else:
if array_sum(probs) > 1:
probs = probs * (1 / array_sum(probs))
corpus = split_input(text_file)
last_word = corpus[-1]
chosen = np.random.choice(range(len(probs)), 1, p=probs)
word = words_unq[chosen[0]]
if last_word in ".!?":
word = capitalize(word)
with open(text_file, 'a') as file:
if is_punctuation(word):
file.write(word)
else:
if new_line:
new_line = 0
file.write(word)
else:
file.write(' ' + word)
def array_sum(array):
sum = 0.0
for i in array:
sum += i
return sum
print("-------------------------------------\n")
print(" Welcome to our C.L.I. TextGenerator\n")
print(" version : 1.0\n")
ok = 0
while ok == 0:
print("--------------------------------------\n\n")
print("Main Menu\n\nOptions:\n1 -> Start\n2 -> Quit")
option = getpass.getpass('')
if option != '1' and option != '2':
while option != '1' and option != '2':
print("The option introduced does not exist\nRestarting", end = '')
for _ in range(4): # va printa 4 puncte
print('.', end = '', flush = True)
time.sleep(1)
print('\n\n\n')
print("--------------------------------------\n")
print("Main Menu\n\nOptions:\n1 -> Start\n2 -> Quit")
option = getpass.getpass('')
if option == '2':
ok = 1
print("Shutting down")
for _ in range(5):
print('.', end = '', flush = True)
time.sleep(1)
print('\n')
if option == '1':
print("--------------------------------------------------------------------")
print("Let's start with choosing the language in which I will operate:")
language = str(input("1. ENGLISH\n2. ROMANIAN\nYour choice : "))
print("Loading..............................................................\n")
if language == '1' or language == "ENGLISH":
print("We have 3 training sets\n\n!! Disclaimer:Every training set determines a different result!!\n!! because of its dimensions/complexity !!\n\nOptions :\n", end = '')
print("Option 1 .................................... Training Set 1 (small size, strong coherence)(type 'S')")
print("Option 2 .................................... Training Set 2 (medium size, medium coherence)(type 'M')")
print("Option 3 .................................... Training Set 3 (large size, low coherence)(type 'L')\n")
Option10 = str(input("Enter your option -> "))
if Option10 == 'L':
with open ('tests/output/outputS.txt', 'w') as file:
file.write('rivers of your blood')
filepath = "tests/training_input/english/large_20k_tinyshakespeare.txt"
output = "tests/output/outputS.txt"
n = int(input("Enter the number of words you want to generate: "))
seq_num = int(input("Enter a number from 1 to 4 for accuracy (higher is better): "))
words = split_input(filepath)
seqs = create_seq(words, seq_num)
print("Working", end = '')
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
seq_dict = create_dict(unique(seqs))
word_dict = create_dict(unique(words))
stoch = stochastic(unique(seqs), unique(words), seqs, words, seq_num)
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
sample_n_words(output, word_dict, seq_dict, stoch, unique(words), n, seq_num)
print("\n\nDone! Check outputS.txt")
INPUT = str(input("Return to main menu ? Y/N\n-> "))
if INPUT == "Y":
ok = 0
else:
ok = 1
print()
sample_n_words(output, word_dict, seq_dict, stoch, unique(words), n, seq_num)
elif Option10 == 'M':
with open ('tests/output/outputS.txt', 'w') as file:
file.write('STORY OF THE')
filepath = "tests/training_input/english/medium_2k_jekyll_and_hyde.txt"
output = "tests/output/outputS.txt"
n = int(input("Enter the number of words you want to generate: "))
seq_num = int(input("Enter a number from 1 to 3 for accuracy (higher is better): "))
words = split_input(filepath)
seqs = create_seq(words, seq_num)
print("Working", end = '')
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
seq_dict = create_dict(unique(seqs))
word_dict = create_dict(unique(words))
stoch = stochastic(unique(seqs), unique(words), seqs, words, seq_num)
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
sample_n_words(output, word_dict, seq_dict, stoch, unique(words), n, seq_num)
print("\n\nDone! Check outputS.txt")
INPUT = str(input("Return to main menu ? Y/N\n-> "))
if INPUT == "Y":
ok = 0
else:
ok = 1
print("\n")
sample_n_words(output, word_dict, seq_dict, stoch, unique(words), n, seq_num)
elif Option10 == 'S':
with open ('tests/output/outputS.txt', 'w') as file:
file.write('Once upon a')
filepath = "tests/training_input/english/small_100_poe.txt"
output = "tests/output/outputS.txt"
n = int(input("Enter the number of words you want to generate: "))
seq_num = int(input("Enter a number from 1 to 3 for accuracy (higher is better): "))
words = split_input(filepath)
seqs = create_seq(words, seq_num)
print("Working", end = '')
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
seq_dict = create_dict(unique(seqs))
word_dict = create_dict(unique(words))
stoch = stochastic(unique(seqs), unique(words), seqs, words, seq_num)
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
sample_n_words(output, word_dict, seq_dict, stoch, unique(words), n, seq_num)
print("\n\nDone! Check outputS.txt")
INPUT = str(input("Return to main menu ? Y/N\n-> "))
if INPUT == "Y":
ok = 0
else:
ok = 1
print("\n")
else:
print("Option does not exist !!!\nShutting down", end = '')
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(1)
print('\n')
ok = 1
elif language == '2' or language == "ROMANIAN":
print("You chose ROMANIAN\n\nROMANIAN generator is still in beta, so be patient and don't expect the best results\n\n")
print("!!---------------------------------------------------------------------------!!\n")
print("Info : Romanian mode basically generates stories for kids containing parts from\nother well-known stories\n")
print("!!---------------------------------------------------------------------------!!\n")
n = int(input("Enter the number of words you want to generate: "))
seq_num = int(input("Enter a number from 1 to 5 for accuracy (higher is better): "))
with open ('tests/output/outputS.txt', 'w') as file:
file.write('A fost odată')
filepath = "tests/training_input/romanian/romanian_data.txt"
output = "tests/output/outputS.txt"
if seq_num > 3:
print("DISCLAIMER: for the chosen accuracy, %d extra words will be generated at the beginning with accuracy 3.\n" % (seq_num - 3))
words_aux = split_input(filepath)
seqs_aux = create_seq(words_aux, 3)
seq_dict_aux = create_dict(unique(seqs_aux))
word_dict_aux = create_dict(unique(words_aux))
stoch_aux = stochastic(unique(seqs_aux), unique(words_aux), seqs_aux, words_aux, 3)
sample_n_words(output, word_dict_aux, seq_dict_aux, stoch_aux, unique(words_aux), seq_num - 3, 3)
words = split_input(filepath)
seqs = create_seq(words, seq_num)
print("Working", end = '')
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
seq_dict = create_dict(unique(seqs))
word_dict = create_dict(unique(words))
stoch = stochastic(unique(seqs), unique(words), seqs, words, seq_num)
for _ in range(3):
print('.', end = '', flush = True)
time.sleep(0.5)
sample_n_words(output, word_dict, seq_dict, stoch, unique(words), n, seq_num)
print("\n\nDone! Check outputS.txt")
INPUT = str(input("Return to main menu ? Y/N\n-> "))
if INPUT == "Y":
ok = 0
else:
ok = 1
print("\n")