Group Mini Project

QMP2: Mini Project II Use the following dataset and classify tweets into positive and negative tweets. Dataset: https://www.kaggle.com/ruchi798/data-science-tweets

Tweet Sentiment Classification

Solution and implementation for QMP2 from Data Science Laboratory (ds).

mini_project_tweet.py Download
import kagglehub
import pandas as pd
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

# Download VADER lexicon
nltk.download('vader_lexicon')

# Download dataset
path = kagglehub.dataset_download("ruchi798/data-science-tweets")

# Load dataset
df = pd.read_csv(path + "/tweets/data_science.csv")

# Keep only tweet column
df = df[['tweet']].dropna()

# Initialize analyzer
sia = SentimentIntensityAnalyzer()

# Function to classify sentiment
def classify_sentiment(text):
    score = sia.polarity_scores(text)['compound']
    if score >= 0.05:
        return "Positive"
    elif score <= -0.05:
        return "Negative"
    return "Neutral"

# Take sample tweets
sample_df = df.sample(3)

print("\n--- Sample Tweet Classification ---\n")

# Loop through sample tweets
for i, row in sample_df.iterrows():
    tweet = row['tweet']
    sentiment = classify_sentiment(tweet)
    
    print("Tweet:", tweet + "...")
    print("Sentiment:", sentiment)
    print("-" * 50)

# Input a user tweet for classification
user_tweet = input("Enter a tweet: ")
print("Sentiment:", classify_sentiment(user_tweet))

Other Questions in Data Science Laboratory

See All Available Questions
Download