# -*- coding: utf-8 -*-
"""
Created on Tue Sep 14 19:15:54 2021

@author: USER
"""

import json
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

datastore = []

for line in open('/Users/USER/AppData/Local/Programs/Python/Python39/Sarcasm_Headlines_Dataset.json','r'):
    datastore.append(json.loads(line))
    
sentences=[]
labels=[]
urls=[]

for item in datastore:
    sentences.append(item['headline'])
    labels.append(item['is_sarcastic'])
    
vocab_size=1000
embedding_dim=16
max_length=120
trunc_type='post'
padding_type='post'
oov_tok='<OOV>'
training_size=20000

training_sentences=sentences[0:training_size]
testing_sentences=sentences[training_size:]
training_labels=labels[0:training_size]
testing_labels=labels[training_size:]

tokenizer=Tokenizer(num_words=vocab_size,oov_token=oov_tok)
tokenizer.fit_on_texts(training_sentences)

word_index=tokenizer.word_index

training_sequences=tokenizer.texts_to_sequences(training_sentences)
training_padded=pad_sequences(training_sequences,maxlen=max_length,padding=padding_type,truncating=trunc_type) 

testing_sequences=tokenizer.texts_to_sequences(testing_sentences)
testing_padded=pad_sequences(testing_sequences,maxlen=max_length,padding=padding_type,truncating=trunc_type)

model=tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size,embedding_dim,input_length=max_length),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
    tf.keras.layers.Dense(24,activation='relu'),
    tf.keras.layers.Dense(1,activation='sigmoid')
    ])

model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()

training_padded=np.array(training_padded)
training_labels=np.array(training_labels)

testing_padded=np.array(testing_padded)
testing_labels=np.array(testing_labels)

num_epochs=50
history=model.fit(training_padded,training_labels,epochs=num_epochs,validation_data=(testing_padded,testing_labels),verbose=1)

import matplotlib.pyplot as plt

def graphs(history,string):
    plt.plot(history.history[string])
    plt.plot(history.history['val_'+string])
    plt.xlabel('Epochs')
    plt.ylabel(string)
    plt.legend([string,'val_'+string])
    plt.show()
    
graphs(history, 'accuracy')
graphs(history, 'loss') 

model.save('test.h5')
   
   
    
    
    
    