Bag-of-Words-representation

Natural Language Processing (NLP) i Python

Fouad Trad

Machine Learning Engineer

Repetition av NLP-arbetsflödet

Komplett arbetsflödesdiagram som visar att kapitel 3 och 4 fokuserar på transformers-biblioteken.

Natural Language Processing (NLP) i Python

Bag-of-Words (BoW)

  • Grundläggande teknik för att representera text som tal
  • Representerar text genom att räkna hur ofta varje ord förekommer
  • Lägger orden i en påse och räknar dem
  • Ignorerar grammatik och ordning

Bild som visar hur ord ur en text läggs i en påse och sedan räknas individuellt.

Natural Language Processing (NLP) i Python

BoW-exempel

Bild som visar två meningar: "I love NLP" och "I love machine learning."

Natural Language Processing (NLP) i Python

BoW-exempel

Bild som visar det vokabulär som härletts från meningarna: I, love, NLP, machine och learning.

  • Bygg ett vokabulär av alla unika ord
Natural Language Processing (NLP) i Python

BoW-exempel

Bild som visar särdragsvektorer för varje mening, genererade genom att räkna ordförekomster enligt det definierade vokabuläret.

  • Bygg ett vokabulär av alla unika ord
  • Räkna hur många gånger varje ord i vokabuläret förekommer
Natural Language Processing (NLP) i Python

BoW med kod

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

BoW med kod

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

BoW-utdata

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

Gles matris: tabell till största delen fylld med nollor

Natural Language Processing (NLP) i Python

BoW-utdata

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

Ordfrekvenser

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

Stapeldiagram som visar ord och deras frekvenser, där stoppord är vanligast förekommande.

Natural Language Processing (NLP) i Python

Nu kör vi en övning!

Natural Language Processing (NLP) i Python

Preparing Video For Download...