0
0
NLPml~10 mins

Entity types (PERSON, ORG, LOC, DATE) in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract named entities from the text using spaCy.

NLP
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Apple was founded by Steve Jobs in California.')
for ent in doc.[1]:
    print(ent.text, ent.label_)
Drag options to blanks, or click blank then click option'
Aspans
Bentities
Ctokens
Dents
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'entities' instead of 'ents' causes an attribute error.
2fill in blank
medium

Complete the code to filter and print only entities of type PERSON.

NLP
for ent in doc.ents:
    if ent.label_ == '[1]':
        print(ent.text)
Drag options to blanks, or click blank then click option'
ALOC
BDATE
CPERSON
DORG
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ORG' or 'LOC' will filter organizations or locations instead.
3fill in blank
hard

Fix the error in the code to correctly count the number of DATE entities.

NLP
date_count = 0
for ent in doc.ents:
    if ent.label_ == '[1]':
        date_count += [2]
print('Number of dates:', date_count)
Drag options to blanks, or click blank then click option'
ADATE
BPERSON
C1
Dent
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the entity object instead of 1 causes a TypeError.
4fill in blank
hard

Fill both blanks to create a dictionary mapping entity text to their types for ORG and LOC entities.

NLP
entity_dict = {ent.[1]: ent.[2] for ent in doc.ents if ent.label_ in ['ORG', 'LOC']}
Drag options to blanks, or click blank then click option'
Atext
Blabel_
Cstart_char
Dend_char
Attempts:
3 left
💡 Hint
Common Mistakes
Using character positions instead of text or label causes wrong dictionary keys or values.
5fill in blank
hard

Fill both blanks to print all unique entity types found in the text.

NLP
unique_labels = set(ent.[1] for ent in doc.[2])
for label in unique_labels:
    print(label)
Drag options to blanks, or click blank then click option'
Alabel_
Bents
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after label causes a TypeError in Python 3.