import pandas as pd
df = pd.read_csv('training.1600000.processed.noemoticon.csv',encoding = 'latin',header=None)

# Obtenir le nombre de lignes et de colonnes
nb_lignes, nb_colonnes = df.shape
print("\nNombre de lignes  :", nb_lignes)
print("Nombre de colonnes : ", nb_colonnes)

print("\nInformations sur les colonnes : ")
infos_colonnes = df.info()

# Afficher les premières statistiques du DataFrame
stats = df.describe()
print("\nStatistiques du DataFrame : ")
print(stats)

# Afficher un extrait du DataFrame
print("\nExtrait du DataFrame : ")
extrait = df.head()
print(extrait)

df['feeling'].unique()

import matplotlib.pyplot as plt
import seaborn as sns

val_count=df["feeling"].value_counts()
plt.figure(figsize= (5,5))
sns.countplot(data = df, x= 'feeling')
plt.title(f'Distribution négatif/positif \n{val_count.to_string(index=False, header=None)}')

from nltk.corpus import stopwords
import re

# Valeur True et False
df["feeling"] = df["feeling"].replace({4: True, 0: False})

# Fonction de nettoyage de la colonne 'tweet'
stop_words = set(stopwords.words('english'))
def nettoyage_tweet(tweet):
    # Retrait des URLs
    tweet = re.sub(r"https?://\S+|www\.\S+"," ", tweet)
    
    # Retrait de l'HTML éventuel
    tweet = re.sub(r"<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});"," ", tweet)
    
    # Retrait de la ponctuation
    tweet = re.sub(r"[^\w\s]", " ", tweet)

    # Retrait des mots avec nombre (comme 'AJA89')
    tweet = re.sub(r"\w*\d\w*", " ", tweet)
        
    # Retrait des valeurs numériques
    tweet = re.sub(r"[0-9]+", " ", tweet)
        
    # Retrait des espaces
    tweet = re.sub(r"\s+", " ", tweet).strip()

    # tout en minuscule
    tweet = tweet.lower()

    tokens = []
    for token in tweet.split():
        if token not in stop_words:
            tokens.append(token)
    return " ".join(tokens) 

df.loc[:, 'tweet'] = df['tweet'].apply(nettoyage_tweet)
df.head()

# Nuages
from wordcloud import WordCloud

plt.figure(figsize = (10,10)) 
wc = WordCloud(max_words = 5000 , width = 1500 , height = 800).generate(" ".join(df[df.feeling == True].tweet))
plt.title("Nuages de mots positifs")
plt.imshow(wc , interpolation = 'bilinear')


plt.figure(figsize = (10,10))
wc = WordCloud(max_words = 5000 , width = 1500 , height = 800).generate(" ".join(df[df.feeling == False].tweet))
plt.title("Nuages de mots négatifs")
plt.imshow(wc , interpolation = 'bilinear')

# Analyse de la Longueur des tweets
df['longueur_texte'] = df['tweet'].apply(len)

plt.figure(figsize=(10, 6))
sns.boxplot(x='feeling', y='longueur_texte', data=df)
plt.title('Analyse de la longueur des textes par sentiment')
plt.xlabel('Sentiment')
plt.ylabel('Longueur du Texte')
plt.show()

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from nltk.tokenize import word_tokenize

# Extraction des caractéristiques
vectorizer = TfidfVectorizer(max_features=5000)
X = vectorizer.fit_transform(df['tweet'])
y = df['feeling']

# Division des données
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Entraînement du modèle
model = MultinomialNB()
model.fit(X_train, y_train)

# Prédiction sur l'ensemble de test
y_pred = model.predict(X_test)

# Évaluation du modèle
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
classification_rep = classification_report(y_test, y_pred)

print(f"Accuracy: {accuracy}")
print("\nConfusion Matrix:\n", conf_matrix)
print("\nClassification Report:\n", classification_rep)

# Prédictions
tweet1 = "Enjoying a beautiful day at the beach with my friends! ***"
tweet2 = "Stuck in traffic again! This commute is so frustrating. 😡🚗"
tweet3 = "Working on some tasks and listening to music. Not much happening today."

tweet1 = nettoyage_tweet(tweet1)
tweet2 = nettoyage_tweet(tweet2)
tweet1 = nettoyage_tweet(tweet3)
print(tweet1)
print(tweet2)
print(tweet3)

vectorized_tweet1 = vectorizer.transform([tweet1])
vectorized_tweet2 = vectorizer.transform([tweet2])
vectorized_tweet3 = vectorizer.transform([tweet3])
print(vectorized_tweet1)
print(vectorized_tweet2)
print(vectorized_tweet3)

prediction1 = model.predict(vectorized_tweet1)[0]
prediction2 = model.predict(vectorized_tweet2)[0]
prediction3 = model.predict(vectorized_tweet3)[0]
print(prediction1)
print(prediction2)
print(prediction3)
