LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Advanced NLP with spaCy

Kurs anzeigen

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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
Code bearbeiten und ausführen