Complete the code to extract named entities from the text using spaCy.
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_)
The doc.ents attribute contains the named entities detected in the text.
Complete the code to filter and print only entities of type PERSON.
for ent in doc.ents: if ent.label_ == '[1]': print(ent.text)
The entity label PERSON identifies people in the text.
Fix the error in the code to correctly count the number of DATE entities.
date_count = 0 for ent in doc.ents: if ent.label_ == '[1]': date_count += [2] print('Number of dates:', date_count)
We check if the entity label is 'DATE' and add 1 to the count for each match.
Fill both blanks to create a dictionary mapping entity text to their types for ORG and LOC entities.
entity_dict = {ent.[1]: ent.[2] for ent in doc.ents if ent.label_ in ['ORG', 'LOC']}Use ent.text for the entity string and ent.label_ for its type.
Fill both blanks to print all unique entity types found in the text.
unique_labels = set(ent.[1] for ent in doc.[2]) for label in unique_labels: print(label)
We get entity labels with ent.label_, iterate over doc.ents, and print labels directly.
