Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'entities' instead of 'ents' causes an attribute error.
✗ Incorrect
The doc.ents attribute contains the named entities detected in the text.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ORG' or 'LOC' will filter organizations or locations instead.
✗ Incorrect
The entity label PERSON identifies people in the text.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the entity object instead of 1 causes a TypeError.
✗ Incorrect
We check if the entity label is 'DATE' and add 1 to the count for each match.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using character positions instead of text or label causes wrong dictionary keys or values.
✗ Incorrect
Use ent.text for the entity string and ent.label_ for its type.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after label causes a TypeError in Python 3.
✗ Incorrect
We get entity labels with ent.label_, iterate over doc.ents, and print labels directly.