-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_utils.py
381 lines (294 loc) · 9.26 KB
/
text_utils.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import numpy as np
from copy import deepcopy
from tqdm import tqdm
import os
import emoji
import pandas as pd
class CharByCharSynhthetizer():
""" Synthetize text (char-by-char) from a trained RNN using a one-hot encoder."""
def __init__(self, rnn, char_init, encode_lambda, onehot_encoder, decode_lambda, ts, n_step, path_out):
self.rnn = rnn
self.char_init = char_init
self.encode_lambda = encode_lambda
self.onehot_encoder = onehot_encoder
self.decode_lambda = decode_lambda
self.ts = ts
self.n_step = n_step
self.path_out = path_out
try:
os.remove(path_out)
except OSError:
pass
self.idx_init = encode_lambda([char_init])[0]
self.onehot_init = onehot_encoder(onehot_encoder(np.array([self.idx_init]).T, encode=True))
def sample(self, lenght, p):
""" Weighted sampling of next character based on RNN predicitons."""
# select character from softmax weighted dist over all chars
return np.random.choice(range(lenght), size=1, replace=True, p=p.flatten())
def __call__(self, step):
if step % self.n_step == 0:
assert self.onehot_init.shape == (1, self.rnn.out_dim)
x = deepcopy(self.onehot_init)
sequence = []
for t in range(self.ts):
p = self.rnn.forward(x)
x_idx = self.sample(lenght=self.rnn.out_dim, p=p)
x_char = self.decode_lambda(x_idx)[0]
sequence.append(x_char)
x = self.onehot_encoder(np.array([x_idx]).T, encode=True)
sequence_str = "".join(sequence)
with open(self.path_out, "a") as f:
f.write(f"step={step}\n\n")
f.write(sequence_str)
f.write("\n\n")
tqdm.write(f"step={step}\n\n")
tqdm.write(sequence_str)
tqdm.write("\n\n")
else:
pass
class OneHotEncoder():
""" One-hot encoder class.
Attributes
----------
length : int
The length of the one-hot encoding.
Methods
-------
__init__(layers)
Constuctor.
__call__(x, encode=True)
Encode a sequence of integers into a one-hot encoded vectors,
or decode a sequence of one-hot encoded vectors into a
sequence of integers.
__repr__()
Returns the string representation of class.
"""
def __init__(self, length):
""" Constructor.
Parameters
----------
length : int
The length of the one-hot encoding.
Notes
-----
None
"""
# length of one-hot encoding
self.length = length
def __call__(self, x, encode=True):
""" Encode a sequence of integers into a one-hot encoded vectors,
or decode a sequence of one-hot encoded vectors into a
sequence of integers..
Parameters
----------
x : np.ndarray
The sequence of index representation of chars, of shape (n_chars,)
Returns
-------
e or d: np.ndarray
The sequence of one-hot encoded vectors of chars, of shape (n_chars, length)
Notes
-----
None
"""
if encode:
e = np.zeros((x.shape[0], self.length))
e[np.arange(x.shape[0]), x] = 1
return e.astype(int)
else:
d = np.argwhere(x == 1)[:, 1]
return d.astype(int)
def __repr__(self, ):
""" Returns the string representation of class.
Parameters
----------
Returns
-------
repr_str : str
The string representation of the class.
Notes
-----
None
"""
repr_str = "one-hot encoder"
return repr_str
def unique_characters(data):
""" Get the list of unique characters in a data.
Parameters
----------
data : list
A list of strings. The strings may be of different lenghts.
Returns
-------
np.ndarray
The list of unique characters in all of the strings in data.
Notes
-----
None
"""
chars = []
for text in data:
chars_current = list(dict.fromkeys(text))
chars = list(dict.fromkeys(chars + chars_current))
return np.array(chars)
def char_to_idx(char, chars):
""" Convert a char to an index from the encoder np array.
Parameters
----------
char : str
A char.
chars : np.ndarray
All chars.
Returns
-------
np.ndarray
The index repre of char, of shape (,).
Notes
-----
None
"""
return np.argwhere(char == chars).flatten()[0]
def idx_to_char(idx, chars):
""" Convert an index to char in the encoder np array.
Parameters
----------
idx : int
The index repr of a char.
chars : np.ndarray
All chars.
Returns
-------
str
The char.
Notes
-----
None
"""
return chars[idx]
def encode(decoding, chars):
""" Encode a sequence of chars into a sequence of indices based on the encoder.
Parameters
----------
decoding : np.ndarray
The sequence of chars, of shape (n_chars,)
chars : np.ndarray
All chars.
Returns
-------
encoding : np.ndarray
The sequence of index representation of the chars, of shape (n_chars,)
Notes
-----
None
"""
encoding = []
for d in decoding:
encoding.append(char_to_idx(d, chars))
encoding = np.array(encoding)
return encoding
def decode(encoding, chars):
""" Decode a sequence of indices into a sequence of chars based on the encoder.
Parameters
----------
encoding : np.ndarray
The sequence of index representation of the chars, of shape (n_chars,)
chars : np.ndarray
All chars.
Returns
-------
decoding : np.ndarray
The sequence of chars, of shape (n_chars,)
Notes
-----
None
"""
decoding = []
for e in encoding:
decoding.append(idx_to_char(e, chars))
decoding = np.array(decoding)
return decoding
def make_decoded_dataset(dataset):
""" Decode a dataset of strings into a list of characters.
Parameters
----------
dataset : list
A list of strings (contexts) maybe of varying size.
Returns
-------
decoded_dataset : list
A list of lists (contexts) where a context is a list of characters.
Notes
-----
None
"""
decoded_dataset = []
for context in dataset:
context_elements = list(context)
decoded_dataset.append(context_elements)
return decoded_dataset
def make_encoded_dataset(decoded_dataset, chars):
""" Encode a dataset of list of charcters into a list of integers.
Parameters
----------
decoded_dataset : list
A list of lists (contexts) where a context is a list of characters.
chars : np.ndarray
All chars.
Returns
-------
encoded_dataset : list
A list of lists (contexts) where a context is a list of integers.
An integer corresponds to its index in chars.
Notes
-----
None
"""
encoded_dataset = []
for decoded_context in decoded_dataset:
encoded_context = encode(decoded_context, chars)
encoded_dataset.append(encoded_context)
return encoded_dataset
def make_one_hot_encoded_dataset(encoded_dataset, onehot_encoder):
""" One-hot encode a dataset of list of integers into a list of one-hot encoded vectors.
Parameters
----------
encoded_dataset : list
A list of lists (contexts) where a context is a list of integers.
An integer corresponds to its index in chars.
onehot_encoder : OneHotEncoder
A one-hot encoder initilaized with chars (all unique characters in the dataset).
Returns
-------
onehot_encoded_dataset : list
A list of one-hot encoded vectors (contexts).
The index of 1s in the vectors corresponds to the index of the character in chars.
Notes
-----
None
"""
onehot_encoded_dataset = []
for encoded_context in encoded_dataset:
onehot_encoded_context = onehot_encoder(encoded_context, encode=True)
onehot_encoded_dataset.append(onehot_encoded_context)
return onehot_encoded_dataset
def give_emoji_free_text(text):
"""https://stackoverflow.com/a/50602709"""
return emoji.get_emoji_regexp().sub(r'', text)
def add_eol_to_text(text, eol="."):
return ''.join(list(text) + [eol])
def limit_text_length(df, col_name, max_length=140):
mask = (df[col_name].str.len() <= max_length)
df_filtered = df.loc[mask]
print(f"reduced size from {df.shape[0]} to {df_filtered.shape[0]}\n")
return df_filtered
def synthetize(rnn, eol, chars, onehot_encoder, ts, path_out):
gen_times = 10
for gen_time in range(gen_times):
char_init = eol
encode_lambda = lambda d: encode(d, chars)
decode_lambda = lambda e: decode(e, chars)
# n_step is just a dummy variable here for teh callback to work.
n_step = 1
synthetizer = CharByCharSynhthetizer(rnn, char_init, encode_lambda, onehot_encoder, decode_lambda,
ts, n_step, path_out)
synthetizer(step=n_step)