Bird
Raised Fist0
NLPml~20 mins

NER with spaCy in NLP - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
NER spaCy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Named Entity Recognition with spaCy
What is the output of the following code snippet that uses spaCy to detect named entities in a sentence?
NLP
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Apple is looking at buying U.K. startup for $1 billion')
entities = [(ent.text, ent.label_) for ent in doc.ents]
print(entities)
A[('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')]
B[('Apple', 'PERSON'), ('U.K.', 'LOC'), ('$1 billion', 'MONEY')]
C[('Apple', 'ORG'), ('startup', 'ORG'), ('$1 billion', 'MONEY')]
D[('Apple', 'ORG'), ('U.K.', 'ORG'), ('$1 billion', 'MONEY')]
Attempts:
2 left
💡 Hint
Look at common entity labels spaCy uses for organizations, geopolitical entities, and money.
Model Choice
intermediate
1:30remaining
Choosing the Correct spaCy Model for NER
Which spaCy model is best suited for performing Named Entity Recognition on English text with a balance of speed and accuracy?
Aen_core_web_md
Ben_vectors_web_lg
Cen_core_web_lg
Den_core_web_sm
Attempts:
2 left
💡 Hint
Consider models that include trained pipelines for NER and balance size and speed.
Hyperparameter
advanced
2:00remaining
Effect of Batch Size in spaCy NER Training
During training a spaCy NER model, what is the effect of increasing the batch size parameter?
AIt slows down training and always improves model accuracy.
BIt causes the model to overfit immediately.
CIt has no effect on training speed or accuracy.
DIt speeds up training but may reduce model accuracy due to less frequent weight updates.
Attempts:
2 left
💡 Hint
Think about how batch size affects gradient updates and training speed.
Metrics
advanced
1:30remaining
Evaluating spaCy NER Model Performance
Which metric is most appropriate to evaluate the quality of a spaCy Named Entity Recognition model on a test dataset?
AAccuracy of token classification
BMean Squared Error
CF1-score
DPerplexity
Attempts:
2 left
💡 Hint
NER is a sequence labeling task with imbalanced classes; consider precision and recall.
🔧 Debug
expert
2:30remaining
Debugging spaCy NER Model Training Error
You run spaCy NER training code but get this error: 'ValueError: [E088] The component 'ner' is not initialized. Call 'nlp.begin_training()' first.' Which code snippet fixes this error?
A
nlp = spacy.load('en_core_web_sm')
ner = nlp.get_pipe('ner')
optimizer = nlp.begin_training()
B
nlp = spacy.blank('en')
ner = nlp.add_pipe('ner')
optimizer = nlp.begin_training()
C
nlp = spacy.load('en_core_web_sm')
optimizer = nlp.create_optimizer()
D
nlp = spacy.blank('en')
optimizer = nlp.begin_training()
Attempts:
2 left
💡 Hint
The error means the 'ner' component is missing or not initialized properly.

Practice

(1/5)
1. What does NER (Named Entity Recognition) do in natural language processing?
easy
A. It generates new text based on input prompts.
B. It translates text from one language to another.
C. It summarizes long documents into short paragraphs.
D. It finds and labels important names and terms in text automatically.

Solution

  1. Step 1: Understand NER's purpose

    NER identifies specific names like people, places, or organizations in text.
  2. Step 2: Compare with other NLP tasks

    Translation, summarization, and text generation are different tasks than NER.
  3. Final Answer:

    It finds and labels important names and terms in text automatically. -> Option D
  4. Quick Check:

    NER = Finds names and terms [OK]
Hint: NER extracts names and terms, not translations or summaries [OK]
Common Mistakes:
  • Confusing NER with translation or summarization
  • Thinking NER generates new text
  • Believing NER only finds keywords, not named entities
2. Which of the following is the correct way to load a pre-trained spaCy model for NER?
easy
A. import spacy; nlp = spacy.load('en_core_web_sm')
B. import spacy; nlp = spacy.model('en_core_web_sm')
C. import spacy; nlp = spacy.load_model('en_core_web_sm')
D. import spacy; nlp = spacy.get('en_core_web_sm')

