POS Tagging
প্রতিটা word noun, verb, adjective কিনা চিহ্নিত করা।
'I saw her duck' — এই sentence এর 'duck' কী? পাখি (noun)? নাকি মাথা নিচু করা (verb)? Context ছাড়া বোঝা যায় না। Computer কে এই 'কী ধরনের word' বোঝানোর কাজটাই POS Tagging।
POS (Part-of-Speech) Tagging হলো একটা sentence এর প্রতিটা word কে তার grammatical category — noun, verb, adjective, adverb, pronoun, preposition ইত্যাদি — চিহ্নিত করার process। Modern POS tagger ML-based এবং context দেখে tag করে।
POS tag হলো word এর 'job title'। 'run' কখনো noun ('a morning run'), কখনো verb ('I run'). Tagger এর কাজ হলো surrounding context দেখে সঠিক job title দেওয়া। এটা NLP এর deep understanding এর প্রথম step — কারণ grammar বোঝা ছাড়া meaning বোঝা যায় না।
import nltk
from nltk.tokenize import word_tokenize
nltk.download("punkt", quiet=True)
nltk.download("punkt_tab", quiet=True)
nltk.download("averaged_perceptron_tagger_eng", quiet=True)
text = "Sadiq writes beautiful Bangla NLP tutorials every day."
tokens = word_tokenize(text)
# NLTK tagging
tagged = nltk.pos_tag(tokens)
for word, tag in tagged:
print(f"{word:<12} -> {tag}")
# Common Penn Treebank tags
print("\nCommon tags:")
print("NN = Noun, NNS = Plural Noun, NNP = Proper Noun")
print("VB = Verb, VBD = Past, VBG = -ing form")
print("JJ = Adjective, RB = Adverb, IN = Preposition")
# spaCy — more user-friendly tags
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("She quickly wrote a fantastic novel about AI.")
print("\nspaCy output:")
for token in doc:
print(f"{token.text:<10} POS={token.pos_:<6} TAG={token.tag_}")NLTK Penn Treebank tag set use করে — যেমন NNP (Proper Noun), VBZ (3rd person singular verb)। spaCy দুই ধরনের tag দেয় — coarse (NOUN, VERB — easy to read) এবং fine-grained (NN, VBZ — detailed)। দুটোই কাজে আসে। Modern application এ spaCy preferred কারণ এটা fast এবং accurate।
একটা function `grammar_profile(text)` লিখুন যেটা return করবে: total nouns, total verbs, total adjectives, total adverbs, এবং noun-to-verb ratio। এটা দিয়ে আপনি কোন writer descriptive (বেশি adjective) আর কোন writer action-oriented (বেশি verb) সেটা বুঝতে পারবেন। ২টা ভিন্ন article এ apply করে compare করুন।