DEV Community

Ajeet Singh Raina
Ajeet Singh Raina

Posted on

How to Build and Containerise Sentiment Analysis Using Python, Twitter and Docker

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.

Prerequisites:

  • Basic knowledge of Python.
  • Twitter Developer Account (for API access).
  • Docker Desktop
  • Python libraries:
    • Tweepy (for accessing Twitter's API),
    • TextBlob (for sentiment analysis), and
    • Matplotlib (for visualization).

Step 1: Set Up Twitter API Access:

  1. Create a Twitter Developer Account and create a new App to obtain API keys and access tokens.

  2. Install Tweepy using pip install tweepy.

Step 2: Authentication

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)
Enter fullscreen mode Exit fullscreen mode

Step 3: Collect Tweets:

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]
Enter fullscreen mode Exit fullscreen mode

Step 4: Perform Sentiment Analysis:

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']
Enter fullscreen mode Exit fullscreen mode

Step 5: Visualize the Results:

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()
Enter fullscreen mode Exit fullscreen mode

Step 6: Containerising the Sentiment Analysis using Docker

Containerizing the sentiment analysis solution using Docker can make it easier to distribute and deploy. Here's how you can containerize the solution:

Step 7: Create a Dockerfile

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"]
Enter fullscreen mode Exit fullscreen mode

Step 8: Create a Requirements File

Create a file named requirements.txt in the same directory with the following content:

tweepy
textblob
matplotlib
Enter fullscreen mode Exit fullscreen mode

Step 9: Organize Your Files

Ensure that your Python script (sentiment_analysis.py) and the Docker-related files (Dockerfile and requirements.txt) are in the same directory.

Step 10: Build the Docker Image

Open a terminal and navigate to the project directory. Run the following command to build the Docker image:

docker build -t sentiment-analysis-app .
Enter fullscreen mode Exit fullscreen mode

This command tells Docker to build an image named sentiment-analysis-app using the current directory as the build context.

Step 11: Run the Docker Container

After the image is built, you can run a Docker container from it:

docker run -p 8080:80 sentiment-analysis-app
Enter fullscreen mode Exit fullscreen mode

This command maps port 8080 on your host machine to port 80 in the Docker container.

Step 12: Access the Application

Open a web browser and navigate to http://localhost:8080. You should see the sentiment analysis results displayed as a bar chart.

Conclusion

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!

Top comments (0)