Created
July 26, 2017 18:36
-
-
Save ltbringer/5316fb5f2e88db98cf5749bb762f2d63 to your computer and use it in GitHub Desktop.
nlg encoder decoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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