রোডম্যাপ
PHASE 3 · অধ্যায় 14

টেক্সট ক্লাসিফিকেশন

Text Classification

Document গুলোকে category তে ভাগ করার system তৈরি।

ভূমিকা

Gmail কীভাবে জানে কোন email Promotions, কোনটা Social, কোনটা Primary? News website কীভাবে নিজে নিজে article কে Sports/Politics/Tech এ ভাগ করে? এর পেছনে একটাই magic — Text Classification — NLP এর সবচেয়ে practical task।

ধারণা

Text Classification হলো একটি supervised learning task যেখানে input হিসেবে একটা text দেওয়া হয় এবং model সেটাকে একটা predefined category/label এ assign করে। Binary (spam/not-spam), Multi-class (sports/politics/tech), অথবা Multi-label (একটা article একসাথে tech ও business হতে পারে) — তিন ধরনের হতে পারে।

সহজ ব্যাখ্যা

ভাবুন একজন librarian কে — হাজার হাজার বই দেওয়া হলো, তাকে subject অনুযায়ী shelf এ রাখতে হবে। শুরুতে কিছু labeled example দেখিয়ে দিলে (এটা history, এটা science), পরে সে নিজেই নতুন বই দেখে guess করতে পারবে। Text classifier ও তাই — labeled data দিয়ে pattern শেখে, নতুন text এ apply করে।

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

  • Gmail tab classification (Primary/Social/Promotions)।
  • News categorization — BBC, Prothom Alo এর auto-tagging।
  • Customer support ticket routing — কোন team এ যাবে।
  • Language detection — Google Translate এর first step।
  • Toxic comment detection — Facebook, YouTube এর moderation।

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

1
Step 1 — Labeled dataset
প্রতিটা text এর সাথে তার category label থাকতে হবে।
2
Step 2 — Text কে vector এ
TF-IDF বা embedding দিয়ে numeric form।
3
Step 3 — Train/Test split
80% train, 20% test — model evaluation এর জন্য।
4
Step 4 — Classifier train
Logistic Regression, Naive Bayes, SVM, বা Neural Net।
5
Step 5 — Evaluate
Accuracy, Precision, Recall, F1-score দিয়ে measure।
6
Step 6 — Predict
নতুন text এ category predict করুন।

Python কোড

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

texts = [
    "The team won the championship final last night",
    "New smartphone launched with AI camera features",
    "Parliament passed the new economic reform bill",
    "Striker scored a hat-trick in the derby match",
    "Latest laptop with M3 chip released today",
    "President signed the trade agreement yesterday",
]
labels = ["sports", "tech", "politics", "sports", "tech", "politics"]

X_train, X_test, y_train, y_test = train_test_split(
    texts, labels, test_size=0.34, random_state=42
)

vectorizer = TfidfVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)

model = LogisticRegression()
model.fit(X_train_vec, y_train)

predictions = model.predict(X_test_vec)
print(classification_report(y_test, predictions))

new_text = ["Apple released a new iPhone with neural engine"]
new_vec = vectorizer.transform(new_text)
print("Predicted:", model.predict(new_vec)[0])
ব্যাখ্যা

TfidfVectorizer text কে numeric vector এ রূপান্তর করে। train_test_split data কে দুই ভাগে ভাগ করে। LogisticRegression একটি simple কিন্তু powerful classifier। fit() train করে, predict() নতুন data তে prediction দেয়। classification_report precision, recall, f1-score দেখায়।

সাধারণ ভুল

  • Test data কে vectorizer দিয়ে fit করা — শুধু transform করতে হয়।
  • Imbalanced dataset (একটা class বেশি) — accuracy misleading হয়।
  • Train এ থাকা data test এ থাকলে — data leakage।
  • শুধু accuracy দেখা — F1, confusion matrix ও জরুরি।

অনুশীলন

  1. BBC News dataset দিয়ে 5-class classifier বানান।
  2. Naive Bayes vs Logistic Regression compare করুন।
  3. Confusion matrix plot করুন seaborn দিয়ে।
  4. নিজের 100টা label করা bangla text এ classifier train করুন।

ছোট প্রজেক্ট

News Category Classifier

BBC বা Kaggle থেকে news dataset নিয়ে একটি classifier বানান যা নতুন article দিলে category (sports/tech/politics/business/entertainment) predict করে। TF-IDF + LogisticRegression দিয়ে শুরু করুন, তারপর accuracy improve করার চেষ্টা করুন।

সারাংশ

  • Text Classification = labeled text এ category prediction।
  • Pipeline: Text → Vector → Classifier → Label।
  • Binary, Multi-class, Multi-label — তিন ধরন।
  • TF-IDF + LogisticRegression = strong baseline।
  • Evaluation metrics: Accuracy, Precision, Recall, F1।