Stems dai tweet
In questo esercizio lavorerai con un array chiamato tweets. Contiene il testo dei dati di sentiment delle compagnie aeree raccolti da Twitter.
Il tuo compito è lavorare con questo array e trasformarlo in una lista di token usando una list comprehension. Dopo di che, itera sulla lista di token e crea lo stem di ciascun token. Ricorda che le list comprehension sono un'alternativa in una sola riga ai cicli for.
Questo esercizio fa parte del corso
Sentiment Analysis con Python
Istruzioni dell'esercizio
- Importa la funzione che abbiamo usato per trasformare le stringhe in stems.
- Richiama la funzione Porter stemmer che hai appena importato.
- Usando una list comprehension, crea la lista
tokens. Dovrebbe contenere tutti i token di parola dall'arraytweets. - Itera sulla lista
tokense applica la funzione di stemming a ciascun elemento della lista.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Import the function to perform stemming
____
from nltk import word_tokenize
# Call the stemmer
porter = ____()
# Transform the array of tweets to tokens
tokens = [____]
# Stem the list of tokens
stemmed_tokens = [[____.____(word) for word in tweet] for tweet in tokens]
# Print the first element of the list
print(stemmed_tokens[0])