Created
January 21, 2022 06:03
-
-
Save eileen-code4fun/2c26f69e8344e4c44d2e75e9f0a6f001 to your computer and use it in GitHub Desktop.
Translation Preprocessing
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
def standardize(text): | |
# Split accecented characters. | |
text = tf_text.normalize_utf8(text, 'NFKD') | |
text = tf.strings.lower(text) | |
# Keep space, a to z, and select punctuation. | |
text = tf.strings.regex_replace(text, '[^ a-z.?!,¿]', '') | |
# Add spaces around punctuation. | |
text = tf.strings.regex_replace(text, '[.?!,¿]', r' \0 ') | |
# Strip whitespace. | |
text = tf.strings.strip(text) | |
text = tf.strings.join(['[START]', text, '[END]'], separator=' ') | |
return text | |
eng_text_processor = tf.keras.layers.TextVectorization(standardize=standardize, max_tokens=5000) | |
spa_text_processor = tf.keras.layers.TextVectorization(standardize=standardize, max_tokens=5000) | |
eng_text_processor.adapt(eng_dataset.batch(128)) | |
spa_text_processor.adapt(spa_dataset.batch(128)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment