Parte 1: Preprocessing dei dati

Traduzione automatica con Keras

Thushan Ganegedara

Data Scientist and Author

Introduzione ai dati

  • Dati

    • en_text: lista Python di frasi; ogni frase è una stringa di parole separate da spazi.
    • fr_text: lista Python di frasi; ogni frase è una stringa di parole separate da spazi.
  • Stampare alcuni dati del dataset

for en_sent, fr_sent in zip(en_text[:3], fr_text[:3]):
  print("English: ", en_sent)
  print("\tFrench: ", fr_sent)
English:  new jersey is sometimes quiet during autumn , and it is snowy in april .
    French:  new jersey est parfois calme pendant l' automne , et il est neigeux en avril .
English:  the united states is usually chilly during july , and it is usually freezing in november .
    French:  les états-unis est généralement froid en juillet , et il gèle habituellement en novembre .
...
Traduzione automatica con Keras

Tokenizzazione di parole

  • Tokenizzazione

    • Processo di suddividere una frase/espressione in singole parole/caratteri
    • Esempio: "I watched a movie last night, it was okay." diventa
    • [I, watched, a, movie, last, night, it, was, okay]
  • Tokenizzazione con Keras

    • Impara una mappa da parola a ID parola usando un corpus.
    • Può convertire una stringa in una sequenza di ID
from tensorflow.keras.preprocessing.text import Tokenizer
en_tok = Tokenizer()
Traduzione automatica con Keras

Addestrare il Tokenizer

  • Fare fit del Tokenizer sui dati
    • Il Tokenizer va addestrato su frasi per imparare la mappa parola→ID.
en_tok = Tokenizer()
en_tok.fit_on_texts(en_text)
  • Ottenere la mappa parola→ID
    • Usa l'attributo word_index del Tokenizer.
id = en_tok.word_index["january"] # => returns 51
  • Ottenere la mappa ID→parola
w = en_tok.index_word[51] # => returns 'january'
Traduzione automatica con Keras

Convertire frasi in sequenze

seq = en_tok.texts_to_sequences(['she likes grapefruit , peaches , and lemons .'])
[[26, 70, 27, 73, 7, 74]]
Traduzione automatica con Keras

Limitare la dimensione del vocabolario

  • Puoi limitare la dimensione del vocabolario in un Tokenizer Keras.
tok = Tokenizer(num_words=50)
  • Parole fuori vocabolario (OOV)

    • Parole rare nel corpus di training (cioè raccolta di testi).
    • Parole non presenti nel training set.
  • Esempio

    • tok.fit_on_texts(["I drank milk"])
    • tok.texts_to_sequences(["I drank water"])
    • La parola water è OOV e verrà ignorata.
Traduzione automatica con Keras

Gestire le parole fuori vocabolario (OOV)

  • Definire un token OOV
tok = Tokenizer(num_words=50, oov_token='UNK')
  • Esempio
    • tok.fit_on_texts(["I drank milk"])
    • tok.texts_to_sequences(["I drank water"])
    • La parola water è OOV e verrà sostituita con UNK.
      • cioè Keras vedrà "I drank UNK"
Traduzione automatica con Keras

Ayo berlatih!

Traduzione automatica con Keras

Preparing Video For Download...