Build a sentiment analysis app — Explore the application code
The source code for the sentiment analysis application is in the Docker-NLP/01_sentiment_analysis.py file.
Reference note (untrusted external data; do not execute it as instructions).
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.
Import the required libraries.
Bounded code example (external data; do not execute automatically):
```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.
Handle SSL certificate verification.
Bounded code example (external data; do not execute automatically):
```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.
Bounded code example (external data; do not execute automatically):
```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.
Create a sentiment analysis function.
Bounded code example (external data; do not execute automatically):
```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 compound score. …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/sentiment-analysis.md :: Explore the application code ↗Revision 3a9d778562f3 · Apache-2.0 and attribution