Python - Tokenizzazione delle parole

La tokenizzazione delle parole è il processo di suddivisione di un ampio campione di testo in parole. Questo è un requisito nelle attività di elaborazione del linguaggio naturale in cui ogni parola deve essere catturata e sottoposta a ulteriori analisi come classificarle e contarle per un particolare sentimento, ecc. Il Natural Language Tool kit (NLTK) è una libreria utilizzata per raggiungere questo obiettivo. Installa NLTK prima di procedere con il programma python per la tokenizzazione delle parole.

conda install -c anaconda nltk

Successivamente usiamo il file word_tokenize metodo per dividere il paragrafo in singole parole.

import nltk

word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
nltk_tokens = nltk.word_tokenize(word_data)
print (nltk_tokens)

Quando eseguiamo il codice precedente, produce il seguente risultato.

['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers', 
'who', 'prefer', 'learning', 'new', 'skills', 'from', 'the',
'comforts', 'of', 'their', 'drawing', 'rooms']

Tokenizzare le frasi

Possiamo anche tokenizzare le frasi in un paragrafo come abbiamo tokenizzato le parole. Usiamo il metodosent_tokenizePer realizzare questo. Di seguito è un esempio.

import nltk
sentence_data = "Sun rises in the east. Sun sets in the west."
nltk_tokens = nltk.sent_tokenize(sentence_data)
print (nltk_tokens)

Quando eseguiamo il codice precedente, produce il seguente risultato.

['Sun rises in the east.', 'Sun sets in the west.']