Get startedGet started for free

Identifying named entities in news headlines

News organizations often tag named entities like people, locations, and organizations in headlines to improve search, indexing, and recommendations. Your job is to use a Hugging Face pipeline to automatically detect and group these entities in a news headline.

This exercise is part of the course

Natural Language Processing (NLP) in Python

View Course

Exercise instructions

  • Create a ner_pipeline using the "dslim/bert-base-NER" model.
  • Extract the named entities from the given headline.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from transformers import pipeline
# Create the NER pipeline
ner_pipeline = pipeline(
    task="____",
    model="____",
    grouped_entities=True
)
headline = "Apple is planning to open a new office in San Francisco next year."

# Get named entities
entities = ____

for entity in entities:
    print(f"{entity['entity_group']}: {entity['word']}")
Edit and Run Code