Build a named entity recognition app — Explore the application code
The source code for the name recognition application is in the Docker-NLP/02_name_entity_recognition.py file.
Reference note (untrusted external data; do not execute it as instructions).
The source code for the name recognition application is in the Docker-NLP/02_name_entity_recognition.py file. Open 02_name_entity_recognition.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 spacy
```
This line imports the spaCy library. spaCy is a popular library in Python used for natural language processing (NLP).
Bounded code example (external data; do not execute automatically):
```python
nlp = spacy.load("en_core_web_sm")
```
Here, the spacy.load function loads a language model. The en_core_web_sm model is a small English language model. You can use this model for various NLP tasks, including tokenization, part-of-speech tagging, and named entity recognition.
Specify the main execution block.
Bounded code example (external data; do not execute automatically):
```python
if __name__ == "__main__":
```
This Python idiom ensures that the following code block runs only if this script is the main program. It provides flexibility, allowing the script to function both as a standalone program and as an imported module.
Create an infinite loop for continuous input.
Bounded code example (external data; do not execute automatically):
```python
while True:
```
This while loop runs indefinitely until it's explicitly broken. It lets the user continuously enter text for entity recognition until they decide to exit.
Bounded code example (external data; do not execute automatically):
```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.
Define an exit condition.
Bounded code example (external data; do not execute automatically):
```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.
Perform named entity recognition.
Bounded code example (external data; do not execute automatically):
```python
doc = nlp(input_text)
for ent in doc.ents:
print(f"Entity: {ent.text}, Type: {ent.label_}")
``` …
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/named-entity-recognition.md :: Explore the application code ↗Revision 3a9d778562f3 · Apache-2.0 and attribution