"""
# Emoji Prediction using Transfer Learning
"""
# !pip install emoji
import emoji
import numpy as np
import pandas as pd
mapping = pd.read_csv("../input/twitter-emoji-prediction/Mapping.csv")
output = pd.read_csv("../input/twitter-emoji-prediction/OutputFormat.csv")
train = pd.read_csv("../input/twitter-emoji-prediction/Train.csv")
test = pd.read_csv("../input/twitter-emoji-prediction/Test.csv")
mapping
mapping = mapping.drop("Unnamed: 0", axis=1)
mapping.head()
mapping = mapping.set_index('number')
mapping.head()
mapping_dict = mapping.to_dict()
mapping_dict = mapping_dict['emoticons']
mapping_dict
train.head()
train.columns
train = train.drop('Unnamed: 0', axis=1)
train.tail()
# changing the labels to emoji
# train = train.replace({"Label": mapping_dict})
train.head()
"""
## Text Preprocessing using Embeddings
"""
X_train = train['TEXT'].values
y_train = train['Label'].values

print(X_train.shape, y_train.shape)
# maxLen = X_train.map(len).max()
# maxLen, type(maxLen)
file = open("../input/glove6b50dtxt/glove.6B.50d.txt", encoding = 'utf8')
file
def intialize_emb_matrix(file):
    embedding_matrix = {}
    for line in file:
        values = line.split()
        word = values[0]
        embedding = np.array(values[1:], dtype='float64')
        embedding_matrix[word] = embedding

    return embedding_matrix 

embedding_matrix = intialize_emb_matrix(file)
embedding_matrix['dance']
embedding_matrix['dance'].shape 
def get_emb_data(data, max_len):
#     max_len = 168
    embedding_data = np.zeros((len(data), max_len, 50))  # from glove6B50d
    
    for idx in range(data.shape[0]):
        words_in_sentence = data[idx].split()
        
        for i in range(len(words_in_sentence)):
            if embedding_matrix.get(words_in_sentence[i].lower()) is not None:
                embedding_data[idx][i] = embedding_matrix[words_in_sentence[i].lower()]
                
    return embedding_data
# representing each word in the sentence acc. to glove embedding [str --> numerical]
X_temb = get_emb_data(X_train, 168)
X_temb
X_temb.shape
# X_temb = X_temb.reshape(-1, 20, 50)
# X_temb.shape
# convert outputs to categorical variables
from keras.utils.np_utils import to_categorical
# converting y_train to one hot vectors so that cross-entropy loss can be used.
y_train = to_categorical(y_train)
y_train
"""
## Model Building
"""
import keras
from keras.models import Sequential
from keras.layers import LSTM, SimpleRNN, Dense, Dropout
model = Sequential()
lm = len(mapping_dict)
lm
model.add(LSTM(units = 256, return_sequences=True, input_shape = (168,50)))
model.add(Dropout(0.3))
model.add(LSTM(units=128))
model.add(Dropout(0.3))
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=64, activation='relu'))
model.add(Dense(units=32, activation='relu'))
model.add(Dense(units=20, activation='relu'))
model.add(Dense(units=20, activation='softmax'))
model.summary()
model.compile(optimizer='adam', loss=keras.losses.categorical_crossentropy, metrics=['acc'])
# model Training
res = model.fit(X_temb, y_train, validation_split=0.2, batch_size=32, epochs=10, verbose=2)
"""
## Using Another dataset for Text to emoji prediction
"""
train_data = pd.read_csv("../input/emojify/train_emoji.csv", header=None)
test_data = pd.read_csv("../input/emojify/test_emoji.csv", header=None)
train_data.head()
test_data.head()
train_data.drop(labels=[2, 3], axis=1, inplace=True)
train_data.tail()
emoji.EMOJI_UNICODE['en']
emoji.emojize(':angry_face:')
emoji_mapping = {
    '0': ':beating_heart:',
    '1': ':baseball:',
    '2': ':beaming_face_with_smiling_eyes:',
    '3': ':angry_face:',
    '4': ':face_savoring_food:'
}
for key, value in emoji_mapping.items():
    emoji_mapping[key] = emoji.emojize(value)
    
print(emoji_mapping)
    
X_train = train_data[0].values
y_train = train_data[1].values
# def intialize_emb_matrix(file):
#     embedding_matrix = {}
#     for line in file:
#         values = line.split()
#         word = values[0]
#         embedding = np.array(values[1:], dtype='float64')
#         embedding_matrix[word] = embedding

#     return embedding_matrix 

file
# we have already created an embedding matrix using the golve vectors
embedding_matrix
X_train = get_emb_data(X_train, 10)
X_train
y_train = to_categorical(y_train)
y_train
model_tte = Sequential()
model_tte.add(LSTM(units=64, input_shape=(10, 50), return_sequences=True))
model_tte.add(Dropout(0.3))
model_tte.add(LSTM(units=32))
model_tte.add(Dropout(0.2))
model_tte.add(Dense(units=10, activation='relu'))
model_tte.add(Dense(units=5, activation='softmax'))
model_tte.summary()
model_tte.compile(optimizer='adam', loss=keras.losses.categorical_crossentropy, metrics=['acc'])
history = model_tte.fit(X_train, y_train, validation_split=0.1, verbose=2, batch_size=32, epochs=100)
model_tte2 = Sequential()
model_tte2.add(LSTM(units=128, input_shape=(10, 50), return_sequences=True))
model_tte2.add(Dropout(0.3))
model_tte2.add(LSTM(units=64))
model_tte2.add(Dropout(0.2))
model_tte2.add(Dense(units=32, activation='relu'))
model_tte2.add(Dense(units=20, activation='relu'))
model_tte2.add(Dense(units=5, activation='relu'))
model_tte2.summary()
model_tte2.compile(optimizer='adam', loss=keras.losses.categorical_crossentropy, metrics=['acc'])
history2 = model_tte2.fit(X_train, y_train, validation_split=0.1, verbose=2, batch_size=32, epochs=100)
model_tte3 = Sequential()
model_tte3.add(LSTM(units=128, input_shape=(10, 50), return_sequences=True))
model_tte3.add(Dropout(0.3))
model_tte3.add(LSTM(units=64))
model_tte3.add(Dropout(0.2))
model_tte3.add(Dense(units=32, activation='relu'))
model_tte3.add(Dense(units=20, activation='relu'))
model_tte3.add(Dense(units=5, activation='softmax'))
model_tte3.summary()
model_tte3.compile(optimizer='adam', loss=keras.losses.categorical_crossentropy, metrics=['acc'])
history3 = model_tte3.fit(X_train, y_train, validation_split=0.1, verbose=2, batch_size=32, epochs=100)
test_data = pd.read_csv("../input/emojify/test_emoji.csv", header=None)
test_data.head()
# preprocessing test data
test_data[0] = test_data[0].apply(lambda x:x[:-1])
X_test = test_data[0].values
y_test = test_data[1].values
X_test = get_emb_data(X_test, 10)
y_test = to_categorical(y_test)
model_tte3.evaluate(X_test, y_test)[1]*100
predicted = model_tte3.predict(X_test)
classes = np.argmax(predicted,axis=1)

classes
for t in range(len(test)):
    print(test_data[0].iloc[t])
    print("Predictions: ", emoji.emojize(emoji_mapping[str(classes[t])]))
    print('Actual: ', emoji.emojize(emoji_mapping[str(test_data[1].iloc[t])]))