Machine Translation ด้วย Keras
Thushan Ganegedara
Data Scientist and Author
ข้อมูล
en_text : Python list ของประโยค โดยแต่ละประโยคเป็น string ของคำที่คั่นด้วยช่องว่างfr_text: Python list ของประโยค โดยแต่ละประโยคเป็น string ของคำที่คั่นด้วยช่องว่างการแสดงข้อมูลบางส่วนในชุดข้อมูล
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 .
...
การตัดคำ (Tokenization)
"I watched a movie last night, it was okay." จะกลายเป็น[I, watched, a, movie, last, night, it, was, okay]การตัดคำด้วย Keras
from tensorflow.keras.preprocessing.text import Tokenizer
en_tok = Tokenizer()
en_tok = Tokenizer()
en_tok.fit_on_texts(en_text)
word_index ของ Tokenizerid = en_tok.word_index["january"] # => returns 51
w = en_tok.index_word[51] # => returns 'january'
seq = en_tok.texts_to_sequences(['she likes grapefruit , peaches , and lemons .'])
[[26, 70, 27, 73, 7, 74]]
Tokenizer ของ Keras ได้tok = Tokenizer(num_words=50)
คำนอก vocabulary (OOV words)
เช่น
tok.fit_on_texts(["I drank milk"])tok.texts_to_sequences(["I drank water"])water เป็น OOV word และจะถูกละเว้นtok = Tokenizer(num_words=50, oov_token='UNK')
tok.fit_on_texts(["I drank milk"])tok.texts_to_sequences(["I drank water"])water เป็น OOV word และจะถูกแทนที่ด้วย UNKMachine Translation ด้วย Keras