Created
April 28, 2019 11:53
-
-
Save ChunML/76b1e8e8471f9e41536faeeb46ff9095 to your computer and use it in GitHub Desktop.
This file contains 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
def unicode_to_ascii(s): | |
return ''.join( | |
c for c in unicodedata.normalize('NFD', s) | |
if unicodedata.category(c) != 'Mn') | |
def normalize_string(s): | |
s = unicode_to_ascii(s) | |
s = re.sub(r'([!.?])', r' \1', s) | |
s = re.sub(r'[^a-zA-Z.!?]+', r' ', s) | |
s = re.sub(r'\s+', r' ', s) | |
return s | |
raw_data_en, raw_data_fr = list(zip(*raw_data)) | |
raw_data_en, raw_data_fr = list(raw_data_en), list(raw_data_fr) | |
raw_data_en = [normalize_string(data) for data in raw_data_en] | |
raw_data_fr_in = ['<start> ' + normalize_string(data) for data in raw_data_fr] | |
raw_data_fr_out = [normalize_string(data) + ' <end>' for data in raw_data_fr] | |
en_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='') | |
en_tokenizer.fit_on_texts(raw_data_en) | |
data_en = en_tokenizer.texts_to_sequences(raw_data_en) | |
data_en = tf.keras.preprocessing.sequence.pad_sequences(data_en, | |
padding='post') | |
fr_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='') | |
fr_tokenizer.fit_on_texts(raw_data_fr_in) | |
fr_tokenizer.fit_on_texts(raw_data_fr_out) | |
data_fr_in = fr_tokenizer.texts_to_sequences(raw_data_fr_in) | |
data_fr_in = tf.keras.preprocessing.sequence.pad_sequences(data_fr_in, | |
padding='post') | |
data_fr_out = fr_tokenizer.texts_to_sequences(raw_data_fr_out) | |
data_fr_out = tf.keras.preprocessing.sequence.pad_sequences(data_fr_out, | |
padding='post') | |
BATCH_SIZE = 5 | |
dataset = tf.data.Dataset.from_tensor_slices( | |
(data_en, data_fr_in, data_fr_out)) | |
dataset = dataset.shuffle(20).batch(BATCH_SIZE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment