Named Entity Recognition
Text থেকে person, place, organization detect করা।
'Sadiqul Islam lives in Dhaka and works at Google since 2021.' — এই sentence থেকে আপনি instantly বের করতে পারেন: Person = Sadiqul Islam, Location = Dhaka, Organization = Google, Date = 2021। Computer কে এই 'instant extraction' শেখানোর কাজটাই NER।
Named Entity Recognition (NER) হলো text থেকে real-world entities — Person, Organization, Location, Date, Money, Percent ইত্যাদি — automatically identify এবং classify করার process। এটা Information Extraction এর foundation।
NER হলো একটা highlighter। Text এর উপর hover করলে সে আলাদা color এ mark করে — সবুজে person, নীলে place, হলুদে date। শুধু চেনা না — category ও বলে দেয়। ChatGPT যখন আপনার নাম মনে রাখে, বা Gmail যখন email থেকে automatic calendar event বানায় — সবই NER এর জাদু।
import spacy
# python -m spacy download en_core_web_sm
nlp = spacy.load("en_core_web_sm")
text = ("Sadiqul Islam lives in Dhaka and works at Google since 2021. "
"He earned 50000 dollars last year. Apple released iPhone 15 in September.")
doc = nlp(text)
print(f"{'Entity':<25}{'Label':<12}{'Meaning'}")
print("-" * 60)
for ent in doc.ents:
print(f"{ent.text:<25}{ent.label_:<12}{spacy.explain(ent.label_)}")
# Get only persons and organizations
persons = [ent.text for ent in doc.ents if ent.label_ == "PERSON"]
orgs = [ent.text for ent in doc.ents if ent.label_ == "ORG"]
print("\nPersons:", persons)
print("Organizations:", orgs)
# HuggingFace approach — modern transformer-based NER
# pip install transformers
from transformers import pipeline
ner_model = pipeline("ner", grouped_entities=True)
results = ner_model("Elon Musk founded SpaceX in California.")
for r in results:
print(r["word"], "->", r["entity_group"])spaCy এর pre-trained model 'en_core_web_sm' direct text থেকে entities extract করে। `doc.ents` সব detected entity দেয়, প্রতিটার `.text` (original word) এবং `.label_` (category) থাকে। Common labels: PERSON, ORG, GPE (country/city), DATE, MONEY, PERCENT। HuggingFace pipeline আরও powerful — BERT-based, কিন্তু slower। `grouped_entities=True` দিলে adjacent token গুলো merge করে দেয় (যেমন 'Elon' + 'Musk' = 'Elon Musk')।
একটা script লিখুন যেটা ৫টা news article (text file বা URL) থেকে সব entity extract করবে এবং একটা summary report দিবে: প্রতিটা article এ কতজন person mentioned, কতগুলো organization, কতগুলো location। Bonus: সবচেয়ে frequent person/org/location কে বের করুন — কোন news এ কে সবচেয়ে বেশি আলোচিত। এই তথ্য একটা JSON file এ save করুন।