the user continuously enter text for entity recognition until they decide
to exit.
5. Get user input.
```python
input_text = input("Enter the text for entity recognition (type 'exit' to end): ")
```
This line prompts the user to enter text. The program will then perform entity recognition on this text.
6. Define an exit condition.
```python
if input_text.lower() == 'exit':
print("Exiting...")
break
```
If the user types something, the program converts the input to lowercase and
compares it to `exit`. If they match, the program prints **Exiting...** and
breaks out of the while loop, effectively ending the program.
7. Perform named entity recognition.
```python
doc = nlp(input_text)
for ent in doc.ents:
print(f"Entity: {ent.text}, Type: {ent.label_}")
```
- `doc = nlp(input_text)`: Here, the nlp model processes the user-input text. This creates a Doc object which contains various NLP attributes, including identified entities.
- `for ent in doc.ents:`: This loop iterates over the entities found in the text.
- `print(f"Entity: {ent.text}, Type: {ent.label_}")`: For each entity, it prints the entity text and its type (like PERSON, ORG, or GPE).
8. 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
# 02 named_entity_recognition
spacy==3.7.2
...
```
Only the `spacy` package is required for the named recognition 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.
3. Copy the requirements file into the image.
```dockerfile
COPY requirements.txt /app
```
The `COPY` command transfers the `requirements.txt` file from
your local machine into the Docker image. This file lists all Python
dependencies required by the application. Copying it into the container
lets the next command (`RUN pip install`) install these dependencies
inside the image environment.
4. Install the Python dependencies in the image.
```dockerfile
RUN pip install --no-cache-dir -r requirements.txt
```
This line uses `pip`, Python's package installer, to install the packages