PART 4 • DEEP & GENERATIVE AI • LEVEL 20

Natural Language Processing

Turn human language into computable evidence. Trace tokenization, construct TF-IDF vectors, measure semantic similarity and design reliable classification and extraction systems.

⏱️ 380–470 min🎯 Beginner → Interview Ready🧪 2 Computational Labs💼 NLP Engineering Focus
“clear examplesbuild confidence”clearexamplesbuildconfidenceTFIDFSEMTEXT → TOKENS → VECTORS → DECISION
LANGUAGE CONTRACTpreserve meaning • expose evidence • evaluate the tasktext → x ∈ ℝᵈ → model(x)

By the End of This Level, You Can

01Explain why language is ambiguous, contextual and structurally different from ordinary tabular data.
02Normalize, tokenize and build word, subword and n-gram representations.
03Calculate Bag of Words, TF-IDF, cosine similarity and Naive Bayes predictions.
04Explain static embeddings, contextual representations and sequence labelling.
05Select metrics for classification, extraction, generation and retrieval tasks.
06Design leakage-safe, multilingual and production-ready NLP experiments.

Six Building Blocks of Language Learning

NLP connects raw symbols to representations, context, tasks and measurable decisions.

CORPUSCollection of language

Documents, sentences or conversations sampled from the environment where the system will operate.

TOKENModel input unit

A word, subword, character or byte produced by a defined tokenizer.

VOCABULARYKnown token inventory

A mapping from token strings to numerical identifiers or vector dimensions.

CONTEXTMeaning around a token

Nearby words, sentence structure, document topic and conversational history change interpretation.

REPRESENTATIONNumbers for language

Sparse counts, TF-IDF values or dense embeddings expose evidence to a model.

TASKRequired language decision

Classification, extraction, retrieval, translation, summarization or generation defines the output and metric.

Language Is Symbolic, Ambiguous and Context Dependent

The same characters can express different meanings, while different sentences can communicate the same intent.

LEXICAL AMBIGUITYOne form, several senses

“Bank” can mean a financial institution or the side of a river.

SYNTACTIC AMBIGUITYSeveral valid structures

“I saw the student with a telescope” does not state who owns the telescope.

PRAGMATICSSituation changes intent

“Can you open the window?” is grammatically a question but functionally a request.

COMPOSITIONOrder changes meaning

“Dog bites man” and “man bites dog” contain the same words but describe different events.

DETAILED EXPLANATION

Natural language contains morphology, syntax, semantics, discourse and pragmatics. Characters form tokens; tokens participate in phrases and sentences; references can cross sentence boundaries; speaker intent depends on shared knowledge. An NLP model never observes meaning directly. It learns statistical relationships between language inputs and task outputs from a corpus. Therefore the data definition, annotation rules, tokenizer and evaluation slices are part of the model—not merely preprocessing details.

WORKED INTUITION

In “This phone is sick,” the word “sick” may be negative in medical text but positive slang in a product conversation. Domain and surrounding context determine the useful interpretation.

AI / PLACEMENT CONNECTION

Begin an NLP system-design answer by defining the document unit, language, domain, output task and latency requirement.

COMMON MISCONCEPTION

Text is not clean simply because it is readable. Encoding, Unicode variants, repeated text, templates, annotation disagreement and hidden metadata can dominate results.

Normalization and Tokenization Define What the Model Can See

Every cleaning rule can remove noise, destroy meaning or change the vocabulary.

1Decode

Read text using the correct encoding and Unicode policy.

2Normalize

Apply justified case, whitespace and punctuation rules.

3Tokenize

Create words, subwords, characters or bytes.

4Map

Convert tokens into stable vocabulary IDs.

5Mask

Pad batches while preventing padding from affecting the model.

DETAILED EXPLANATION

Whitespace tokenization is easy but fails for punctuation-rich text and languages without spaces. Word tokenizers preserve readable units but create large vocabularies and out-of-vocabulary words. Subword methods such as BPE and WordPiece learn reusable fragments, balancing vocabulary size with sequence length. Character and byte models remove unknown-token problems but produce longer sequences. Lowercasing, stemming, lemmatization, stop-word removal and punctuation deletion are task-dependent decisions: negation, casing, emoji and hashtags may be essential evidence.

