Skip to content

Instantly share code, notes, and snippets.

@ltbringer
Created July 26, 2017 18:36
Show Gist options
  • Save ltbringer/5316fb5f2e88db98cf5749bb762f2d63 to your computer and use it in GitHub Desktop.
Save ltbringer/5316fb5f2e88db98cf5749bb762f2d63 to your computer and use it in GitHub Desktop.
nlg encoder decoder
import numpy as np
def char_to_vec(str_char):
byte_str = '{0:08b}'.format(ord(str_char), 'b')
return np.array([int(bit) for bit in byte_str])
def sentence_to_vec(sentence):
sentence = sentence.lower()
return np.array([char_to_vec(letter) for letter in sentence])
def vec_to_char(char_vec):
return ''.join([str(v) for v in char_vec])
def decode_to_str(sentence_matrix):
return ''.join([chr(int(vec_to_char(char_vec), 2)) for char_vec in sentence_matrix])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment