Esercizio di preprocessing del testo
Ora tocca a te applicare le tecniche che hai imparato per ripulire il testo e ottenere risultati NLP migliori. Dovrai rimuovere le stop word e i caratteri non alfabetici, lemmatizzare ed eseguire un nuovo bag-of-words sul testo pulito.
Parti dagli stessi token creati nell'esercizio precedente: lower_tokens. Hai anche importato la classe Counter.
Questo esercizio fa parte del corso
Introduzione al Natural Language Processing in Python
Istruzioni dell'esercizio
- Importa la classe
WordNetLemmatizerdanltk.stem. - Crea una lista
alpha_onlyche contenga solo caratteri alfabetici. Puoi usare il metodo.isalpha()per verificarlo. - Crea un'altra lista chiamata
no_stopscomposta dalle parole dialpha_onlyche non sono presenti inenglish_stops. - Inizializza un oggetto
WordNetLemmatizerchiamatowordnet_lemmatizere usa il suo metodo.lemmatize()sui token inno_stopsper creare una nuova lista chiamatalemmatized. - Crea un nuovo
Counterchiamatobowcon le parole lemmatizzate. - Infine, stampa i 10 token più comuni.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Import WordNetLemmatizer
____
# Retain alphabetic words: alpha_only
alpha_only = [t for t in ____ if ____]
# Remove all stop words: no_stops
no_stops = [t for t in ____ if t not in ____]
# Instantiate the WordNetLemmatizer
wordnet_lemmatizer = ____
# Lemmatize all tokens into a new list: lemmatized
lemmatized = [____ for t in ____]
# Create the bag-of-words: bow
bow = ____(____)
# Print the 10 most common tokens
print(____.____(__))