WORKED INTUITION

“unhelpful” may be unseen as a whole word but represented using familiar fragments such as “un”, “help” and “ful”.

AI / PLACEMENT CONNECTION

State whether the tokenizer is trained only on training data, versioned with the model and identical during serving.

COMMON MISCONCEPTION

Removing every stop word can reverse sentiment: deleting “not” from “not useful” leaves the opposite evidence.

PREMIUM COMPUTATIONAL VISUALIZER

🧮 Tokenization & Vector-Space Workbench

Process a real mini-corpus from raw text to normalized tokens, vocabulary, document vectors and cosine similarity. Every displayed number is calculated from the selected corpus.

CodeBhavya • Normalize, Count, Compare
PHASEReady
DOCUMENT
TOKENS
VOCABULARY
BEST COSINE

Sparse Vectors Preserve Explicit Lexical Evidence

Bag of Words and TF-IDF ignore much structure, yet remain fast, interpretable and difficult to beat on many small-data tasks.

BINARY BOWxⱼ ∈ {0,1}Token presence

Useful when repetition should not increase evidence.

COUNT VECTORxⱼ = count(tⱼ,d)Token frequency

Preserves repeated occurrences but favours long documents.

TF-IDFtf(t,d) × log((N+1)/(df+1))Local importance, global rarity

Downweights tokens appearing across many documents.

COSINE(a·b)/(‖a‖‖b‖)Direction similarity

Compares lexical profiles while reducing document-length effects.

DETAILED EXPLANATION

A vocabulary gives every feature a stable coordinate. Unigrams capture individual tokens; n-grams add local order such as “not useful” but increase dimensionality and sparsity. TF-IDF multiplies within-document frequency by inverse document frequency. Exact library formulas differ in smoothing, normalization and sublinear term frequency, so save vectorizer settings with the model. Sparse matrices store only nonzero entries and pair naturally with linear classifiers.

WORKED INTUITION

If “course” occurs in every review, it identifies the domain but contributes little discrimination. “confusing” appearing in one review receives higher IDF and can be strong negative evidence.

AI / PLACEMENT CONNECTION

For limited labelled text, compare word and character TF-IDF with Logistic Regression or Linear SVM before choosing a deep model.

COMMON MISCONCEPTION

TF-IDF does not understand meaning. Synonyms occupy different dimensions unless training examples connect them through the downstream model.

Classical NLP Models Turn Token Evidence into Decisions

Simple models expose their assumptions clearly and provide strong production baselines.

MULTINOMIAL NAIVE BAYES
  1. Count documents and tokens per class.
  2. Estimate class priors.
  3. Apply Laplace smoothing to token likelihoods.
  4. Add log probabilities for query tokens.
  5. Choose the class with maximum log score.
LOGISTIC REGRESSION
  1. Construct sparse document vectors.
  2. Calculate a weighted linear score.
  3. Map the score through sigmoid or softmax.
  4. Optimize regularized cross-entropy.
  5. Calibrate and tune the decision threshold.
LINEAR SVM
  1. Represent documents in sparse space.
  2. Find a maximum-margin separator.
  3. Penalize margin violations using C.
  4. Use class weights when appropriate.
  5. Calibrate scores if probabilities are required.
CHARACTER N-GRAM MODEL
  1. Create overlapping character fragments.
  2. Build sparse counts or TF-IDF.
  3. Train a regularized linear model.
  4. Capture spelling and morphology variation.
  5. Evaluate by language and text quality.
DETAILED EXPLANATION

Naive Bayes assumes conditional independence of tokens given the class, an unrealistic assumption that still produces useful estimates in high-dimensional text. Logistic Regression learns discriminative feature weights, while Linear SVM optimizes margin and often performs strongly with TF-IDF. Character n-grams are robust to misspellings and helpful for language identification, toxicity and morphologically rich languages. Use pipelines so vectorization is fitted within each training fold rather than on the full dataset.

