From 52225cc37188d4d37115fc82d28adf66619272a2 Mon Sep 17 00:00:00 2001 From: mutalibcs Date: Tue, 7 Jan 2025 15:01:46 +0600 Subject: [PATCH] updated --- .gitignore | 324 ++++++++++++++++++++++++++--------------------------- LICENSE | 42 +++---- README.md | 200 ++++++++++++++++----------------- 3 files changed, 283 insertions(+), 283 deletions(-) diff --git a/.gitignore b/.gitignore index f9fce7f..eb34cf2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,162 +1,162 @@ -# Byte-compiled / optimized / DLL files -tweet-data.csv -190303020001_190103020041_Twitter Sentiment Analysis.docx -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +# Byte-compiled / optimized / DLL files +tweet-data.csv +190303020001_190103020041_Twitter Sentiment Analysis.docx +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/LICENSE b/LICENSE index 1932ee6..055e410 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2023 Md. Abdul Mutalib - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2023 Md. Abdul Mutalib + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 508ccbe..fbd02bb 100644 --- a/README.md +++ b/README.md @@ -1,100 +1,100 @@ -![GitHub](https://img.shields.io/github/license/mamutalib/Twitter-Sentiment-Analysis) ![GitHub Repo stars](https://img.shields.io/github/stars/mamutalib/Twitter-Sentiment-Analysis) ![GitHub watchers](https://img.shields.io/github/watchers/mamutalib/Twitter-Sentiment-Analysis) -![GitHub forks](https://img.shields.io/github/forks/mamutalib/Twitter-Sentiment-Analysis) ![GitHub repo size](https://img.shields.io/github/repo-size/mamutalib/Twitter-Sentiment-Analysis) - -# Contents -- [Introduction](#introduction) -- [Data Set](#data-collection) -- [Methodology](#methodology) - - [Preproceccing](#data-preprocessing) - - [Feature Extraction](#feature-extraction) - - [Results](#results) - - [Model Evaluation ](#model-evaluation) - -# Introduction - -In this project, we explore sentiment analysis, a powerful tool for understanding people's emotions and opinions in text. Our focus is on Twitter data, where users express their feelings on various topics. The goal is to build a machine learning model that can accurately predict whether a tweet's sentiment is positive or negative. - -To achieve this, we preprocess the tweets, removing unnecessary elements like URLs and usernames, and converting words to their base forms. We then use TF-IDF to create meaningful feature vectors representing the importance of each word. - -We'll compare the performance of different machine learning algorithms, such as Naive Bayes, Support Vector Machines (SVM), and Logistic Regression, to find the best model. By evaluating accuracy, precision, recall, and F1-score, we aim to achieve reliable sentiment analysis results. - -# Data Collection - -We gathered our data from Kaggle, a reliable platform for accessing datasets. The dataset was specifically designed for sentiment analysis, containing a variety of tweets with positive and negative sentiments. Kaggle ensures data quality and relevance, saving us time on data collection and cleaning. - -By using this pre-processed dataset, we could concentrate on model development and analysis without worrying about data complexities or user privacy. It provided a solid starting point for our sentiment analysis project. - -| ![Figure 1 Data Set Sample ](docs/dataset-sample.png) | -|:--:| -| Figure 1 Data Set Sample -| - -# Methodology - -## Data Preprocessing - -During the data preprocessing phase, we took several important steps to ensure the text data's quality and relevance for sentiment analysis. These steps involved carefully cleaning and refining the text to create a more meaningful and informative dataset. Here is the each of these steps: - -1. Removing Stop Words: In this step, we got rid of common and non-informative words like "the," "is," "and," and others that don't carry much sentiment-related meaning. These words often appear frequently in text but do not provide much information to the sentiment analysis process. -1. Removing Special Characters: Special characters and punctuations were eliminated to ensure that the text is as clean and clear as possible. Removing unnecessary characters helps us focus on the essential content and prevents potential confusion during analysis. -1. Removing URLs: Since URLs or web links are not relevant to sentiment analysis, we replaced them with the word "URL." -1. Removing Mentions(User ID): User mentions, such as "@username," were replaced with the word "USER." While mentions are essential for communication on social media, they are not significant in determining sentiment, so removing them helps us focus on the actual content. -1. Removing Hashtags: Hashtags like "#sentimentanalysis" were excluded from the text during preprocessing. While hashtags are vital for categorizing and indexing social media content, they are not relevant to sentiment analysis, and excluding them improves the accuracy of our sentiment predictions. - - -| ![Data set after preprocessing ](docs/preprocessed-data.png) | -|:--:| -| Figure 2 Data set after preprocessing -| - -After preprocessing our data, we created word clouds to visualize the most frequent words in both negative and positive tweets. Word clouds are graphical representations that display the most commonly occurring words in a dataset, with word size indicating frequency. - -**Word Cloud for Negative Sentiments:** - -Negative word like “bad”, “suck”, “sad” etc are shown on the word cloud picture. - -| ![Word Cloud based on Negative Tweet ](docs/word-cloud-negative-tweet.png) | -|:--:| -| -Figure 3 Word Cloud based on Negative Tweet -| - - -**Word Cloud for Positive Sentiments:** - -Positive Words like "love," "happy," "good," and "great" are shown in the word cloud. - -| ![ Word Cloud based on Negative Tweet ](docs/word-cloud-positive-tweet.png) | -|:--:| -| -Figure 4 Word Cloud based on Negative Tweet -| - -## Feature Extraction - -During the feature extraction phase, we use **TfidfVectorizer** to convert preprocessed tweets into numerical values, allowing our sentiment analysis models to understand and predict sentiments accurately. It calculates word importance based on frequency and uniqueness across the dataset. This transformation is crucial for an effective sentiment analysis system. - -## Results - -**SVM:** In SVM model, we have got 90% on training while 81% on testing data set. -**Linear Regression (LR):** In LR model, we got 85 on training while 82 on testing. -**Naive Bayes:** In Naïve Bayes model 82% and 80% for training and testing respectively. - - - -||**Training Accuracy** |**Testing Accuracy** | -| :- | - | - | -|**SVM Model** |90% |81% | -|**Naive Bayes Model** |82% |80% | -|**Logistic Regression** |85% |82% | - -| ![dataset-sample.png](docs/prediction.png) | -|:--:| -| -Figure 5 Prediction on unseen data set. -| - - -## Model Evaluation - -In conclusion, the SVM model achieved the highest accuracy on the training data (90%) but slightly lower accuracy on the testing data (81%), indicating some degree of overfitting. The Linear Regression (LR) model performed well on both training (85%) and testing (82%) data, showing better generalization compared to SVM. The Naïve Bayes model also exhibited reasonable performance, with 82% accuracy on the training data and 80% on the testing data. Overall, the LR model appears to be the most balanced and suitable choice, as it demonstrated competitive accuracy on both training and testing datasets without significant overfitting. +![GitHub](https://img.shields.io/github/license/mamutalib/Twitter-Sentiment-Analysis) ![GitHub Repo stars](https://img.shields.io/github/stars/mamutalib/Twitter-Sentiment-Analysis) ![GitHub watchers](https://img.shields.io/github/watchers/mamutalib/Twitter-Sentiment-Analysis) +![GitHub forks](https://img.shields.io/github/forks/mamutalib/Twitter-Sentiment-Analysis) ![GitHub repo size](https://img.shields.io/github/repo-size/mamutalib/Twitter-Sentiment-Analysis) + +# Contents +- [Introduction](#introduction) +- [Data Set](#data-collection) +- [Methodology](#methodology) + - [Preproceccing](#data-preprocessing) + - [Feature Extraction](#feature-extraction) + - [Results](#results) + - [Model Evaluation ](#model-evaluation) + +# Introduction + +In this project, we explore sentiment analysis, a powerful tool for understanding people's emotions and opinions in text. Our focus is on Twitter data, where users express their feelings on various topics. The goal is to build a machine learning model that can accurately predict whether a tweet's sentiment is positive or negative. + +To achieve this, we preprocess the tweets, removing unnecessary elements like URLs and usernames, and converting words to their base forms. We then use TF-IDF to create meaningful feature vectors representing the importance of each word. + +We'll compare the performance of different machine learning algorithms, such as Naive Bayes, Support Vector Machines (SVM), and Logistic Regression, to find the best model. By evaluating accuracy, precision, recall, and F1-score, we aim to achieve reliable sentiment analysis results. + +# Data Collection + +We gathered our data from Kaggle, a reliable platform for accessing datasets. The dataset was specifically designed for sentiment analysis, containing a variety of tweets with positive and negative sentiments. Kaggle ensures data quality and relevance, saving us time on data collection and cleaning. + +By using this pre-processed dataset, we could concentrate on model development and analysis without worrying about data complexities or user privacy. It provided a solid starting point for our sentiment analysis project. + +| ![Figure 1 Data Set Sample ](docs/dataset-sample.png) | +|:--:| +| Figure 1 Data Set Sample +| + +# Methodology + +## Data Preprocessing + +During the data preprocessing phase, we took several important steps to ensure the text data's quality and relevance for sentiment analysis. These steps involved carefully cleaning and refining the text to create a more meaningful and informative dataset. Here is the each of these steps: + +1. Removing Stop Words: In this step, we got rid of common and non-informative words like "the," "is," "and," and others that don't carry much sentiment-related meaning. These words often appear frequently in text but do not provide much information to the sentiment analysis process. +1. Removing Special Characters: Special characters and punctuations were eliminated to ensure that the text is as clean and clear as possible. Removing unnecessary characters helps us focus on the essential content and prevents potential confusion during analysis. +1. Removing URLs: Since URLs or web links are not relevant to sentiment analysis, we replaced them with the word "URL." +1. Removing Mentions(User ID): User mentions, such as "@username," were replaced with the word "USER." While mentions are essential for communication on social media, they are not significant in determining sentiment, so removing them helps us focus on the actual content. +1. Removing Hashtags: Hashtags like "#sentimentanalysis" were excluded from the text during preprocessing. While hashtags are vital for categorizing and indexing social media content, they are not relevant to sentiment analysis, and excluding them improves the accuracy of our sentiment predictions. + + +| ![Data set after preprocessing ](docs/preprocessed-data.png) | +|:--:| +| Figure 2 Data set after preprocessing +| + +After preprocessing our data, we created word clouds to visualize the most frequent words in both negative and positive tweets. Word clouds are graphical representations that display the most commonly occurring words in a dataset, with word size indicating frequency. + +**Word Cloud for Negative Sentiments:** + +Negative word like “bad”, “suck”, “sad” etc are shown on the word cloud picture. + +| ![Word Cloud based on Negative Tweet ](docs/word-cloud-negative-tweet.png) | +|:--:| +| +Figure 3 Word Cloud based on Negative Tweet +| + + +**Word Cloud for Positive Sentiments:** + +Positive Words like "love," "happy," "good," and "great" are shown in the word cloud. + +| ![ Word Cloud based on Negative Tweet ](docs/word-cloud-positive-tweet.png) | +|:--:| +| +Figure 4 Word Cloud based on Negative Tweet +| + +## Feature Extraction + +During the feature extraction phase, we use **TfidfVectorizer** to convert preprocessed tweets into numerical values, allowing our sentiment analysis models to understand and predict sentiments accurately. It calculates word importance based on frequency and uniqueness across the dataset. This transformation is crucial for an effective sentiment analysis system. + +## Results + +**SVM:** In SVM model, we have got 90% on training while 81% on testing data set. +**Linear Regression (LR):** In LR model, we got 85 on training while 82 on testing. +**Naive Bayes:** In Naïve Bayes model 82% and 80% for training and testing respectively. + + + +||**Training Accuracy** |**Testing Accuracy** | +| :- | - | - | +|**SVM Model** |90% |81% | +|**Naive Bayes Model** |82% |80% | +|**Logistic Regression** |85% |82% | + +| ![dataset-sample.png](docs/prediction.png) | +|:--:| +| +Figure 5 Prediction on unseen data set. +| + + +## Model Evaluation + +In conclusion, the SVM model achieved the highest accuracy on the training data (90%) but slightly lower accuracy on the testing data (81%), indicating some degree of overfitting. The Linear Regression (LR) model performed well on both training (85%) and testing (82%) data, showing better generalization compared to SVM. The Naïve Bayes model also exhibited reasonable performance, with 82% accuracy on the training data and 80% on the testing data. Overall, the LR model appears to be the most balanced and suitable choice, as it demonstrated competitive accuracy on both training and testing datasets without significant overfitting.