import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import tensorflow
from tensorflow.keras.models import Sequential 
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding,Bidirectional,LSTM,Dense,Dropout
from tensorflow.keras.utils import to_categorical
df=pd.read_csv('../input/twitter-and-reddit-sentimental-analysis-dataset/Reddit_Data.csv')
df.head(5)
"""
# Exploratory Data Analysis
"""
dist=list(df.category)
pp=[0,0,0]
for i in dist:
    if i==-1:
        pp[0]+=1
    elif i==0:
        pp[1]+=1
    else:
        pp[2]+=1
print(pp)
        

labels=['Negative','Neutral','Positive']
sns.barplot(x=labels,y=pp)
plt.show()
comment=list(df.clean_comment.astype(str))
sentiment=list(df.category)
reddit_dict=dict(zip(comment,sentiment))
print(list(reddit_dict.items())[:5])
Neg_list=[]
Pos_list=[]
Neutral_list=[]
for i,j in reddit_dict.items():
    if j==-1:
        Neg_list.append(i)
    elif j==0:
        Neutral_list.append(i)
    else:
        Pos_list.append(i)    
print(Neg_list[:2],'\n',Neutral_list[:2],'\n',Pos_list[:2])
pos_len=[]
for i in Pos_list:
    pos_len.append(len(i))
neg_len=[]
for i in Neg_list:
    neg_len.append(len(i))
Neutral_len=[]
for i in Neutral_list:
    Neutral_len.append(len(i))
plt.subplots(figsize=(20,8))
plt.title("Word Length Variation")
plt.plot(Neutral_len[:250],c='b',label='Neutral')
plt.plot(neg_len[:250],c='r',label='Negative')
plt.plot(pos_len[:250],c='g',label='Positive')
plt.legend(loc='upper left')
plt.show()
pos_mean=sum(pos_len)//len(pos_len)
neg_mean=sum(neg_len)//len(neg_len)
neutral_mean=sum(Neutral_len)//len(Neutral_len)
combined_mean=(sum(pos_len)+sum(neg_len)+sum(Neutral_len))//(len(pos_len)+len(neg_len)+len(Neutral_len))
plt.title("Average Word Length")
sns.barplot(x=['Negative','Neutral','Positive','Combined'],y=[neg_mean,neutral_mean,pos_mean,combined_mean])
plt.show()
"""
# Tokeinzer

"""
X=df['clean_comment'].astype('str')
X[:5]
lp=""
for i in X:
    lp+=i+" "
print(lp[:100])
st=lp.split(' ')
dict_len=len(set(st))
dict_len,len(st)
tokenizer=Tokenizer(num_words=dict_len,lower=True,oov_token="OOV")
tokenizer.fit_on_texts(X)
len(tokenizer.word_index)

X_train=tokenizer.texts_to_sequences(X)
X_train_padded=pad_sequences(X_train,maxlen=175,padding='post',truncating='post')
X_train[:2]
"""
# One Hot Encode The Sentiment Values
"""
df['category']=df['category'].replace({-1:2})
mp={0:"Neutral",1:"Positve",2:"Negative"}
Y=df['category'].values
Y_hot=to_categorical(Y)
print(Y_hot[:3])
"""
# The Model
"""
model=Sequential()
model.add(Embedding(dict_len,64,input_length=175))
model.add(Dropout(0.3))
model.add(Bidirectional(LSTM(175,return_sequences=True)))
model.add(Dropout(0.3))
model.add(Bidirectional(LSTM(350,return_sequences=True)))
model.add(Dropout(0.3))
model.add(Bidirectional(LSTM(700)))
model.add(Dense(3,activation='softmax'))
print(model.summary())
model.compile(optimizer='adam',metrics=['accuracy'],loss='categorical_crossentropy')

hist=model.fit(X_train_padded,Y_hot,epochs=5,validation_split=0.2)
plt.plot(hist.history['accuracy'],c='b',label='Training')
plt.plot(hist.history['val_accuracy'],c='r',label='Validation')
plt.legend(loc='lower right')
plt.show()
plt.plot(hist.history['loss'],c='b',label='Training')
plt.plot(hist.history['val_loss'],c='r',label='Validation')
plt.legend(loc='upper right')
plt.show()
"""
# Check For Your Own Data
"""
def predict(s):
    X_tes=[]
    X_tes.append(s)
    X_test=tokenizer.texts_to_sequences(X_tes)
    X_test_padded=pad_sequences(X_test,maxlen=175,padding='post',truncating='post')
    sent=int(model.predict_classes(X_test_padded))
    print("The Predicted Sentiment is ",mp[sent])
pol="The article is good but its not great moreover i would say you have done a decent job"
predict(pol)
lop="You have done a stupid mistake which made you lose all the progress you made"
predict(lop)
com="It aint hard work but its honest work"
predict(com)
ppp="Lets Find out what this is going to be classified as"
predict(ppp)