Tokenizzazione e lemmatizzazione

Feature Engineering per NLP in Python

Rounak Banik

Data Scientist

Fonti di testo

  • Articoli di news
  • Tweet
  • Commenti
Feature Engineering per NLP in Python

Rendere il testo adatto alle macchine

  • Dogs, dog
  • reduction, REDUCING, Reduce
  • don't, do not
  • won't, will not
Feature Engineering per NLP in Python

Tecniche di preelaborazione del testo

  • Convertire le parole in minuscolo
  • Rimuovere spazi iniziali e finali
  • Rimuovere la punteggiatura
  • Rimuovere le stopword
  • Espandere le forme contratte
  • Rimuovere caratteri speciali (numeri, emoji, ecc.)
Feature Engineering per NLP in Python

Tokenizzazione

"I have a dog. His name is Hachi."

Token:

["I", "have", "a", "dog", ".", "His", "name", "is", "Hachi", "."]
"Don't do this."

Token:

["Do", "n't", "do", "this", "."]
Feature Engineering per NLP in Python

Tokenizzazione con spaCy

import spacy

# Load the en_core_web_sm model nlp = spacy.load('en_core_web_sm')
# Initiliaze string string = "Hello! I don't know what I'm doing here."
# Create a Doc object doc = nlp(string)
# Generate list of tokens tokens = [token.text for token in doc] print(tokens)
['Hello','!','I','do',"n't",'know','what','I',"'m",'doing','here','.']
Feature Engineering per NLP in Python

Lemmatizzazione

  • Converte le parole nella forma base
    • reducing, reduces, reduced, reduction → reduce
    • am, are, is → be
    • n't → not
    • 've → have
Feature Engineering per NLP in Python

Lemmatizzazione con spaCy

import spacy

# Load the en_core_web_sm model
nlp = spacy.load('en_core_web_sm')
# Initiliaze string
string = "Hello! I don't know what I'm doing here."
# Create a Doc object
doc = nlp(string)

# Generate list of lemmas lemmas = [token.lemma_ for token in doc] print(lemmas)
['hello','!','-PRON-','do','not','know','what','-PRON','be','do','here', '.']
Feature Engineering per NLP in Python

Esercitiamoci!

Feature Engineering per NLP in Python

Preparing Video For Download...