---
title: Build a sentiment analysis app
linkTitle: Sentiment analysis
keywords: nlp, natural language processing, sentiment analysis, python, nltk
description: Learn how to build and run a sentiment analysis application using Python, NLTK, and Docker.
summary: |
This guide demonstrates how to containerize sentiment analysis models using
Docker.
tags: [ai]
languages: [python]
aliases:
- /guides/use-case/nlp/sentiment-analysis/
params:
time: 20 minutes
---
## Overview
In this guide, you learn how to build and run a sentiment analysis application.
You'll build the application using Python with the Natural Language Toolkit
(NLTK), and then set up the environment and run the application using Docker.
The application analyzes user input text for sentiment using NLTK's
SentimentIntensityAnalyzer and outputs whether the sentiment is positive,
negative, or neutral.
## Prerequisites
- You have installed the latest version of [Docker Desktop](/get-started/get-docker.md). Docker adds new features regularly and some parts of this guide may work only with the latest version of Docker Desktop.
- You have a [Git client](https://git-scm.com/downloads). The examples in this section use a command-line based Git client, but you can use any client.
## Get the sample application
1. Open a terminal, and clone the sample application's repository using the
following command.
```console
$ git clone https://github.com/harsh4870/Docker-NLP.git
```
2. Verify that you cloned the repository.
You should see the following files in your `Docker-NLP` directory.
```text
01_sentiment_analysis.py
02_name_entity_recognition.py
03_text_classification.py
04_text_summarization.py
05_language_translation.py
entrypoint.sh
requirements.txt
Dockerfile
README.md
```
## Explore the application code
The source code for the sentiment analysis application is in the `Docker-NLP/01_sentiment_analysis.py` file. Open `01_sentiment_analysis.py` in a text or code editor to explore its contents in the following steps.
1. Import the required libraries.
```python
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
import ssl
```
- `nltk`: This is the Natural Language Toolkit library used for working with
human language data in Python.
- `SentimentIntensityAnalyzer`: This is a specific tool from NLTK used for
determining the sentiment of a piece of text.
- `ssl`: This module provides access to Transport Layer Security (encryption)
functions used for secure web connections.
2. Handle SSL certificate verification.
```python
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
```
This block is a workaround for certain environments where downloading data through NLTK might fail due to SSL certificate verification issues. It's telling Python to ignore SSL certificate verification for HTTPS requests.
3. Download NLTK resources.
```python
nltk.download('vader_lexicon')
nltk.download('punkt')
```
- `vader_lexicon`: This is a lexicon used by the `SentimentIntensityAnalyzer`
for sentiment analysis.
- `punkt`: This is used by NLTK for tokenizing sentences. It's necessary for
the `SentimentIntensityAnalyzer` to function correctly.
4. Create a sentiment analysis function.
```python
def perform_semantic_analysis(text):
sid = SentimentIntensityAnalyzer()
sentiment_score = sid.polarity_scores(text)
if sentiment_score['compound'] >= 0.05:
return "Positive"
elif sentiment_score['compound'] <= -0.05:
return "Negative"
else:
return "Neutral"
```
- `SentimentIntensityAnalyzer()` creates an instance of the
analyzer.
- `polarity_scores(text)` generates a sentiment score for the input text.
The function returns **Positive**, **Negative**, or **Neutral** based on the