This project performs sentiment analysis on a dataset of tweets using a BERT pre-trained model. The analysis classifies text into different emotions, including "Very Sad," "Sad," "Neutral," "Happy," and "Very Happy."
Sentiment analysis is a powerful tool used to determine the emotional tone behind a body of text. In this project, we use a BERT-based model to classify tweets into five emotion categories: "Very Sad," "Sad," "Neutral," "Happy," and "Very Happy."
The dataset used in this project consists of tweets, each labeled with various features such as year, month, day, time, platform, and sentiment. The original sentiment labels (e.g., "1 star" to "5 stars") were converted to emotions for more intuitive analysis.
The dataset was preprocessed by dropping irrelevant columns and applying the sentiment analysis model to the text column to generate emotion labels.
The sentiment analysis model used in this project is based on the BERT architecture, specifically the nlptown/bert-base-multilingual-uncased-sentiment model from Hugging Face. The model predicts a sentiment score that is then mapped to an emotion using a predefined dictionary.
To run this project locally, you'll need to have Python installed along with the necessary libraries. You can install the required dependencies using pip:
pip install pandas transformers-
Clone this repository:
git clone https://github.com/yourusername/sentiment-analysis.git cd sentiment-analysis -
Place your dataset in the project directory and ensure it's named
sentiment_analysis.csv. -
Run the sentiment analysis script:
python sentiment_analysis.py
-
The script will output the dataset with emotion labels and save it to
sentiment_analysis_results.csv.
The output of the analysis is a CSV file where each tweet is associated with an emotion label, providing insights into the overall sentiment of the dataset.
Here’s an example of the dataset before and after applying sentiment analysis:
Before:
| Text | Year | Month | Day | Time of Tweet | Platform | Sentiment |
|---|---|---|---|---|---|---|
| "I love this product!" | 2023 | 01 | 15 | 12:30 PM | 5 stars | |
| "This is the worst experience ever." | 2023 | 01 | 15 | 01:00 PM | 1 star |
After:
| Text | Emotion |
|---|---|
| "I love this product!" | Very Happy |
| "This is the worst experience ever." | Very Sad |
To better understand the sentiment distribution in the dataset, a bar plot is generated to visualize how many tweets fall under each emotion category: "Very Sad," "Sad," "Neutral," "Happy," and "Very Happy."

If you'd like to contribute to this project, please fork the repository and submit a pull request. We welcome any improvements or suggestions!