import keras
from gensim.models import KeyedVectors
!wget -P /root/input/ -c "https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz"
EMBEDDING_FILE = '/root/input/GoogleNews-vectors-negative300.bin.gz' # from above
word2vec = KeyedVectors.load_word2vec_format(EMBEDDING_FILE, binary=True)
result = word2vec.most_similar_cosmul(positive=['actor','woman'], negative=['man'] )
result[0]
"""
# Part 2
"""
from keras.datasets import imdb
from keras.preprocessing import sequence

max_features = 10000  # number of words to consider as features
maxlen = 500  # cut texts after this number of words (among top max_features most common words)
batch_size = 32

print('Loading data...')
(input_train, y_train), (input_test, y_test) = imdb.load_data(num_words=max_features)
print(len(input_train), 'train sequences')
print(len(input_test), 'test sequences')

print('Pad sequences (samples x time)')
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)
from keras.layers import Dense, Bidirectional, SimpleRNN, Embedding, Dropout, LSTM
from keras.models import Sequential
from keras.callbacks import EarlyStopping
es=EarlyStopping(patience=4,min_delta=0.001, mode='max', monitor='val_acc')
import matplotlib.pyplot as plt
def overfit(history):
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    epochs = range(len(acc))

    plt.plot(epochs, acc, 'bo', label='Training acc')
    plt.plot(epochs, val_acc, 'b', label='Validation acc')
    plt.title('Training and validation accuracy')
    plt.legend()

    plt.figure()

    plt.plot(epochs, loss, 'bo', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Validation loss')
    plt.title('Training and validation loss')
    plt.legend()

    plt.show()

metrics=[]
model = Sequential()
model.add(Embedding(10000, 32))
model.add(SimpleRNN(32))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(input_train, y_train,
                    epochs=10,
                    batch_size=128,
                    validation_split=0.2,callbacks=[es])




overfit(history)
m=model.evaluate(input_test,y_test)
print(m)
metrics.append({"single RNN": m})
from keras.layers import LSTM

model = Sequential()
model.add(Embedding(max_features, 32))
model.add(LSTM(32))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
history = model.fit(input_train, y_train,
                    epochs=10,
                    batch_size=128,
                    validation_split=0.2,callbacks=[es])
overfit(history)
m=model.evaluate(input_test,y_test)
print(m)
metrics.append({"small LSTM": m})
from keras.layers import Dense, Bidirectional, SimpleRNN, Embedding, Dropout
model = Sequential()
model.add(Embedding(10000, 32))
model.add(Bidirectional(LSTM(64, return_sequences=True)))

model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
model.summary()

history = model.fit(input_train, y_train,
                    epochs=20,
                    batch_size=128,
                    validation_split=0.2,callbacks=[es])

overfit(history)
m=model.evaluate(input_test,y_test)
print(m)
metrics.append({"Bi LSTM": m})

model = Sequential()
model.add(Embedding(10000, 32))
model.add(LSTM(128, return_sequences=True))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
history = model.fit(input_train, y_train,
                    epochs=10,
                    batch_size=128,
                    validation_split=0.2,callbacks=[es])
overfit(history)
m=model.evaluate(input_test,y_test)
print(m)
metrics.append({"large LSTM": m})
plt.title("Models Accuracy")
plt.bar([list(i)[0] for i in metrics], [list(i.values())[0][1] for i in metrics])
import tensorflow
model = Sequential()
model.add(Embedding(10000, 32))
model.add(LSTM(64,dropout=0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=tensorflow.keras.optimizers.Adam(learning_rate=0.00001),
              loss='binary_crossentropy',
              metrics=['acc'])
model.summary()

history = model.fit(input_train, y_train,
                    epochs=80,
                    batch_size=128,
                    validation_split=0.2,callbacks=[es])

overfit(history)
m=model.evaluate(input_test,y_test)
import tensorflow
es=EarlyStopping(patience=3,min_delta=0.001, mode='max', monitor='val_acc')
model = Sequential()

model.add(Embedding(10000,32))

model.add(Bidirectional(LSTM(64,dropout=0.2, return_sequences=False)))
#model.add(Bidirectional(LSTM(32,dropout=0.2, return_sequences=True)))
model.add(Dense(32))
model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=tensorflow.keras.optimizers.Adam(learning_rate=0.0001),
              loss='binary_crossentropy',
              metrics=['acc'])
model.summary()

history = model.fit(input_train, y_train,
                    epochs=40,
                    batch_size=128,
                    validation_split=0.2,callbacks=[es])

overfit(history)
m=model.evaluate(input_test,y_test)
import numpy as np
from gensim.test.utils import common_texts
from gensim.models import Word2Vec
test_sentences = [
  "That movie was absolutely awful",
  "The acting was a bit lacking",
  "The film was creative and surprising",
  "Absolutely fantastic!",
  "This movie is not worth the money",
  "The only positive thing with this movie is the music"
]

text=[]
for i in test_sentences:
    phrase=[]
    for u in i.split(" "):
        phrase.append(u)
    text.append(phrase)
b=tensorflow.keras.datasets.imdb.get_word_index(path="imdb_word_index.json")
    

vecs=[]

for i in text:
    #arr=np.zeros((10))
    arr=[]
    for u in range(len(i)):
        try:
            mm=i[u].lower()
            
            #arr[u]=b[mm]
            arr.append(b[mm])
        except:
            #arr[u]=b['the']
            arr.append(1)
    #vecs.append(arr.astype("int"))
    vecs.append(arr)

vecs=tensorflow.keras.preprocessing.sequence.pad_sequences(vecs, maxlen=10,value=0, padding="post")
print(vecs)
preds=model.predict_classes(vecs)

for i in range(len(test_sentences)):
    
    print(test_sentences[i] + " : "+str(preds[i]))