Impostare attributi di estensione (2)
Proviamo a impostare attributi più complessi usando getter ed estensioni di metodo. L'oggetto nlp è già stato creato per te e le classi Doc, Token e Span sono già importate.
Ricorda che se esegui il tuo codice più di una volta, potresti vedere un messaggio di errore che indica che l'estensione esiste già. Questo perché DataCamp riavvia il tuo codice nella stessa sessione. Per risolvere, puoi impostare force=True su set_extension, oppure ricaricare per avviare una nuova sessione Python. Nulla di tutto ciò influisce sulla risposta che invii.
Questo esercizio fa parte del corso
NLP avanzato con spaCy
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Define the getter function
def get_has_number(doc):
# Return if any of the tokens in the doc return True for token.like_num
return any(____ for token in doc)
# Register the Doc property extension 'has_number' with the getter get_has_number
____.____(____, ____=____)
# Process the text and check the custom has_number attribute
doc = nlp("The museum closed for five years in 2012.")
print('has_number:', ____)