MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Advanced NLP with spaCy

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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
Edit dan Jalankan Kode