রোডম্যাপ
PHASE 2 · অধ্যায় 10

ব্যাগ অফ ওয়ার্ডস

Bag of Words

Text কে numbers এ convert করার প্রথম formula।

ভূমিকা

Computer অংক জানে, ভাষা না। তাহলে আমরা যদি ভাষাকে অংকে রূপান্তর করতে পারি — computer ভাষা বুঝবে। সবচেয়ে সহজ recipe: প্রতিটা document কে গুনে দেখুন কোন word কতবার আছে — এটাই Bag of Words। নাম শুনে মনে হবে ছেলেমানুষি, কিন্তু এটা ছিল NLP এর প্রথম বিপ্লব।

ধারণা

Bag of Words (BoW) হলো text কে numeric vector এ রূপান্তর করার সবচেয়ে সহজ technique। প্রথমে পুরো corpus থেকে unique words এর একটা vocabulary বানানো হয়। তারপর প্রতিটা document কে একটা vector হিসেবে represent করা হয় — যেখানে প্রতিটা position এ সেই vocabulary-word টা ওই document এ কতবার এসেছে তার count।

সহজ ব্যাখ্যা

ভাবুন আপনার কাছে অনেকগুলো ব্যাগ আছে, প্রতিটায় বিভিন্ন রঙের মার্বেল। প্রতিটা ব্যাগের 'identity' হলো — কোন রঙের মার্বেল কতটা আছে। Word order, grammar, context — কিছুই matter করে না, শুধু count। সেজন্যই 'bag' — order ভুলে যান, শুধু গণনা গোনেন। 'I love NLP' আর 'NLP love I' — BoW এর কাছে identical!

বাস্তব ব্যবহার

  • Spam filter — spam email এ কোন word কতবার আসে তার pattern শেখে।
  • Document classification — news article কোন category তে।
  • Sentiment analysis — review এ positive/negative word এর count।
  • Search engine এর foundation — modern search ও BoW থেকে শুরু।
  • Topic detection — কোন document কোন topic এ — word frequency দিয়ে।

ধাপে ধাপে বিশ্লেষণ

1
Step 1 — Corpus প্রস্তুত করুন
কয়েকটা document একসাথে রাখুন (list of strings)।
2
Step 2 — Vocabulary বানান
সব document এর সব unique word collect করুন।
3
Step 3 — Count Vector বানান
প্রতিটা document এর জন্য একটা vector, যেখানে প্রতিটা vocab word এর count।
4
Step 4 — Matrix বানান
All documents এর vector একসাথে = Document-Term Matrix।
5
Step 5 — ML model এ feed করুন
এই matrix কে ML algorithm এ pass করে classification/clustering।

Python কোড

from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd

corpus = [
    "I love NLP and machine learning",
    "Machine learning is fun",
    "NLP is a part of machine learning",
    "I love Bangla NLP",
]

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)

# Vocabulary
vocab = vectorizer.get_feature_names_out()
print("Vocabulary:", vocab)

# Document-Term Matrix
df = pd.DataFrame(X.toarray(), columns=vocab)
print("\nDocument-Term Matrix:")
print(df)

# Single document encoding
new_text = ["I love machine learning"]
encoded = vectorizer.transform(new_text).toarray()
print("\nEncoded:", encoded)

# With stop word removal + lowercase + bigrams
v2 = CountVectorizer(stop_words="english", ngram_range=(1, 2))
X2 = v2.fit_transform(corpus)
print("\nWith bigrams vocab:", v2.get_feature_names_out())
ব্যাখ্যা

sklearn এর CountVectorizer automatic ভাবে lowercase, tokenize এবং vocabulary build করে দেয়। `fit_transform()` প্রথমে vocabulary শেখে, তারপর count matrix বানায়। `transform()` নতুন text কে existing vocab অনুযায়ী encode করে। `ngram_range=(1,2)` দিলে শুধু single word না, পাশাপাশি 2-word phrase (bigram) ও vocab এ আসে — যা context কিছুটা ধরতে পারে।

সাধারণ ভুল

  • Word order হারিয়ে যায় — sentiment এ 'not good' vs 'good not' একই হয়ে যায়।
  • Vocab খুব বড় হলে sparse matrix বিশাল — memory সমস্যা।
  • নতুন document এ fit_transform call করা — পুরোনো model এর vocab নষ্ট হয়। শুধু transform use করুন।
  • Stop words না removed — common word 'the', 'is' কে বেশি weight দেয়।
  • Rare word এর শক্তি ignore করা — TF-IDF এ এটা সমাধান হয়।

অনুশীলন

  1. ৫টা movie review দিয়ে BoW matrix বানান। কোন word সবচেয়ে frequent?
  2. একই corpus এ unigram (1-word) এবং bigram (2-word) দুটো compare করুন।
  3. BoW দিয়ে document similarity বের করার চেষ্টা করুন — vector এর difference দিয়ে।

ছোট প্রজেক্ট

Mini Project: Mini Spam Detector

১০টা spam SMS এবং ১০টা ham (normal) SMS এর list বানান। CountVectorizer দিয়ে BoW feature বের করুন। sklearn এর MultinomialNB (Naive Bayes) model train করুন। তারপর একটা নতুন SMS দিয়ে predict করুন — spam নাকি ham। এটা world এর সবচেয়ে classic NLP application এর mini version।

সারাংশ

  • BoW = text কে word-count vector এ রূপান্তর।
  • সহজ, fast, interpretable — কিন্তু order এবং context হারায়।
  • Spam detection, classification এর starting point।
  • Improvement: TF-IDF (পরের chapter) — শুধু count না, importance ও measure করে।