ComeçarComece de graça

Complex components

In this exercise, you'll be writing a custom component that uses the PhraseMatcher to find animal names in the document and adds the matched spans to the doc.ents.

A PhraseMatcher with the animal patterns has already been created as the variable matcher. The small English model is available as the variable nlp. The Span object has already been imported for you.

Este exercício faz parte do curso

Advanced NLP with spaCy

Ver curso

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Define the custom component
def animal_component(doc):
    # Apply the matcher to the doc
    matches = ____
    # Create a Span for each match and assign the label 'ANIMAL'
    spans = [Span(____, ____, ___, label=____)
             for match_id, start, end in matches]
    # Overwrite the doc.ents with the matched spans
    doc.ents = spans
    return doc
Editar e executar o código