WORKED INTUITION

A spam classifier may assign large positive weight to “free offer” and negative weight to “meeting schedule”; bigrams distinguish “not selected” from “selected”.

AI / PLACEMENT CONNECTION

Be ready to derive Laplace-smoothed Naive Bayes in log space and explain why multiplying many probabilities underflows.

COMMON MISCONCEPTION

High accuracy can hide failure on a rare intent. Inspect per-class precision, recall, confusion and threshold-dependent costs.

Embeddings Learn Distributed Semantic Representations

Dense vectors place tokens or documents in a geometry where useful relationships can become measurable.

ONE-HOT[0,0,1,0,…]

Unique identity; no learned similarity.

CO-OCCURRENCEM[word, context]

Meaning estimated from surrounding tokens.

DENSE EMBEDDINGe(word) ∈ ℝᵈ

Compressed distributed features.

CONTEXTUAL VECTORh(token | sentence)

The same token changes with its sentence.

DETAILED EXPLANATION

The distributional hypothesis says words used in similar contexts tend to have related meanings. Count-based methods construct a word–context matrix and may transform it with PPMI before dimensionality reduction. Word2Vec learns embeddings using predictive objectives such as skip-gram or CBOW; GloVe combines global co-occurrence statistics with a weighted factorization objective. These static methods assign one vector per token. Contextual encoders instead compute a vector from the complete input, allowing “bank” to differ across financial and river sentences.

WORKED INTUITION

If “Python” and “Java” frequently occur near “developer”, “code” and “application”, their context vectors become more similar than either is to an unrelated word.

AI / PLACEMENT CONNECTION

Explain the difference among one-hot identity, sparse TF-IDF evidence, static word embeddings and contextual token embeddings.

COMMON MISCONCEPTION

Cosine proximity is learned association, not guaranteed synonymy, truth or fairness. Embeddings can encode stereotypes present in the corpus.

PREMIUM COMPUTATIONAL VISUALIZER

🧭 Context Window & Semantic Geometry Laboratory

Build a word–context matrix from actual sentences, apply count or PPMI weighting, calculate cosine similarity and reveal data-derived semantic neighbours.

CodeBhavya • Count Context, Measure Meaning
PHASEReady
TARGETpython
CONTEXT LINKS
BEST NEIGHBOUR
COSINE

NLP Tasks Require Different Output Structures and Metrics

A model is useful only when its output, matching policy and evaluation reflect the real language decision.

TEXT CLASSIFICATION

Predict document or sentence labels. Use macro/micro F1, per-class recall, calibration and threshold cost.

SEQUENCE LABELLING

Predict a tag per token for NER or POS. Evaluate entity spans, not only individual token accuracy.

INFORMATION RETRIEVAL

Rank relevant text. Use Recall@K, MRR, nDCG and judgement quality.

MACHINE TRANSLATION

Generate target-language text. Combine automatic metrics with human adequacy and fluency review.

SUMMARIZATION

Produce concise faithful text. Measure overlap, semantic coverage, factual consistency and usefulness.

QUESTION ANSWERING

Return spans or generated answers. Use exact match/F1 plus groundedness and unanswerable-case evaluation.

DETAILED EXPLANATION

Token classification requires alignment between tokenizer subwords and human labels. Named entities should usually be evaluated as complete spans using schemes such as BIO. Retrieval metrics care about rank position, while generation metrics cannot fully capture factuality or usefulness. Establish an annotation guide, measure inter-annotator agreement, preserve difficult examples and evaluate slices by language, length, topic, demographic mention and input quality.

WORKED INTUITION

If the reference entity is “New York City,” predicting only “New York” may achieve good token accuracy but fails exact span extraction.

AI / PLACEMENT CONNECTION

Match each task to its output unit: document label, token tag, span, ranked list or generated sequence.

COMMON MISCONCEPTION

BLEU, ROUGE or embedding similarity alone cannot establish factual correctness or safe deployment.

Sequence Models Add Context Beyond Independent Tokens

