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