# Natural Language Processing with RNN ------> Sentiment Analysis on Movie reviews using LSTM(Long-Short Term Memory)

from utils import encode
from tensorflow import keras
import numpy as np

VOCAB_SIZE = 88548
MAXLEN = 250
BATCH_SIZE = 64

(train_data, train_label), (test_data,
                            test_label) = keras.datasets.imdb.load_data(
                                num_words=VOCAB_SIZE)

# Pre-process data
# make data input length the same by padding. fill empty spaces with zeros
train_data = keras.preprocessing.sequence.pad_sequences(train_data, MAXLEN)
test_data = keras.preprocessing.sequence.pad_sequences(test_data, MAXLEN)

# CREATE THE MODEL
# 32 stands for the output dimension of the vectors generated by the embedding layer.
model = keras.Sequential([
    keras.layers.Embedding(VOCAB_SIZE, 32),
    keras.layers.LSTM(32),
    keras.layers.Dense(1, activation="sigmoid")
])

# COMPILE AND TRAIN THE MODEL
model.compile(loss="binary_crossentropy", optimizer="rmsprop", metrics=["acc"])
trained_model = model.fit(train_data,
                          train_label,
                          epochs=1,
                          validation_split=0.2)

# Evaluate the model on the test_data so see how it ll perform
results = model.evaluate(test_data, test_label)


# Make Predictions
def predict(text):
    encoded_text = encode(text, MAXLEN)
    pred = np.zeros((1, 250))
    pred[0] = encoded_text
    result = model.predict(pred)
    print(result)
    if result[0] > 0.5:
        print('Positive Review')
    else:
        print("Negative Review")


feedback_a = "That movie was so awesome! I really loved it and would watch it again because it was amazing"
feedback_b = "That movie sucked. I hated it and wouldn't watch it again. It was one of the worst things I ever watched"

predict(feedback_a)
predict(feedback_b)
