AI PROJECT CLASS X/XII

UNIT 2: Unlocking your Future in AI
Unit 6: Machine Learning Algorithms
October 1, 2024
AI Project
AI PROJECT CLASS X/XII
October 6, 2024
AI Project

Project 1:

AI-Based Sentiment Analysis of Social Media Posts

Problem Statement
In today’s digital world, millions of social media posts are generated every day, containing various opinions and emotions. Understanding public sentiment can help businesses, governments, and individuals make informed decisions. However, manual analysis of such massive data is impractical. This project focuses on developing a sentiment analysis system that uses machine learning to classify social media posts into positive, negative, or neutral categories.

Users/Stakeholders

  • Businesses: To assess customer opinions and improve products or services.
  • Government Organizations: To gauge public sentiment on policies.
  • Social Media Analysts: To track trends and predict public reactions.

Objectives

  • Develop a sentiment analysis system that can process large datasets of social media posts.
  • Achieve high accuracy in classifying sentiments as positive, negative, or neutral.
  • Provide real-time analysis for newly posted content.

Features

  • Natural Language Processing (NLP): Implements text processing and vectorization techniques.
  • Machine Learning Algorithms: Utilizes models like Naive Bayes, Support Vector Machines (SVM), and Decision Trees.
  • Performance Metrics: Offers precision, recall, F1-score, and accuracy reports.
  • Data Visualization: Graphical insights into sentiment distribution and model performance.

AI Used

  • Supervised Learning Models: SVM, Naive Bayes, Decision Trees.
  • NLP: CountVectorizer and TF-IDF for text analysis.

Dataset

  • Publicly available datasets of social media posts (e.g., from Twitter or Reddit) labeled with sentiment.

Solution
This Python-based system reads social media posts, processes the text data using NLP techniques, and classifies them using machine learning models. The application can be extended to integrate with live social media feeds for real-time sentiment tracking.

Python Code:

Import required libraries

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
import nltk
from nltk.corpus import stopwords

Load dataset (assuming it’s a CSV file with ‘text’ and ‘sentiment’ columns)

df = pd.read_csv(‘social_media_posts.csv’) # Replace with the path to your dataset

Preprocess data

nltk.download(‘stopwords’)
stop_words = set(stopwords.words(‘english’))
df[‘text’] = df[‘text’].apply(lambda x: ‘ ‘.join([word for word in x.split() if word.lower() not in stop_words]))

Split the dataset into training and testing sets

X = df[‘text’]
y = df[‘sentiment’] # Assume ‘sentiment’ is labeled as ‘positive’, ‘negative’, or ‘neutral’
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Vectorize the text data (convert text to numerical data)

vectorizer = CountVectorizer()
X_train_counts = vectorizer.fit_transform(X_train)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

Train the Naive Bayes classifier

clf = MultinomialNB().fit(X_train_tfidf, y_train)

Test the classifier

X_test_counts = vectorizer.transform(X_test)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
y_pred = clf.predict(X_test_tfidf)

Print performance metrics

print(f”Accuracy: {accuracy_score(y_test, y_pred)}”)
print(classification_report(y_test, y_pred))

Steps to run this code:

  1. Replace 'social_media_posts.csv' with the path to your dataset.
  2. Ensure the dataset has two columns: one for the social media text (text) and another for sentiment labels (sentiment).
  3. Install required libraries: pip install pandas scikit-learn nltk.
  4. Run the code to train and test the sentiment analysis model.

ai cbse
ai cbse
This site is dedicated to provide contents, notes, questions bank,blogs,articles and other materials for AI students of CBSE.

Leave a Reply

Your email address will not be published. Required fields are marked *