This repository demonstrates how to perform sentiment analysis using the TextBlob library for both web articles and text files. Sentiment analysis is a technique that quantifies the sentiment or emotional tone in a piece of text, allowing you to determine whether the text expresses positive, negative, or neutral sentiment.
Before using this code, make sure you have the following Python packages installed:
textblob
newspaper3k
(ornewspaper
if former didnt work)nltk
You can install them using pip
via command prompt:
pip install textblob newspaper3k nltk
To analyze the sentiment of a web article, follow these steps:
-
Clone this repository or download the
article-analysis.py
script. -
Edit the
url
variable in the script to specify the URL of the web article you want to analyze. For example:url = "https://www.example.com/article"
-
Run the
article-analysis.py
script using Python. It will download and parse the article, perform natural language processing (NLP), extract a summary, and calculate the sentiment polarity. The sentiment polarity value will be printed to the console on a scale from -1 (negative) to 1 (positive).
To analyze the sentiment of a text file, follow these steps:
-
Clone this repository or download the
text-analysis.py
script. -
Create a text file (e.g.,
sample-text.txt
) containing the text you want to analyze. Make sure the file is in the same directory as the script. -
Run the
text-analysis.py
script using Python. It will read the text from the file, create a TextBlob object, and calculate the sentiment polarity. The sentiment polarity value will be printed to the console on a scale from -1 (negative) to 1 (positive).
- For the article analysis, you will see a sentiment polarity value printed to the console, indicating the overall sentiment expressed in the article.
- For the text file analysis, you will see a sentiment polarity value printed to the console, indicating the sentiment of the text in the file.
Feel free to use these scripts and adapt them to analyze sentiment in your own text data.
For more information on the TextBlob, Newspaper, and NLTK libraries, please refer to their official documentation and resources.
- Various tools regarding Sentiment Analysis
- A google collab-research website via .ipynb file where states an overview about sentiment analysis tools, emphasizes in choosing which sentiment analysis tool for a specific use case.
- Sentiment Analysis in Python
- Web article which contains several libraries
- Simple Sentiment Text Analysis in Python
- Basis for the tutorial
- ChatGPT
- For advanced AI-assisted troubleshooting/debugging if out of my knowledge range
- Natural Language ToolKit (NLTK)
- NLTK is a Python library for text analysis and processing.
- TextBlob
- TextBlob is a simple Python library for text analysis, including sentiment analysis.
- Newspaper/Newspaper3k
- Newspaper is a Python library for web article extraction and analysis.