Language meaning depends on order, long-range relationships and structured output constraints.

N-GRAM LANGUAGE MODELP(wₜ | wₜ₋ₙ₊₁…wₜ₋₁)Finite lexical context

Counts local sequences and requires smoothing for unseen n-grams.

RNN / LSTM / GRUhₜ = f(xₜ,hₜ₋₁)Recurrent learned memory

Processes tokens sequentially using shared parameters.

CRFP(y₁…yₙ | x)Structured label sequence

Scores transitions so neighbouring output tags remain compatible.

ATTENTION BRIDGEcontext = ΣαᵢvᵢSelective access

Lets a representation use relevant positions directly—the foundation for Level 21.

DETAILED EXPLANATION

N-gram models estimate next-token probabilities from limited history and need additive, backoff or interpolation smoothing. Recurrent networks compress earlier inputs into a hidden state, while bidirectional encoders use left and right context when the complete sentence is available. CRFs are useful when output labels obey transition rules, such as an inside-entity tag following a valid beginning. Attention reduces the distance between related positions and leads directly to transformer architectures.

WORKED INTUITION

In named-entity recognition, a CRF can discourage an I-ORG tag immediately after O unless a valid B-ORG begins the organization span.

AI / PLACEMENT CONNECTION

Distinguish causal language modelling, masked encoding and bidirectional sequence labelling by what context is legally visible.

COMMON MISCONCEPTION

A bidirectional encoder is valid for classifying a completed sentence but leaks future tokens in left-to-right generation.

Reliable NLP Depends on Data Boundaries, Languages and Human Impact

Text models often learn source, author and annotation shortcuts that disappear after deployment.

GROUPED SPLITS

Keep the same user, thread, template or near-duplicate document inside one split.

TIME SPLITS

Use chronological evaluation when topics, vocabulary or policy change over time.

POINT-IN-TIME FEATURES

Exclude moderator outcomes, future replies and metadata unavailable when prediction occurs.

MULTILINGUAL SLICES

Report performance by language, script, code-mixing, dialect and transliteration pattern.

PRIVACY

Minimize personal text, control retention, redact identifiers and restrict diagnostic logs.

HUMAN REVIEW

Define confidence-based escalation, appeal and override for consequential language decisions.

DETAILED EXPLANATION

Near-duplicate templates can cross random splits and make memorization look like generalization. A support-ticket classifier may learn agent signatures instead of customer intent; a toxicity model may associate identity terms with harmful labels; a multilingual model may perform well overall while failing a smaller language. Evaluation must mirror deployment and include abstention, confidence calibration, drift monitoring, privacy review and a documented response when the model is uncertain or harmful.

WORKED INTUITION

If messages from one conversation appear in both train and test, repeated phrases and quoted replies leak the answer even when no row is identical.

AI / PLACEMENT CONNECTION

For system design, connect model errors to workflow cost: false routing, missed abuse, delayed support or unfair moderation.

COMMON MISCONCEPTION

Removing explicit names does not guarantee anonymity; rare phrases, locations and metadata can still identify a person.

PROGRAM TRACING • TRUE NESTED-LOOP EXECUTION

Trace Multinomial Naive Bayes from First Principles

Follow class priors, every training document, token counts, Laplace-smoothed query likelihoods and final log-score comparison. The cursor returns through each loop exactly as Python executes.

NLP Logic Before Framework Calls

Use these compact procedure maps for revision, coding rounds and interviews.

TF-IDF PIPELINE
  1. Fit normalization and vocabulary on training text.
  2. Count term frequency per document.
  3. Count document frequency per token.
  4. Calculate smoothed inverse document frequency.
  5. Multiply, normalize and transform validation consistently.
NAIVE BAYES
  1. Estimate class priors from training labels.
  2. Count token occurrences per class.
  3. Add smoothing to numerator and vocabulary denominator.
  4. Add log prior and query-token log likelihoods.
  5. Select the class with the greatest total log score.
