Biểu diễn Bag-of-Words

Natural Language Processing (NLP) bằng Python

Fouad Trad

Machine Learning Engineer

Ôn tập quy trình NLP

Sơ đồ quy trình đầy đủ, nêu Chương 3 và 4 tập trung vào thư viện transformers.

Natural Language Processing (NLP) bằng Python

Bag-of-Words (BoW)

  • Kỹ thuật nền tảng để biểu diễn văn bản thành số
  • Biểu diễn văn bản bằng cách đếm số lần mỗi từ xuất hiện
  • Bỏ các từ vào “túi” và đếm chúng
  • Bỏ qua ngữ pháp và thứ tự

Hình minh họa các từ của văn bản được ném vào một chiếc túi rồi đếm số lần xuất hiện từng từ.

Natural Language Processing (NLP) bằng Python

Ví dụ BoW

Hình hiển thị hai câu: 'I love NLP' và 'I love machine learning.'

Natural Language Processing (NLP) bằng Python

Ví dụ BoW

Hình hiển thị từ vựng rút ra từ các câu: I, love, NLP, machine và learning.

  • Xây dựng từ vựng gồm tất cả từ duy nhất
Natural Language Processing (NLP) bằng Python

Ví dụ BoW

Hình hiển thị vector đặc trưng cho từng câu, tạo bằng cách đếm số lần từ theo từ vựng đã định.

  • Xây dựng từ vựng gồm tất cả từ duy nhất
  • Đếm số lần mỗi từ trong từ vựng xuất hiện
Natural Language Processing (NLP) bằng Python

BoW với mã

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) bằng Python

BoW với mã

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) bằng Python

Đầu ra 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)>

Ma trận thưa: bảng chủ yếu là số 0

Natural Language Processing (NLP) bằng Python

Đầu ra 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) bằng Python

Tần suất từ

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("Tần suất từ trong đánh giá phim")
plt.xlabel("Từ") plt.ylabel("Tần suất") plt.show()

Biểu đồ cột hiển thị các từ và tần suất của chúng, với stop words xuất hiện nhiều nhất.

Natural Language Processing (NLP) bằng Python

Ayo berlatih!

Natural Language Processing (NLP) bằng Python

Preparing Video For Download...