Menu Close

Bag of Words (BoW)

1. Pengertian Bag of Words (BoW)

Bag of Words (BoW) adalah teknik representasi teks ke dalam bentuk numerik yang digunakan dalam Natural Language Processing (NLP) dan Information Retrieval. Pendekatan ini tidak memperhatikan urutan kata tetapi fokus pada frekuensi kemunculan kata dalam dokumen.

Sumber: https://www.freepik.com/

Ciri utama BoW:

  • Mengabaikan tata bahasa dan urutan kata.
  • Mempertimbangkan hanya keberadaan (presence) atau frekuensi (frequency) kata.
  • Digunakan untuk analisis teks, seperti klasifikasi teks, pengelompokan dokumen, dan pencarian informasi.

2. Cara Kerja Bag of Words

a. Proses Utama BoW:

  1. Tokenisasi: Memecah teks menjadi kata-kata individual (tokens).
    Contoh:
    Teks: “Bag of Words is simple and powerful”
    Tokens: ["Bag", "of", "Words", "is", "simple", "and", "powerful"]
  2. Membuat Kosakata (Vocabulary): Membuat daftar unik dari semua kata yang muncul di teks/dokumen.
    Contoh:
    Kosakata: ["Bag", "of", "Words", "is", "simple", "and", "powerful"]
  3. Membangun Representasi Vektor: Representasi setiap dokumen dibuat berdasarkan frekuensi kata-kata dari kosakata.
    Contoh:
    Teks: “Words are powerful, and bag of words is simple.”
    Kosakata: ["Bag", "of", "Words", "is", "simple", "and", "powerful"]
    Vektor: [1, 1, 2, 1, 1, 1, 1]

3. Variasi Implementasi Bag of Word

a. Binary Bag of Words (Binary BoW):

Mencatat keberadaan kata saja (1 jika kata ada, 0 jika tidak ada).
Contoh:
Teks: “Bag of words is simple.”
Kosakata: ["Bag", "of", "Words", "is", "simple", "and", "powerful"]
Binary Vector: [1, 1, 1, 1, 1, 0, 0]

b. Term Frequency (TF):

Menghitung jumlah kemunculan kata di dokumen tertentu.
Contoh:
Teks: “Words are powerful, and bag of words is simple.”
Kosakata: ["Bag", "of", "Words", "is", "simple", "and", "powerful"]
Term Frequency: [1, 1, 2, 1, 1, 1, 1]

c. TF-IDF (Term Frequency-Inverse Document Frequency):

  • Kombinasi dari Term Frequency dan Inverse Document Frequency (IDF).
  • IDF mengurangi bobot kata yang sering muncul di banyak dokumen (kata umum seperti “the”, “is”).
    Formula:

TF-IDF(t,d)=TF(t,d)×log⁡NDF(t)\text{TF-IDF}(t, d) = \text{TF}(t, d) \times \log \frac{N}{\text{DF}(t)}TF-IDF(t,d)=TF(t,d)×logDF(t)N​

Keterangan:

  • ttt: kata tertentu
  • ddd: dokumen tertentu
  • NNN: jumlah total dokumen
  • DF(t)\text{DF}(t)DF(t): jumlah dokumen yang mengandung kata ttt

4. Keuntungan dan Kelemahan Bag of Words

Keuntungan:

  • Sederhana dan intuitif: Mudah diimplementasikan.
  • Efektif untuk data teks kecil: Sangat baik untuk teks pendek dan dataset yang sederhana.
  • Kompatibilitas: Dapat digunakan dengan berbagai algoritma machine learning (SVM, Naive Bayes, dll).

Kelemahan:

  1. Mengabaikan urutan kata: BoW tidak memperhatikan konteks atau urutan kata. Contoh: “I love NLP” dan “NLP love I” dianggap sama.
  2. Dimensi besar: Kosakata yang besar menyebabkan representasi BoW menjadi sparse (banyak nilai 0).
  3. Tidak menangkap makna semantik: Tidak bisa membedakan sinonim atau polisemik (kata dengan makna berbeda).

5. Contoh Implementasi Bag of Words di Python

Berikut contoh sederhana menggunakan Python dan CountVectorizer dari scikit-learn:

from sklearn.feature_extraction.text import CountVectorizer

# Data contoh
corpus = [
    "Bag of Words is simple and powerful",
    "Words are powerful and meaningful",
    "Bag of Words ignores word order"
]

# Membuat model BoW
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)

# Menampilkan Kosakata
print("Kosakata:", vectorizer.get_feature_names_out())

# Menampilkan Representasi BoW
print("Representasi BoW:\n", X.toarray())

Output:

luaCopy codeKosakata: ['and', 'are', 'bag', 'ignores', 'is', 'meaningful', 'of', 'order', 'powerful', 'simple', 'word', 'words']
Representasi BoW:
 [[1 0 1 0 1 0 1 0 1 1 0 1]
  [1 1 0 0 0 1 0 0 1 0 0 1]
  [0 0 1 1 0 0 1 1 0 0 1 1]]

6. Kapan Menggunakan Bag of Words?

BoW cocok digunakan jika:

  • Dataset kecil: Saat data teks relatif sederhana.
  • Model cepat: Untuk kebutuhan model baseline atau pemrosesan cepat.
  • Fokus pada frekuensi kata: Misalnya, untuk klasifikasi atau clustering berbasis kata kunci.

Namun, jika Anda membutuhkan pemahaman lebih dalam terhadap konteks atau makna teks, seperti analisis semantik, pertimbangkan teknik lain seperti Word2Vec, TF-IDF, atau model berbasis transformer (BERT, GPT).

Leave a Reply

Your email address will not be published. Required fields are marked *