Here's another example using the textblob library to look at NYT headlines.
The textblob library outputs two metrics:
Polarity measures the sentiment's direction on a scale from -1 to 1.
-1 indicates a very negative sentiment.
0 indicates a neutral sentiment.
1 indicates a very positive sentiment.
This score is determined by analyzing the text's adjectives and other sentiment-bearing words. A text with a high number of negatively connotated words will have a negative polarity, while one with many positive words will have a positive polarity. Neutral statements, often factual or lacking strong emotional words, will have a polarity close to 0.
Subjectivity measures how much of the text is based on opinion versus factual information, on a scale from 0 to 1.
0 indicates very objective text (fact-based).
1 indicates very subjective text (opinion-based).
This score is assessed by identifying phrases and expressions that reflect personal opinions or feelings, as opposed to statements of fact. The more a text relies on opinions, the higher its subjectivity score.
When a sentiment analysis returns Polarity=0.0 and Subjectivity=0.0, it suggests that the analyzed text is perceived as neutral and objective. This can happen when the text:
Lacks emotionally charged words or phrases, leading to a neutral polarity.
Contains mostly factual information without explicit opinions, biases, or subjective statements, resulting in a low subjectivity score.
To try this example, you'll need the following:
An API key from the NYT developer site. That's covered here
Install the textblob and requests library in the Terminal on your Raspberry Pi.
$ pip3 install requests textblob
Install the sentiment analysis lexicon:
Using Thonny, create a Python file to download the sentiment lexicon. You only need two lines of code:
import nltk
nltk.download('punkt')
Save the code as something like nyt_lexicon_download.py and run it. It takes only about a second.
Add your API Key to the NYT sentiment analysis example and run the code.
Challenge:
Can you have a visual indicator of some kind change to represent the "polarity" of the day?