Représentation « sac de mots »

Natural Language Processing (NLP) en Python

Fouad Trad

Machine Learning Engineer

Récapitulatif du flux NLP

Schéma complet du flux de travail indiquant que les chapitres 3 et 4 portent sur les bibliothèques transformers.

Natural Language Processing (NLP) en Python

Bag-of-Words (BoW)

  • Technique de base pour représenter du texte en nombres
  • Représente le texte en comptant la fréquence de chaque mot
  • Met les mots dans un sac et les compte
  • Ignore la grammaire et l'ordre

Image montrant des mots d'un texte jetés dans un sac, puis le comptage des occurrences individuelles.

Natural Language Processing (NLP) en Python

Exemple BoW

Image montrant deux phrases : « I love NLP » et « I love machine learning. »

Natural Language Processing (NLP) en Python

Exemple BoW

Image montrant le vocabulaire tiré des phrases : I, love, NLP, machine et learning.

  • Construire un vocabulaire de mots uniques
Natural Language Processing (NLP) en Python

Exemple BoW

Image affichant des vecteurs de caractéristiques pour chaque phrase, générés en comptant les occurrences selon le vocabulaire défini.

  • Construire un vocabulaire de mots uniques
  • Compter le nombre d'occurrences de chaque mot du vocabulaire
Natural Language Processing (NLP) en Python

BoW avec du code

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) en Python

BoW avec du code

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) en Python

Sortie 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 creuse : tableau surtout rempli de zéros

Natural Language Processing (NLP) en Python

Sortie 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) en Python

Fréquences des mots

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("Word Frequencies in Movie Reviews")
plt.xlabel("Words") plt.ylabel("Frequency") plt.show()

Diagramme à barres montrant les mots et leurs fréquences, les mots vides étant les plus fréquents.

Natural Language Processing (NLP) en Python

Passons à la pratique !

Natural Language Processing (NLP) en Python

Preparing Video For Download...