Composants complexes
Dans cet exercice, vous allez écrire un composant personnalisé qui utilise le PhraseMatcher pour repérer des noms d’animaux dans le document et ajouter les segments trouvés à doc.ents.
Un PhraseMatcher avec les motifs d’animaux a déjà été créé dans la variable matcher. Le petit modèle anglais est disponible dans la variable nlp. L’objet Span a déjà été importé pour vous.
Cet exercice fait partie du cours
NLP avancé avec spaCy
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# 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