Chapter 27
টেক্সট প্রসেসিং
Text Processing
🎬 কাঁচা টেক্সট থেকে মডেল-ready
কম্পিউটার সরাসরি ভাষা বোঝে না — সংখ্যা বোঝে। Text Processing হলো সেই সেতু: cleaning, tokenization, normalization, encoding — যাতে কাঁচা শব্দ মডেলের ইনপুট হতে পারে।
মূল ধাপগুলো
- Cleaning — HTML, URL, emoji, ducplicate ছাঁকা।
- Normalization — lower-case, unicode NFC।
- Tokenization — word, subword, character।
- Stopword / Stemming / Lemmatization — classical NLP।
- Encoding — BoW, TF-IDF, embedding ids।
Cleaning উদাহরণ
import re, unicodedata
def clean(text):
text = unicodedata.normalize("NFC", text)
text = re.sub(r"http\S+", " ", text) # URL
text = re.sub(r"<.*?>", " ", text) # HTML
text = re.sub(r"[^\w\s\u0980-\u09FF]", " ", text) # বাংলা+ASCII
text = re.sub(r"\s+", " ", text).strip().lower()
return textTokenization
Word tokenization
from nltk.tokenize import word_tokenize
print(word_tokenize("আমি ডিপ লার্নিং শিখছি।"))Subword — BPE / WordPiece / SentencePiece
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("bert-base-multilingual-cased")
print(tok.tokenize("আমি ট্রান্সফরমার ভালোবাসি"))
# ['আমি', 'ট্রান', '##স', '##ফরম', '##ার', 'ভাল', '##োবাসি']Stopword, Stemming, Lemmatization
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer, WordNetLemmatizer
sw = set(stopwords.words("english"))
ps, wl = PorterStemmer(), WordNetLemmatizer()
words = ["running", "studies", "better"]
print([ps.stem(w) for w in words]) # run, studi, better
print([wl.lemmatize(w, pos="v") for w in words]) # run, study, betterEncoding
Bag of Words
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X = vec.fit_transform(["i love nlp", "nlp is fun"])TF-IDF
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(ngram_range=(1,2), min_df=2)
X = tfidf.fit_transform(corpus)Vocab → IDs (Deep model-এর জন্য)
from collections import Counter
tokens = text.split()
vocab = {w:i+2 for i,(w,_) in enumerate(Counter(tokens).most_common(20000))}
vocab["<pad>"], vocab["<unk>"] = 0, 1
ids = [vocab.get(w, 1) for w in tokens]🔑 আধুনিক practice
আজকাল প্রায়শই raw text → pretrained tokenizer (BPE/WordPiece) → directly model। Classical stemming/stopword removal LLM যুগে কম প্রাসঙ্গিক, কিন্তু search/classical ML-এ এখনও কার্যকর।
বাংলা-নির্দিষ্ট চ্যালেঞ্জ
- Unicode normalization (NFC) আবশ্যক।
- Compound character — sentence-piece tokenizer ভালো।
- সীমিত labeled data → multilingual model (XLM-R, mBERT)।
অনুশীলন
১. একটি বাংলা নিউজ corpus clean ও tokenize করুন।
২. TF-IDF দিয়ে শীর্ষ-২০ keyword বের করুন।
৩. BPE বনাম word tokenization-এ vocab size তুলনা করুন।
সারসংক্ষেপ
✨ এই অধ্যায়ে যা শিখলাম
- Clean → Normalize → Tokenize → Encode।
- Subword tokenization আধুনিক ডিফল্ট।
- TF-IDF classical, embeddings deep।