Rappresentazione Bag-of-Words

Natural Language Processing (NLP) in Python

Fouad Trad

Machine Learning Engineer

Riepilogo workflow NLP

Diagramma completo del workflow che indica che i Capitoli 3 e 4 trattano le librerie transformers.

Natural Language Processing (NLP) in Python

Bag-of-Words (BoW)

  • Tecnica di base per rappresentare testo come numeri
  • Rappresenta il testo contando quante volte appare ogni parola
  • Mette le parole in un “sacco” e le conta
  • Ignora grammatica e ordine

Immagine che mostra parole di un testo gettate in un sacco e poi il conteggio delle occorrenze delle singole parole.

Natural Language Processing (NLP) in Python

Esempio BoW

Immagine con due frasi: 'I love NLP' e 'I love machine learning.'

Natural Language Processing (NLP) in Python

Esempio BoW

Immagine con il vocabolario ricavato dalle frasi: I, love, NLP, machine e learning.

  • Crea un vocabolario di parole uniche
Natural Language Processing (NLP) in Python

Esempio BoW

Immagine con i vettori di feature per ogni frase, generati contando le occorrenze in base al vocabolario.

  • Crea un vocabolario di parole uniche
  • Conta quante volte appare ogni parola del vocabolario
Natural Language Processing (NLP) in Python

BoW con codice

reviews = ["I loved the movie. It was amazing!",
           "The movie was okay.",
           "I hated the movie. It was boring."]

def preprocess(text):
text = text.lower()
tokens = word_tokenize(text)
tokens = [word for word in tokens if word not in string.punctuation]
return " ".join(tokens)
cleaned_reviews = [preprocess(review) for review in reviews]
print(cleaned_reviews)
['i loved the movie it was amazing', 
 'the movie was okay', 
 'i hated the movie it was boring']
Natural Language Processing (NLP) in Python

BoW con codice

from sklearn.feature_extraction.text import CountVectorizer


vectorizer = CountVectorizer()
vectorizer.fit(cleaned_reviews)
print(vectorizer.get_feature_names_out())
['amazing' 'boring' 'hated' 'it' 'loved' 'movie' 'okay' 'the' 'was']
Natural Language Processing (NLP) in Python

Output BoW

X = vectorizer.transform(cleaned_reviews)

# OR X = vectorizer.fit_transform(cleaned_reviews)
print(X)
<Compressed Sparse Row sparse matrix of dtype 'int64'
    with 16 stored elements and shape (3, 9)>

Matrice sparsa: tabella quasi tutta di zeri

Natural Language Processing (NLP) in Python

Output BoW

print(X.toarray())
[[1 0 0 1 1 1 0 1 1]
 [0 0 0 0 0 1 1 1 1]
 [0 1 1 1 0 1 0 1 1]]
print(vectorizer.get_feature_names_out())
['amazing' 'boring' 'hated' 'it' 'loved' 'movie' 'okay' 'the' 'was']
Natural Language Processing (NLP) in Python

Frequenze delle parole

import numpy as np

word_counts = np.sum(X.toarray(), axis=0)
words = vectorizer.get_feature_names_out()
import matplotlib.pyplot as plt

plt.bar(words, word_counts)
plt.title("Frequenze delle parole nelle recensioni di film")
plt.xlabel("Parole") plt.ylabel("Frequenza") plt.show()

Grafico a barre con parole e frequenze; le stop word sono le più frequenti.

Natural Language Processing (NLP) in Python

Passons à la pratique !

Natural Language Processing (NLP) in Python

Preparing Video For Download...