October 2018
Intermediate to advanced
252 pages
6h 49m
English
class CharTable(object):
def __init__(self, char):
self.char = sorted(set(char))
self.char_indices = dict((ch, i) for i, ch in enumerate(self.char))
self.indices_char = dict((i, ch) for i, ch in enumerate(self.char))
def encode(self, C, num_rows):
x = np.zeros((num_rows, len(self.char)))
for i, ch in enumerate(C):
x[i, self.char_indices[ch]] = 1
return x
def decode(self, x, calc_argmax=True):
if calc_argmax:
x = x.argmax(axis=-1)
return ''.join(self.indices_char[x] for x in x)