Performing named entity recognition using spaCy
In this recipe, we will parse out named entities from an article text used in Chapter 4, Classifying Texts. We will load the package and the parsing engine, and loop through the NER results.
Getting ready
In this recipe, we will use the spacy
package. If you haven't installed it yet, install it using the following command:
pip install spacy
After you install spaCy, you will need to download a language model. We will download the small model:
python -m spacy download en_core_web_sm
How to do it…
The NER happens automatically with the processing that spaCy does for an input text. Accessing the entities happens through the doc.ents
variable. The steps for this recipe are as follows:
- Import
spacy
:import spacy
- Initialize the spaCy engine:
nlp = spacy.load("en_core_web_sm")
- Initialize the article text:
article = """iPhone 12: Apple makes jump to 5G Apple has confirmed its...