pythonbeginner

spaCy Named Entity Recognition

Extract entities, dependencies, and noun phrases from text using spaCy's industrial-strength NLP pipeline.

python
import spacy

nlp = spacy.load('en_core_web_sm')

texts = [
    'Elon Musk founded SpaceX in Hawthorne, California in 2002.',
    'OpenAI released GPT-4 in March 2023 and it was trained on a massive dataset.',
    'The Python Software Foundation is based in Delaware, USA.',
]

for text in texts:
    doc = nlp(text)
    print(f'Text: {text}')
    print('  Entities:')
    for ent in doc.ents:
        print(f'    {ent.text!r:30} [{ent.label_}] - {spacy.explain(ent.label_)}')
    noun_phrases = [chunk.text for chunk in doc.noun_chunks]
    print(f'  Noun phrases: {noun_phrases}\n')

Use Cases

  • entity extraction
  • text mining
  • information extraction

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.