Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Twitter Sentiment Analysis using Python Program
Twitter sentiment analysis allows us to analyze public opinions and emotions from tweets using Python. We'll use the Twitter API to fetch tweets and TextBlob library to analyze their sentiment polarity.
Prerequisites
Before starting, you need ?
- A Twitter account with verified phone number
- Twitter Developer Account for API access
- Python installed on your system
Setting Up Twitter API
Visit the Twitter Developer portal and create a new app. After creating the app, navigate to the "Keys and Tokens" tab to obtain your API credentials ?
# Twitter API credentials consumer_key = 'your_consumer_key' consumer_secret = 'your_consumer_secret' access_token = 'your_access_token' access_token_secret = 'your_access_token_secret'
Installing Dependencies
Install the required Python libraries ?
pip install tweepy textblob
What is TextBlob?
TextBlob is a Python library for processing textual data. It provides a simple API for diving into common natural language processing tasks such as sentiment analysis. The sentiment polarity ranges from -1 (negative) to +1 (positive).
Complete Twitter Sentiment Analysis Script
import tweepy
from textblob import TextBlob
# Twitter API credentials
consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token = 'your_access_token'
access_token_secret = 'your_access_token_secret'
# Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Create API object
api = tweepy.API(auth)
# Search for tweets
search_term = 'Python programming'
public_tweets = api.search_tweets(q=search_term, count=10)
print(f"Analyzing sentiment for tweets about: {search_term}\n")
for tweet in public_tweets:
print(f"Tweet: {tweet.text}")
# Perform sentiment analysis
analysis = TextBlob(tweet.text)
print(f"Polarity: {analysis.sentiment.polarity:.2f}")
print(f"Subjectivity: {analysis.sentiment.subjectivity:.2f}")
# Classify sentiment
if analysis.sentiment.polarity > 0:
sentiment = "Positive"
elif analysis.sentiment.polarity < 0:
sentiment = "Negative"
else:
sentiment = "Neutral"
print(f"Sentiment: {sentiment}")
print("-" * 50)
Understanding Sentiment Metrics
| Metric | Range | Description |
|---|---|---|
| Polarity | -1.0 to +1.0 | Measures positive or negative emotion |
| Subjectivity | 0.0 to 1.0 | Measures opinion vs factual content |
Example Output
Analyzing sentiment for tweets about: Python programming Tweet: Python is an amazing programming language! Love it. Polarity: 0.75 Subjectivity: 0.90 Sentiment: Positive -------------------------------------------------- Tweet: Python programming can be challenging sometimes. Polarity: -0.25 Subjectivity: 0.50 Sentiment: Negative --------------------------------------------------
Enhanced Analysis Function
def analyze_sentiment(text):
"""
Analyze sentiment of given text
Returns sentiment classification and scores
"""
analysis = TextBlob(text)
polarity = analysis.sentiment.polarity
subjectivity = analysis.sentiment.subjectivity
if polarity > 0.1:
sentiment = "Positive"
elif polarity < -0.1:
sentiment = "Negative"
else:
sentiment = "Neutral"
return {
'sentiment': sentiment,
'polarity': polarity,
'subjectivity': subjectivity
}
# Usage example
tweet_text = "I love learning Python programming!"
result = analyze_sentiment(tweet_text)
print(f"Sentiment: {result['sentiment']}")
print(f"Polarity: {result['polarity']:.2f}")
Conclusion
Twitter sentiment analysis using Python helps extract valuable insights from social media data. By combining Tweepy for data collection and TextBlob for sentiment analysis, you can build powerful tools to understand public opinion and emotions expressed in tweets.
