Sentiment analysis is a powerful technique that allows us to gauge the emotional tone behind a piece of text. In today's digital age, social media platforms like Twitter have become a treasure trove of opinions and sentiments.
In this tutorial, we will walk you through the process of building and containerising a sentiment analysis tool using Python and Twitter's API. By the end of this article, you'll be able to extract and analyze tweets to determine whether they carry a positive, negative, or neutral sentiment using Docker container.
- Basic knowledge of Python.
- Twitter(X) Developer Account (for API access).
- Docker Desktop
- Python libraries:
- Tweepy (for accessing Twitter's API),
- TextBlob (for sentiment analysis), and
- Matplotlib (for visualization).
- Create a Twitter Developer Account and create a new App to obtain API keys and access tokens.
- Install Tweepy using pip install tweepy.
import tweepy # Fill in your API keys and access tokens consumer_key = 'your_consumer_key' consumer_secret = 'your_consumer_secret' access_token = 'your_access_token' access_token_secret = 'your_access_token_secret' # Authenticate with Twitter's API auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth)
query = "your_search_query" num_tweets = 100 # Number of tweets to retrieve tweets = tweepy.Cursor(api.search, q=query, lang='en').items(num_tweets) tweet_texts = [tweet.text for tweet in tweets]
from textblob import TextBlob positive = 0 negative = 0 neutral = 0 for tweet in tweet_texts: analysis = TextBlob(tweet) if analysis.sentiment.polarity > 0: positive += 1 elif analysis.sentiment.polarity < 0: negative += 1 else: neutral += 1 sentiment_counts = [positive, negative, neutral] labels = ['Positive', 'Negative', 'Neutral']
import matplotlib.pyplot as plt plt.figure(figsize=(8, 6)) plt.bar(labels, sentiment_counts, color=['green', 'red', 'gray']) plt.title('Sentiment Analysis of Tweets') plt.xlabel('Sentiment') plt.ylabel('Number of Tweets') plt.show()
Containerizing the sentiment analysis solution using Docker can make it easier to distribute and deploy. Here's how you can containerize the solution:
Create a file named Dockerfile in your project directory with the following content:
# Use an official Python runtime as a parent image FROM python:3.8-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME SentimentAnalysisApp # Run sentiment analysis script when the container launches CMD ["python", "sentiment_analysis.py"]
Create a file named requirements.txt in the same directory with the following content:
tweepy textblob matplotlib
Ensure that your Python script (sentiment_analysis.py) and the Docker-related files (Dockerfile and requirements.txt) are in the same directory.
Open a terminal and navigate to the project directory. Run the following command to build the Docker image:
docker build -t sentiment-analysis-app .
This command tells Docker to build an image named sentiment-analysis-app using the current directory as the build context.
After the image is built, you can run a Docker container from it:
docker run -p 8080:80 sentiment-analysis-app
This command maps port 8080 on your host machine to port 80 in the Docker container.
Open a web browser and navigate to
http://localhost:8080. You should see the sentiment analysis results displayed as a bar chart.
By containerizing your sentiment analysis solution using Docker, you've encapsulated the entire application and its dependencies into a portable and isolated environment. This makes it easier to share and deploy the application across different systems without worrying about compatibility issues. Remember to keep your Dockerfile and requirements updated as your project evolves. Happy containerizing!