Sentiment for KLM
Fri May 31 2024 16:21:44 GMT+0000 (Coordinated Universal Time)
Saved by @madgakantara
from pymongo import MongoClient, errors from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer # MongoDB connection settings mongo_uri = "mongodb://localhost:27017/" database_name = "twitter_database" source_collection_name = "tweets" def get_text_from_doc(doc): """ Extracts the text from the document. Returns the text if available, otherwise returns None. """ text = doc.get("json_data", {}).get("text") if text: return text extended_text = doc.get("json_data", {}).get("extended_tweet", {}).get("full_text") return extended_text 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'] if compound_score > 0.05: return 'positive' elif compound_score < -0.05: return 'negative' else: return 'neutral' try: client = MongoClient(mongo_uri, serverSelectionTimeoutMS=5000) db = client[database_name] source_collection = db[source_collection_name] # Define query to filter tweets related to KLM (tweets made by KLM or mentioning KLM) query = { "$or": [ {"json_data.user.id": 56377143}, # Tweets made by KLM {"json_data.entities.user_mentions.id": 56377143} # Tweets mentioning KLM ] } # Count variables for sentiment analysis positive_count = 0 neutral_count = 0 negative_count = 0 cursor = source_collection.find(query) for doc in cursor: text = get_text_from_doc(doc) if text: sentiment = analyze_sentiment(text) if sentiment == 'positive': positive_count += 1 elif sentiment == 'neutral': neutral_count += 1 elif sentiment == 'negative': negative_count += 1 print("Number of tweets related to KLM:") print("Positive:", positive_count) print("Neutral:", neutral_count) print("Negative:", negative_count) except errors.ServerSelectionTimeoutError as err: print("Failed to connect to MongoDB server:", err) except errors.PyMongoError as err: print("An error occurred while working with MongoDB:", err) finally: client.close()
Comments