NLP Basics
Tokenization, TF-IDF, sentiment analysis — turn raw text into numbers a model can learn from.
Flipkart processes 2 million product reviews per day. A human reading all of them? Impossible. An NLP pipeline classifies each review as positive/negative/neutral in real time, feeds the insight to sellers automatically.
Every chatbot, email classifier, autocomplete, and LLM is built on the same foundational NLP primitives you'll learn today.
📈 NLP Pipeline (5–25 min)
Tokenize — split text into words/tokens.
Clean — lowercase, remove punctuation, stop words.
Vectorize — TF-IDF, Bag-of-Words, or embeddings → numeric matrix.
Model — feed matrix to classifier (Naive Bayes, Logistic Regression, NN).
💻 Live Demo — Review Sentiment Classifier (25–45 min)
>import re from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report # Sample product reviews reviews = [ "Excellent product, very happy with the quality!", "Worst purchase ever. Broke in 2 days.", "Good value for money, works as expected.", "Terrible quality, do not buy this.", "Amazing! Fast delivery and perfect condition.", "Disappointed. Not as described in listing.", "Superb build quality, highly recommend!", "Stopped working after a week. Waste of money.", "Average product. Nothing special about it.", "Brilliant! Exactly what I needed. Very satisfied.", ] labels = [1, 0, 1, 0, 1, 0, 1, 0, 0, 1] # 1=positive, 0=negative # Clean text def clean(text): text = text.lower() text = re.sub(r"[^a-z\s]", "" " , text) return text clean_reviews = [clean(r) for r in reviews] # TF-IDF: how important is a word to this doc vs the corpus? vectorizer = TfidfVectorizer(stop_words="english", max_features=500) X = vectorizer.fit_transform(clean_reviews) X_tr, X_te, y_tr, y_te = train_test_split(X, labels, test_size=0.3, random_state=42) clf = LogisticRegression() clf.fit(X_tr, y_tr) print(classification_report(y_te, clf.predict(X_te))) # Predict new review new = ["Really bad experience, total waste of money!"] vec = vectorizer.transform(new) print(f"Sentiment: {'Positive' if clf.predict(vec)[0]==1 else 'Negative'}" )
📋 You Code It (45–55 min)
Add 5 more reviews to the dataset (mix of positive/negative). Print the top 10 TF-IDF features by weight using vectorizer.get_feature_names_out() and clf.coef_. Which words most indicate spam?
⚡ Day 8 Cheat Sheet
TfidfVectorizer(stop_words="english")vectorizer.fit_transform(texts)vectorizer.transform(new_texts)vectorizer.get_feature_names_out()re.sub(r"[^a-z]", " ", text)text.lower()clf.coef_[0]np.argsort(coef)[-10:]