FINAL CODE SENTIMENT ENGLISH

PHOTO EMBED

Sat Jun 01 2024 11:59:47 GMT+0000 (Coordinated Universal Time)

Saved by @madgakantara

from pymongo import MongoClient, errors
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import statistics
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import string

mongo_uri = "mongodb://localhost:27017/"
database_name = "twitter_database"
collection_name = "tweet_cleaned"

client = MongoClient(mongo_uri, serverSelectionTimeoutMS=5000)
db = client[database_name]
collection = db[collection_name]
 
def analyze_sentiment(text):
    """
    Analyzes the sentiment of the given text using VADER.
    Returns the compound sentiment score.
    """
    analyzer = SentimentIntensityAnalyzer()
    sentiment = analyzer.polarity_scores(text)
    compound_score = sentiment['compound']
    return compound_score

compound_scores = []
cursor = collection.find()
positive_count = 0
neutral_count = 0
negative_count = 0


for doc in cursor:
    lang = doc.get("json_data.lang")
    user_id = doc.get("json_data.user.id")
    user_mention = doc.get("json_data.entities.user_mentions.id")
    tweet_text = doc.get('json_data.text')
    full_text = doc.get('json_data.extended_tweet.full_text')
    
    if lang == 'en' :
        if full_text:
            if (user_id == 56377143) or (user_mention == 56377143) or ('KLM' in full_text) or ('klm' in full_text):
                sentiment = analyze_sentiment(full_text)
                compound_scores.append(sentiment)
                if sentiment >= 0.05:
                    positive_count += 1
                elif sentiment <= -0.05:
                    negative_count += 1
                else:
                    neutral_count += 1
        elif tweet_text:
            if (user_id == 56377143) or (user_mention == 56377143) or ('KLM' in tweet_text) or ('klm' in tweet_text):
                sentiment = analyze_sentiment(tweet_text)
                compound_scores.append(sentiment)
                if sentiment >= 0.05:
                    positive_count += 1
                elif sentiment <= -0.05:
                    negative_count += 1
                else:
                    neutral_count += 1
mean_score = statistics.mean(compound_scores)
print("Positive:", positive_count)
print("Neutral:", neutral_count)
print("Negative:", negative_count)
print("Mean compound score:", mean_score)


client.close()
content_copyCOPY