Home Explore Blog CI



docker

2nd chunk of `content/guides/sentiment-analysis.md`
bc90f6d3326c767cd0aaec4e765be5f96ff1315caf99951a0000000100000fb8
   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
   compound score.

5. Create the main loop.

   ```python
   if __name__ == "__main__":
       while True:
           input_text = input("Enter the text for semantic analysis (type 'exit' to end): ")

           if input_text.lower() == 'exit':
               print("Exiting...")
               break

           result = perform_semantic_analysis(input_text)
           print(f"Sentiment: {result}")
   ```

   This part of the script runs an infinite loop to accept user input for
   analysis. If the user types `exit`, the program terminates. Otherwise, it
   prints out the sentiment of the provided text.

6. Create `requirements.txt`.

   The sample application already contains the
   `requirements.txt` file to specify the necessary packages that the
   application imports. Open `requirements.txt` in a code or text editor to
   explore its contents.

   ```text
   # 01 sentiment_analysis
   nltk==3.6.5

   ...
   ```

   Only the `nltk` package is required for the sentiment analysis application.

## Explore the application environment

You'll use Docker to run the application in a container. Docker lets you
containerize the application, providing a consistent and isolated environment
for running it. This means the application will operate as intended within its
Docker container, regardless of the underlying system differences.

To run the application in a container, a Dockerfile is required. A Dockerfile is
a text document that contains all the commands you would call on the command
line to assemble an image. An image is a read-only template with instructions
for creating a Docker container.

The sample application already contains a `Dockerfile`. Open the `Dockerfile` in a code or text editor to explore its contents.

The following steps explain each part of the `Dockerfile`. For more details, see the [Dockerfile reference](/reference/dockerfile/).

1. Specify the base image.

   ```dockerfile
   FROM python:3.8-slim
   ```

   This command sets the foundation for the build. `python:3.8-slim` is a
   lightweight version of the Python 3.8 image, optimized for size and speed.
   Using this slim image reduces the overall size of your Docker image, leading
   to quicker downloads and less surface area for security vulnerabilities. This
   is particularly useful for a Python-based application where you might not
   need the full standard Python image.

2. Set the working directory.

   ```dockerfile
   WORKDIR /app
   ```

   `WORKDIR` sets the current working directory within the Docker image. By
   setting it to `/app`, you ensure that all subsequent commands in the
   Dockerfile (like `COPY` and `RUN`) are executed in this directory. This also
   helps in organizing your Docker image, as all application-related files are
   contained in a specific directory.

Title: Sentiment Analysis Code and Environment Exploration
Summary
This section covers the Python code for sentiment analysis, detailing the sentiment analysis function, the main loop for user input, and the `requirements.txt` file. It also explores the Docker environment setup, including the Dockerfile's base image selection (Python 3.8-slim) and the setting of the working directory.