Corrected sentiment testing

PHOTO EMBED

Fri May 31 2024 18:51:38 GMT+0000 (Coordinated Universal Time)

Saved by @madgakantara

from pymongo import MongoClient
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from nltk.corpus import stopwords
import statistics

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)
    return sentiment['compound']

def preprocess_text(text):
    # Remove stopwords
    stop_words = set(stopwords.words('english'))
    tokens = text.split()
    tokens = [token for token in tokens if token.lower() not in stop_words]
    
    return tokens




negative_comments_klm = [
    "I had a terrible experience with KLM. Their customer service was rude and unhelpful.",
    "KLM lost my luggage and didn't offer any compensation or assistance.",
    "Avoid KLM at all costs! They canceled my flight without any notice and left me stranded.",
    "I'll never fly with KLM again. Their planes are old and uncomfortable.",
    "KLM overbooked my flight and bumped me off without any compensation or apology.",
    "KLM delayed my flight multiple times and provided no explanation or updates.",
    "I had a horrible experience with KLM. They were disorganized and incompetent.",
    "KLM's customer service is the worst I've ever encountered. They were unresponsive and dismissive.",
    "KLM's website is a nightmare to use. It's slow, confusing, and full of errors.",
    "I regret choosing KLM for my trip. Their staff was rude and disrespectful.",
    "KLM's flight attendants were unprofessional and uncaring. They ignored my requests and were rude.",
    "I'll never trust KLM again. Their flight schedules are unreliable and constantly changing.",
    "KLM's prices are outrageous for the terrible service they provide. Avoid them if you can.",
    "KLM's planes are dirty and poorly maintained. I felt unsafe throughout the entire flight.",
    "I had a terrible experience with KLM's customer service. They were unhelpful and unsympathetic.",
    "KLM's food and beverage options are awful. I wouldn't feed it to my worst enemy.",
    "I'll never fly KLM again after the nightmare experience I had with them.",
    "KLM's flight attendants were rude and disrespectful. They made me feel unwelcome and uncomfortable.",
    "KLM's cancellation policy is a scam. They charged me outrageous fees to cancel my flight.",
    "I'll be filing a complaint against KLM for the horrendous service I received from them.",
    "KLM's check-in process is a disaster. It took hours to get through, and their staff was unhelpful.",
    "KLM's in-flight entertainment is outdated and limited. There was nothing to do during the flight.",
    "I had a terrible experience with KLM's booking system. It was glitchy and unreliable.",
    "KLM's seats are cramped and uncomfortable. I couldn't sleep a wink on my flight.",
    "I'll never recommend KLM to anyone after the terrible experience I had with them.",
    "KLM's flight attendants were rude and unprofessional. They treated passengers with disrespect.",
    "KLM's flight was a nightmare from start to finish. I'll never fly with them again.",
    "I had a terrible experience with KLM's baggage handling. My luggage was damaged and items were missing.",
    "KLM's customer service representatives were unhelpful and unwilling to assist me with my issue.",
    "I'll be avoiding KLM in the future. Their service was atrocious and their staff were incompetent.",
    "KLM's flight was delayed for hours without any explanation or compensation. It ruined my trip.",
    "I had a horrible experience with KLM's ground staff. They were rude and unhelpful.",
    "KLM's flight was overbooked and they refused to offer any compensation or alternative arrangements.",
    "I'll never fly with KLM again after the terrible experience I had with them.",
    "KLM's flight was cancelled at the last minute and they offered no assistance or compensation.",
    "KLM's customer service was appalling. They were unresponsive and unwilling to help me with my issue.",
    "I had a terrible experience with KLM's flight attendants. They were rude and dismissive.",
    "KLM's flight was delayed multiple times and they provided no information or updates to passengers.",
    "I'll never fly with KLM again. Their service was terrible and their staff were rude.",
    "KLM's flight was overbooked and they refused to compensate me or provide alternative arrangements.",
    "I had a horrible experience with KLM's customer service. They were unhelpful and unresponsive.",
    "KLM's website is a nightmare to navigate. It's slow, confusing, and full of errors.",
    "I'll never trust KLM again after the terrible experience I had with them.",
    "KLM's flight attendants were rude and unprofessional. They made me feel unwelcome and uncomfortable.",
    "KLM's prices are outrageous for the terrible service they provide. Avoid them if you can.",
    "KLM's planes are dirty and poorly maintained. I felt unsafe throughout the entire flight.",
    "I had a terrible experience with KLM's customer service. They were unhelpful and unsympathetic.",
    "KLM's food and beverage options are awful. I wouldn't feed it to my worst enemy.",
    "I'll never fly KLM again after the nightmare experience I had with them.",
    "KLM's flight attendants were rude and disrespectful. They made me feel unwelcome and uncomfortable.",
    "KLM's cancellation policy is a scam. They charged me outrageous fees to cancel my flight.",
    "I'll be filing a complaint against KLM for the horrendous service I received from them.",
    "KLM's check-in process is a disaster. It took hours to get through, and their staff was unhelpful.",
    "KLM's in-flight entertainment is outdated and limited. There was nothing to do during the flight.",
    "I had a terrible experience with KLM's booking system. It was glitchy and",
    "i hate klm"]
list_of_scores = []
for text in negative_comments_klm:
    preprocessed_text = preprocess_text(text)
    processed_text = ' '.join(preprocessed_text)  # Convert preprocessed tokens back to text
    score = analyze_sentiment(processed_text)
    list_of_scores.append(score)

average_score = statistics.mean(list_of_scores)
print("Average sentiment score:", average_score)
content_copyCOPY