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

টপিক মডেলিং

Topic Modeling

অনেক document থেকে hidden topics বের করে আনা।

ভূমিকা

আপনার কাছে ১০,০০০ news article আছে — কেউ label করে দেয়নি। আপনি জানতেও চান না exact category কী, শুধু জানতে চান এই collection এ মোটামুটি কোন কোন topic আছে। মানুষ পড়ে বের করা impossible — Topic Modeling এর কাজ এখানেই।

ধারণা

Topic Modeling একটি unsupervised technique যা label ছাড়া document collection থেকে hidden 'topics' automatically discover করে। সবচেয়ে famous algorithm: LDA (Latent Dirichlet Allocation)। প্রতিটা topic = কিছু word এর probability distribution। প্রতিটা document = কিছু topic এর mixture।

সহজ ব্যাখ্যা

ভাবুন আপনার কাছে অনেক recipe আছে। কেউ বলে দেয়নি কোনটা dessert, কোনটা curry। কিন্তু আপনি observe করেন: কিছু recipe এ 'sugar, chocolate, cream, vanilla' বারবার আসে — এটা একটা 'topic' (probably dessert)। কিছুতে 'chili, masala, onion, oil' — আরেকটা topic। LDA এই pattern statistically বের করে।

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

  • News aggregator — automatically topic-wise grouping।
  • Academic paper exploration — research trend বের করা।
  • Customer feedback analysis — কোন কোন issue বারবার আসছে।
  • Social media trend detection।
  • Legal document discovery — কোন case কোন topic এ।

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

1
Step 1 — Corpus তৈরি
Document collection — minimum 100+ document দরকার।
2
Step 2 — Preprocess
Tokenize, lowercase, stopword remove, lemmatize।
3
Step 3 — Vectorize
CountVectorizer (LDA এর জন্য — TF-IDF না)।
4
Step 4 — Choose number of topics
K = 5/10/20 — experiment করতে হবে।
5
Step 5 — LDA train
Each topic এর top words inspect করে label দিন।
6
Step 6 — Document-topic distribution
প্রতিটা document কোন topic এ কতটুকু — analyze।

Python কোড

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation

documents = [
    "The football team won the match with three goals",
    "Cricket world cup final was thrilling till last over",
    "New AI model achieves state of the art on NLP tasks",
    "Deep learning transformers changed natural language processing",
    "Government announced new tax policy for next year",
    "Parliament debated the economic reform bill yesterday",
    "Striker scored a hat-trick in the championship game",
    "Machine learning research at universities is booming",
    "Election commission released final voter list this week",
]

vectorizer = CountVectorizer(stop_words="english")
X = vectorizer.fit_transform(documents)

lda = LatentDirichletAllocation(n_components=3, random_state=42)
lda.fit(X)

feature_names = vectorizer.get_feature_names_out()
for topic_idx, topic in enumerate(lda.components_):
    top_words = [feature_names[i] for i in topic.argsort()[-5:][::-1]]
    print(f"Topic {topic_idx + 1}: {', '.join(top_words)}")

print("\nDocument topic distribution:")
doc_topics = lda.transform(X)
for i, dist in enumerate(doc_topics):
    dominant = dist.argmax() + 1
    print(f"Doc {i + 1} -> Topic {dominant} ({dist[dominant-1]:.2%})")
ব্যাখ্যা

CountVectorizer document গুলোকে word count matrix এ রূপান্তর করে। LatentDirichletAllocation n_components=3 মানে 3টা topic খুঁজবে। components_ এ প্রতিটা topic এর word probability। argsort()[-5:] top 5 word বের করে। transform() document-topic distribution দেয়।

সাধারণ ভুল

  • Number of topics (K) ভুল নির্বাচন — coherence score দিয়ে tune করুন।
  • TF-IDF use করা LDA তে — LDA count chai, frequency না।
  • Stopword remove না করা — 'the', 'a', 'is' সব topic এ dominate করবে।
  • Topic gulo মানুষের চোখে label করতে হয় — algorithm শুধু word cluster দেয়।

অনুশীলন

  1. 20 Newsgroups dataset এ LDA চালান, 20টা topic বের করুন।
  2. pyLDAvis দিয়ে interactive topic visualization বানান।
  3. Coherence score (gensim) দিয়ে best K খুঁজুন।
  4. NMF (Non-negative Matrix Factorization) compare করুন LDA এর সাথে।

ছোট প্রজেক্ট

News Topic Explorer

একটি script যা news article collection নেয়, LDA দিয়ে 10টা topic discover করে, প্রতিটা topic এর top 10 word print করে, এবং user কে নতুন article দিলে বলে এটা কোন topic এ পড়ে — সব unsupervised, কোনো manual label ছাড়া।

সারাংশ

  • Topic Modeling = unsupervised topic discovery।
  • LDA = most popular algorithm — topic = word distribution।
  • Document = mixture of topics।
  • Use CountVectorizer (not TF-IDF) with LDA।
  • Phase 3 complete! আপনি এখন classification, sentiment, spam, topic — সব ML-based NLP solve করতে পারেন।