# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="s28r0_CGIk8C"
import string
import re
from numpy import array, argmax, random, take
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, LSTM, Embedding, Bidirectional, RepeatVector, TimeDistributed
from keras.preprocessing.text import Tokenizer
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
from keras import optimizers
import matplotlib.pyplot as plt
% matplotlib inline
pd.set_option('display.max_colwidth', 200)


# + id="VZP6TMkjKbHQ"
def read_text(filename):
    file = open(filename, mode='rt', encoding='utf-8')
    text = file.read()
    file.close()
    return text


# + id="cXVDHT6BKnUF"
def to_lines(text):
    sents = text.strip().split('\n')
    sents = [i.split('\t') for i in sents]
    return sents


# + id="vJxzpjGlKsk4"
data = read_text("/content/fra.txt")
fra_eng = to_lines(data)
fra_eng = array(fra_eng)

# + id="JRSEnbtMK956"
fra_eng = fra_eng[:50000,:]

# + colab={"base_uri": "https://localhost:8080/"} id="q-vlOTkeLE_4" outputId="66a92e9b-eb38-4006-886a-dc8c6595fedf"
fra_eng

# + id="YPbk_5nSMzJK"
fra_eng[:,0] = [s.translate(str.maketrans('', '', string.punctuation)) for s in fra_eng[:,0]]
fra_eng[:,1] = [s.translate(str.maketrans('', '', string.punctuation)) for s in fra_eng[:,1]]

# + colab={"base_uri": "https://localhost:8080/"} id="aAU0IKbkM6Su" outputId="5bb90be6-9790-4fce-f076-6f2558071723"
fra_eng

# + id="IuEBH8-4NG7d"
for i in range(len(fra_eng)):
    fra_eng[i,0] = fra_eng[i,0].lower()
    
    fra_eng[i,1] = fra_eng[i,1].lower()

# + colab={"base_uri": "https://localhost:8080/"} id="UuhC9o5CNPO7" outputId="23d8d4e3-0f47-4dcb-de2f-38e47dd323a6"
fra_eng

# + id="gB1jaIgZNSKY"
eng_l = []
fra_l = []

for i in fra_eng[:,1]:
    eng_l.append(len(i.split()))

for i in fra_eng[:,0]:
    fra_l.append(len(i.split()))

# + id="MBlG9qSzNhJ3"
length_df = pd.DataFrame({'eng':eng_l, 'fra':fra_l})

# + colab={"base_uri": "https://localhost:8080/", "height": 281} id="m2oyY1XONrKK" outputId="5ba071ae-5a34-4d16-e330-2ffabdf68b07"
length_df.hist(bins = 30)
plt.show()


# + id="Jjs1ouI-ODuc"
def tokenization(lines):
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(lines)
    return tokenizer


# + colab={"base_uri": "https://localhost:8080/"} id="YN7GlgEiOhPj" outputId="c0eabace-2964-4acd-846d-fa09569132f7"
eng_tokenizer = tokenization(fra_eng[:, 1])
eng_vocab_size = len(eng_tokenizer.word_index) + 1

eng_length = 8
print('English Vocabulary Size: %d' % eng_vocab_size)

# + colab={"base_uri": "https://localhost:8080/"} id="tSZBaTQmO3br" outputId="8d03f829-6b0a-4d63-f79d-330b9fc45bab"
fra_tokenizer = tokenization(fra_eng[:, 0])
fra_vocab_size = len(fra_tokenizer.word_index) + 1

fra_length = 8
print('French Vocabulary Size: %d' % fra_vocab_size)


# + id="FtCUksm-PL2Y"
def encode_sequences(tokenizer, length, lines):
    # integer encode sequences
    seq = tokenizer.texts_to_sequences(lines)
    # pad sequences with 0 values
    seq = pad_sequences(seq, maxlen=length, padding='post')
    return seq


# + id="U_oUdQVtPQgp"
from sklearn.model_selection import train_test_split
train, test = train_test_split(fra_eng, test_size=0.2, random_state = 12)

# + id="fDqhF-RxPYCx"
trainX = encode_sequences(fra_tokenizer, fra_length, train[:, 0])
trainY = encode_sequences(eng_tokenizer, eng_length, train[:, 1])

# + id="8t89bIinPw_P"
testX = encode_sequences(fra_tokenizer, fra_length, test[:, 0])
testY = encode_sequences(eng_tokenizer, eng_length, test[:, 1])


# + id="ajPeLddKQAsB"
def build_model(in_vocab, out_vocab, in_timesteps, out_timesteps, units):
    model = Sequential()
    model.add(Embedding(in_vocab, units, input_length=in_timesteps, mask_zero=True))
    model.add(LSTM(units))
    model.add(RepeatVector(out_timesteps))
    model.add(LSTM(units, return_sequences=True))
    model.add(Dense(out_vocab, activation='softmax'))
    return model


# + id="olpw1zEXQHXL"
model = build_model(fra_vocab_size, eng_vocab_size, fra_length, eng_length, 512)
rms = optimizers.RMSprop(lr=0.001)
model.compile(optimizer=rms, loss='sparse_categorical_crossentropy')

# + colab={"base_uri": "https://localhost:8080/"} id="6TDnK23QQSCh" outputId="88082fb3-1f63-4c24-b365-c603e9aad306"
filename = 'model.h1.152'
checkpoint = ModelCheckpoint(filename, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

history = model.fit(trainX, trainY.reshape(trainY.shape[0], trainY.shape[1], 1), 
          epochs=30, batch_size=512, 
          validation_split = 0.2,
          callbacks=[checkpoint], verbose=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="X1s0YcDCQgXU" outputId="ab647726-ea9d-4caa-a819-df9036201646"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['train','validation'])
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="I8dIm7kPQmEw" outputId="aed2798f-5777-4bdb-ccc1-fc52dd4ba07d"
model = load_model('model.h1.152')
preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))


# + id="9Z-dVZenQ4SU"
def get_word(n, tokenizer):
    for word, index in tokenizer.word_index.items():
        if index == n:
            return word
    return None


# + id="qnHGmWUuRDGM"
preds_text = []
for i in preds:
    temp = []
    for j in range(len(i)):
        t = get_word(i[j], eng_tokenizer)
        if j > 0:
            if (t == get_word(i[j-1], eng_tokenizer)) or (t == None):
                temp.append('')
            else:
                temp.append(t)
             
        else:
            if(t == None):
                temp.append('')
            else:
                temp.append(t)            
        
    preds_text.append(' '.join(temp))

# + id="uHi6XArbRLOs"
pred_df = pd.DataFrame({'actual' : test[:,0], 'predicted' : preds_text})

# + id="GXvbDz7JRO0f"
pd.set_option('display.max_colwidth', 200)

# + colab={"base_uri": "https://localhost:8080/", "height": 787} id="JWqfH7RNRTGF" outputId="89a7bb44-b9d7-4da7-f942-6a1ba57f0499"
pred_df.head(25)

# + colab={"base_uri": "https://localhost:8080/", "height": 787} id="s_QaTqoBRVnI" outputId="999e1453-5031-4896-b6c4-a1f145b3d13d"
pred_df.tail(25)

# + colab={"base_uri": "https://localhost:8080/", "height": 787} id="zBsx-bfjRYVN" outputId="b7a48241-5643-4659-eff7-4b94225e2131"
pred_df.tail(25)

# + colab={"base_uri": "https://localhost:8080/", "height": 787} id="5hGIB2RDRaqi" outputId="0a70b3dc-d19b-4377-a726-a9aed88fde68"
pred_df.sample(25)

StarcoderdataJupyterScriptsDedupFiltered