# -*- coding: utf-8 -*-
"""
Created on Thu Feb 11 13:42:45 2021

@author: ASUS
"""

import pandas as pd
df = pd.read_csv(r'D:\nlp\fake-news-data\train.csv')

df = df.dropna()

X = df.drop('label',axis = 1)
y = df['label']

import tensorflow as tf
from tensorflow.keras.layers import Embedding, Dense, LSTM
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import one_hot

# Vocabulary size
voc_size = 5000

# One Hot Representation
messages = X.copy()
messages.reset_index(inplace = True)

import nltk
import re
from nltk.corpus import stopwords

# Dataset Preprocessing
from nltk.stem import PorterStemmer
ps = PorterStemmer()
corpus = []
for i in range(len(messages)):
    print(i)
    review = re.sub('[^a-zA-Z]',' ',messages['title'][i])
    review = review.lower()
    review = review.split()
    review = [ps.stem(word) for word in review if word not in stopwords.words('english')]
    review = " ".join(review)
    corpus.append(review)
    
onehot_repr = [one_hot(words,voc_size) for words in corpus]
    
sent_len = 20
embedded_doc = pad_sequences(onehot_repr,maxlen = sent_len,padding = 'pre')

# Creating the model
embedding_vector_features = 40
model = Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_len))
model.add(LSTM(100))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer = 'adam',metrics = ['accuracy'])
model.summary()

import numpy as np
X_final = np.array(embedded_doc)
y_final = np.array(y)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_final,y_final,test_size = 0.33,random_state = 42)
model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=10,batch_size=64)

y_pred = model.predict_classes(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test,y_pred)
acc = accuracy_score(y_test,y_pred)

[((9, 5, 9, 52), 'pandas.read_csv', 'pd.read_csv', ({(9, 17, 9, 51): '"""D:\\\\nlp\\\\fake-news-data\\\\train.csv"""'}, {}), "('D:\\\\nlp\\\\fake-news-data\\\\train.csv')", True, 'import pandas as pd\n'), ((35, 5, 35, 20), 'nltk.stem.PorterStemmer', 'PorterStemmer', ({}, {}), '()', False, 'from nltk.stem import PorterStemmer\n'), ((49, 15, 49, 75), 'tensorflow.keras.preprocessing.sequence.pad_sequences', 'pad_sequences', (), '', False, 'from tensorflow.keras.preprocessing.sequence import pad_sequences\n'), ((53, 8, 53, 20), 'tensorflow.keras.models.Sequential', 'Sequential', ({}, {}), '()', False, 'from tensorflow.keras.models import Sequential\n'), ((61, 10, 61, 32), 'numpy.array', 'np.array', ({(61, 19, 61, 31): 'embedded_doc'}, {}), '(embedded_doc)', True, 'import numpy as np\n'), ((62, 10, 62, 21), 'numpy.array', 'np.array', ({(62, 19, 62, 20): 'y'}, {}), '(y)', True, 'import numpy as np\n'), ((65, 35, 65, 103), 'sklearn.model_selection.train_test_split', 'train_test_split', (), '', False, 'from sklearn.model_selection import train_test_split\n'), ((70, 5, 70, 36), 'sklearn.metrics.confusion_matrix', 'confusion_matrix', ({(70, 22, 70, 28): 'y_test', (70, 29, 70, 35): 'y_pred'}, {}), '(y_test, y_pred)', False, 'from sklearn.metrics import confusion_matrix, accuracy_score\n'), ((71, 6, 71, 35), 'sklearn.metrics.accuracy_score', 'accuracy_score', ({(71, 21, 71, 27): 'y_test', (71, 28, 71, 34): 'y_pred'}, {}), '(y_test, y_pred)', False, 'from sklearn.metrics import confusion_matrix, accuracy_score\n'), ((39, 13, 39, 57), 're.sub', 're.sub', ({(39, 20, 39, 31): '"""[^a-zA-Z]"""', (39, 32, 39, 35): '""" """', (39, 36, 39, 56): "messages['title'][i]"}, {}), "('[^a-zA-Z]', ' ', messages['title'][i])", False, 'import re\n'), ((46, 15, 46, 38), 'tensorflow.keras.preprocessing.text.one_hot', 'one_hot', ({(46, 23, 46, 28): 'words', (46, 29, 46, 37): 'voc_size'}, {}), '(words, voc_size)', False, 'from tensorflow.keras.preprocessing.text import one_hot\n'), ((54, 10, 54, 77), 'tensorflow.keras.layers.Embedding', 'Embedding', (), '', False, 'from tensorflow.keras.layers import Embedding, Dense, LSTM\n'), ((55, 10, 55, 19), 'tensorflow.keras.layers.LSTM', 'LSTM', ({(55, 15, 55, 18): '(100)'}, {}), '(100)', False, 'from tensorflow.keras.layers import Embedding, Dense, LSTM\n'), ((56, 10, 56, 39), 'tensorflow.keras.layers.Dense', 'Dense', (), '', False, 'from tensorflow.keras.layers import Embedding, Dense, LSTM\n'), ((42, 62, 42, 88), 'nltk.corpus.stopwords.words', 'stopwords.words', ({(42, 78, 42, 87): '"""english"""'}, {}), "('english')", False, 'from nltk.corpus import stopwords\n')]