Bag-of-Words-representatie

Natural Language Processing (NLP) in Python

Fouad Trad

Machine Learning Engineer

NLP-workflow: terugblik

Het volledige workflowdiagram met de opmerking dat hoofdstukken 3 en 4 focussen op de transformers-bibliotheken.

Natural Language Processing (NLP) in Python

Bag-of-Words (BoW)

  • Basistechniek om tekst als getallen weer te geven
  • Stel tekst voor door te tellen hoe vaak elk woord voorkomt
  • Gooi woorden in een ‘zak’ en tel ze
  • Negeert grammatica en volgorde

Afbeelding met woorden uit een tekst die in een zak worden gegooid en vervolgens worden de afzonderlijke woordvoorkomens geteld.

Natural Language Processing (NLP) in Python

BoW-voorbeeld

Afbeelding met twee zinnen: 'I love NLP' en 'I love machine learning.'

Natural Language Processing (NLP) in Python

BoW-voorbeeld

Afbeelding met de woordenschat afgeleid van de zinnen: I, love, NLP, machine en learning.

  • Bouw een vocabulaire van alle unieke woorden
Natural Language Processing (NLP) in Python

BoW-voorbeeld

Afbeelding met featurevectoren per zin, gemaakt door woordvoorkomens te tellen volgens het gedefinieerde vocabulaire.

  • Bouw een vocabulaire van alle unieke woorden
  • Tel hoe vaak elk woord uit het vocabulaire voorkomt
Natural Language Processing (NLP) in Python

BoW met 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) in Python

BoW met 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) in Python

BoW-uitvoer

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

Sparce matrix: tabel vooral gevuld met nullen

Natural Language Processing (NLP) in Python

BoW-uitvoer

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

Woordfrequenties

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()

Staafdiagram met woorden en hun frequenties; stopwoorden komen het meest voor.

Natural Language Processing (NLP) in Python

Laten we oefenen!

Natural Language Processing (NLP) in Python

Preparing Video For Download...