SKIP-GRAM TRAINING
  1. Select a centre token and context window.
  2. Create positive centre–context pairs.
  3. Sample unrelated negative context tokens.
  4. Score pairs using embedding dot products.
  5. Update vectors to separate positive and negative pairs.
NER PIPELINE
  1. Define entity schema and annotation rules.
  2. Tokenize while retaining label alignment.
  3. Encode contextual token representations.
  4. Predict token or structured sequence labels.
  5. Reconstruct spans and measure entity-level F1.

💻 Natural Language Processing Challenges

Attempt each program independently. Workspaces, hints and model programs remain collapsed initially.

0 / 5Solved independently0 / 500Best score

Test Your Language-Model Reasoning

Select one answer per question. Results show your choice, the correct answer and a clear explanation.

Not checked yet

Diagnose NLP Systems Like an ML Engineer

Use evidence from data, representations, evaluation slices and deployment constraints before changing the architecture.

VALIDATION TOO GOOD?

Check duplicate templates, author overlap, thread leakage, vectorizer fitting and label-revealing metadata.

RARE CLASS MISSED?

Inspect annotation volume, class weights, thresholds, macro F1 and hard-negative examples.

UNKNOWN WORDS?

Compare word, subword, character or byte tokenization and inspect vocabulary coverage.

MULTILINGUAL FAILURE?

Measure each language and script; inspect transliteration, code-mixing and tokenizer fragmentation.

SEMANTIC DRIFT?

Track new terms, topic distribution, embedding neighbours, confidence and human disagreement.

UNSAFE OUTPUT?

Add confidence-based abstention, policy filters, human review, appeals and diagnostic logging controls.

CodeBhavya interview pattern:Define language task → Specify document and label contract → Audit corpus and annotation → Split groups safely → Establish sparse baseline → Choose representation and model → Evaluate task and slices → Inspect errors → Design abstention and monitoring → Connect metrics to human impact.

🎤 Natural Language Processing — Interview Questions

Answer aloud before selecting Show Answer for each explanation.

An NLP Model Is a Language Decision Pipeline

1Define

Specify language, unit and task.

2Tokenize

Preserve useful linguistic evidence.

3Represent

Build sparse or dense vectors.

4Predict

Choose a task-aligned model.

5Verify

Evaluate slices and human impact.

Natural language processing becomes trustworthy when the corpus, tokenizer, representation, model evidence, evaluation unit and failure-handling policy can all be explained.

Eight Practical NLP Habits

01

Inspect raw Unicode text, language distribution and repeated templates before cleaning.

02

Keep normalization conservative until task evidence justifies removing information.

03

Measure token lengths and unknown or fragmented words by language and domain.

04

Build word and character TF-IDF linear baselines before expensive fine-tuning.

05

Fit tokenizers and vectorizers only on training data within each evaluation fold.

06

Read false positives and false negatives; text errors reveal annotation and shortcut problems.

07

Evaluate confidence, abstention and calibration when predictions trigger actions.

08

Version the tokenizer, vocabulary, labels, normalization and maximum-length policy with the model.

Strengthen NLP Reasoning

Calculate intermediate representations and defend every modelling decision.

  1. 01

    Compare word, subword, character and byte tokenization for Telugu–English code-mixed text.

  2. 02

    Explain when lowercasing changes task evidence.

  3. 03

    Create unigram and bigram vocabularies for three short documents.

  4. 04

    Calculate TF, document frequency and smoothed IDF by hand.

  5. 05

    Calculate cosine similarity for two sparse document vectors.

  6. 06

    Derive one Laplace-smoothed Naive Bayes prediction.

  7. 07

    Explain why log probabilities are used in Naive Bayes.

  8. 08

    Compare Logistic Regression and Linear SVM for TF-IDF text.

  9. 09

    Construct centre–context pairs for skip-gram training.

  10. 10

    Calculate one PPMI word–context value.

  11. 11

    Convert BIO tags into named-entity spans.

  12. 12

    Select metrics for imbalanced intent classification.

  13. 13

    Identify five leakage paths in support-ticket text.

  14. 14

    Design multilingual monitoring for a production moderation model.