Solution

  1. Step 1: Recall spaCy model loading syntax

    spaCy uses spacy.load('model_name') to load pre-trained models.
  2. Step 2: Check each option

    Only import spacy; nlp = spacy.load('en_core_web_sm') uses spacy.load correctly; others use invalid functions.
  3. Final Answer:

    import spacy; nlp = spacy.load('en_core_web_sm') -> Option A
  4. Quick Check:

    spaCy model loading = spacy.load() [OK]
Hint: Use spacy.load('model_name') to load models [OK]
Common Mistakes:
  • Using spacy.model or spacy.load_model which don't exist
  • Trying spacy.get which is not a spaCy function
  • Forgetting to import spacy before loading
3. Given this code snippet using spaCy for NER:
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Apple is looking at buying U.K. startup for $1 billion')
entities = [(ent.text, ent.label_) for ent in doc.ents]
print(entities)

What will be the output?
medium
A. [('Apple', 'PERSON'), ('U.K.', 'ORG'), ('$1 billion', 'QUANTITY')]
B. [('Apple', 'ORG'), ('startup', 'ORG'), ('$1 billion', 'MONEY')]
C. [('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')]
D. [('Apple', 'GPE'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')]

Solution

  1. Step 1: Understand spaCy NER labels

    Apple is recognized as an organization (ORG), U.K. as geopolitical entity (GPE), and $1 billion as money (MONEY).
  2. Step 2: Match entities with labels

    [('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')] correctly matches these entities and labels as spaCy outputs.
  3. Final Answer:

    [('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')] -> Option C
  4. Quick Check:

    spaCy NER output matches [('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')] [OK]
Hint: Check spaCy's common entity labels for correct matches [OK]
Common Mistakes:
  • Confusing ORG with PERSON or GPE
  • Mislabeling MONEY as QUANTITY
  • Including words like 'startup' as entities
4. You run this code but get an error:
import spacy
doc = nlp('Google is a tech giant')

What is the most likely cause?
medium
A. spaCy does not support the word 'Google'.
B. The variable 'nlp' is not defined before use.
C. The text input is too short for NER.
D. Missing parentheses in the print statement.

Solution

  1. Step 1: Check variable definitions

    The code uses 'nlp' without defining it by loading a spaCy model first.
  2. Step 2: Identify error cause

    This causes a NameError because 'nlp' is undefined.
  3. Final Answer:

    The variable 'nlp' is not defined before use. -> Option B
  4. Quick Check:

    Undefined variable 'nlp' causes error [OK]
Hint: Always load model with spacy.load before using nlp [OK]
Common Mistakes:
  • Assuming text length causes error
  • Thinking spaCy can't recognize common words
  • Confusing print syntax errors with variable errors
5. You want to extract only person names from a text using spaCy's NER. Which code snippet correctly filters for persons?
hard
A. persons = [ent.text for ent in doc.ents if ent.label_ == 'PERSON']
B. persons = [ent.text for ent in doc.ents if ent.label_ == 'ORG']
C. persons = [ent.text for ent in doc.ents if ent.label_ == 'GPE']
D. persons = [ent.text for ent in doc.ents if ent.label_ == 'MONEY']

Solution

  1. Step 1: Identify label for persons in spaCy

    spaCy uses 'PERSON' label for people names.
  2. Step 2: Filter entities by 'PERSON'

    Filtering doc.ents by ent.label_ == 'PERSON' extracts only person names.
  3. Final Answer:

    persons = [ent.text for ent in doc.ents if ent.label_ == 'PERSON'] -> Option A
  4. Quick Check:

    Filter entities by 'PERSON' label [OK]
Hint: Filter entities with label_ == 'PERSON' to get names [OK]
Common Mistakes:
  • Using wrong labels like ORG or GPE for persons
  • Not filtering entities at all
  • Confusing entity text with label