Class 10 AI Unit 6: Natural Language Processing (NLP) – Notes, Bag of Words, TF-IDF | CBSE Code 417
Why human language is hard for machines, stages of NLP, chatbots, text normalisation, Bag of Words and TF-IDF, explained step by step.
- Why is human language hard for computers?
- Applications of NLP
- Stages of NLP
- Chatbots: script bots vs smart bots
- Text normalisation
- Bag of Words and TF-IDF
- Sentiment analysis in Orange
Your friend breaks your pen and you say, "Nice. Very nice." Everyone around you knows you're annoyed. But a computer reading those words sees two happy words! Human language is full of tone, sarcasm, slang and double meanings. Natural Language Processing (NLP) is the branch of AI that tries to make sense of all this.
1. What is NLP?
NLP is the domain of AI that enables computers to understand, interpret and respond to human language, in both text and speech.
Why natural language is tricky
- Same words, different meanings: "I saw her bat" — a cricket bat or the animal?
- Perfect grammar, no sense: "Chickens feed extravagantly while the moon drinks tea." Grammatically fine, meaningless.
- Different word order, different meaning: "Dog bites man" vs "Man bites dog".
- Context and tone: sarcasm, idioms ("break a leg"), slang and mixed languages (Hinglish!).
2. Applications of NLP
Voice assistants
Alexa, Siri, Google Assistant understand spoken commands.
Machine translation
Google Translate converts Hindi to English and more.
Sentiment analysis
Finds if reviews/tweets are positive, negative or neutral.
Text classification
Spam filters sort emails; news apps sort articles by topic.
Keyword extraction
Pulls out the most important words from a long text.
Captions & summaries
YouTube auto-captions; apps that summarise long articles.
3. Stages of NLP
| Stage | What happens | Example |
|---|---|---|
| Lexical analysis | Breaks text into words/tokens and identifies their structure. | "I love AI" → [I] [love] [AI] |
| Syntactic analysis (parsing) | Checks grammar and word arrangement. | "We are to the zoo going tomorrow" ✘ → "We are going to the zoo tomorrow" ✔ |
| Semantic analysis | Checks whether the sentence makes sense (meaning). | "Hot ice cream" is grammatical but odd in meaning. |
| Discourse integration | Connects the meaning of a sentence with previous ones. | "Riya won. She was happy." → "she" = Riya |
| Pragmatic analysis | Understands the real intended meaning in context. | "Can you pass the salt?" is a request, not a yes/no question. |
4. Chatbots
A chatbot is a program that chats with humans in natural language. The handbook asks you to try chatbots like Elizabot, Mitsuku, Cleverbot and Singtel and compare them.
| Script bot | Smart bot |
|---|---|
| Works on a fixed script / pre-written replies. | Uses AI and NLP; flexible and learns from data. |
| Easy to make; little or no coding. | Complex to build; needs lots of data and coding. |
| Limited functions; gets stuck on unexpected questions. | Handles a wide range of questions and improves over time. |
| Example: customer-care bot with menu options, a bank's FAQ bot. | Example: Alexa, Siri, Google Assistant, ChatGPT. |
5. Text Normalisation
Before a computer can count words, we clean up the text. All the text from all documents together is called the corpus.
| Step | What it does |
|---|---|
| Sentence segmentation | Splits the corpus into sentences. |
| Tokenisation | Splits sentences into tokens (words, numbers, symbols). |
| Removing stopwords, special characters, numbers | Removes very common words (a, an, the, is, and) and symbols that add little meaning. |
| Converting to common case | Makes everything lowercase so "Hello" and "hello" count as one word. |
| Stemming | Chops off word endings. Fast, but the result may not be a real word: "studies" → "studi". |
| Lemmatisation | Reduces to the proper dictionary word (lemma): "studies" → "study". Slower but meaningful. |
6. Bag of Words (BoW)
Bag of Words counts how many times each word appears in each document, ignoring grammar and order. Just like tipping all the words into a bag and counting them.
Steps: 1) Text normalisation → 2) Create the dictionary (all unique words) → 3) Create document vectors (count of each word per document) → 4) Create document vectors for all documents.
D1: "Aman and Anil are stressed" · D2: "Aman went to a therapist" · D3: "Anil went to download a health chatbot"
| Doc | aman | and | anil | are | stressed | went | to | a | therapist | download | health | chatbot |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| D1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| D2 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| D3 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 |
| DF | 2 | 1 | 2 | 1 | 1 | 2 | 2 | 2 | 1 | 1 | 1 | 1 |
The dictionary has 12 unique words. Each row is a document vector. DF (document frequency) = in how many documents the word appears.
Try it yourself
Edit the three documents and press Build. The lab lowercases the text, removes punctuation and (optionally) stopwords, then builds the dictionary, document vectors and TF-IDF (log base 10).
TF-IDF: which words really matter?
Words like "to" and "a" appear everywhere, so they tell us little. Rare words like "stressed" or "therapist" say a lot about a document. TF-IDF gives more weight to such words.
| Term | Meaning / formula |
|---|---|
| TF (Term Frequency) | How many times a word appears in a document. |
| DF (Document Frequency) | Number of documents in which the word appears. |
| IDF (Inverse Document Frequency) | log(N ÷ DF), where N = total number of documents. |
| TF-IDF | TF × log(N ÷ DF) |
"aman" in D1: TF = 1, DF = 2 → 1 × log(3/2) = 1 × 0.176 = 0.176
"stressed" in D1: TF = 1, DF = 1 → 1 × log(3/1) = 1 × 0.477 = 0.477
"stressed" scores higher, so it's more important for D1. That matches common sense!
A word that appears in all documents gets IDF = log(1) = 0, so its TF-IDF is 0. That's why stopwords end up with the lowest value. Use log base 10 in board answers.
7. Sentiment Analysis with Orange (Practical)
In the practical, you'll use Orange's Text Mining add-on on a dataset of reviews:
Corpus (load text) → Preprocess Text (normalise) → Sentiment Analysis (score each review) → Data Table / Word Cloud (see results). Positive reviews get positive scores, negative reviews negative scores.
Quick Check
Q1. "We are to the zoo going tomorrow" — which stage catches this error?
Syntactic analysis (grammar/word order).
Q2. "studies" → "studi" is stemming or lemmatisation?
Stemming.
Q3. What is the IDF of a word that appears in all 4 documents?
log(4/4) = log 1 = 0.
Q4. Is Alexa a script bot or smart bot?
Smart bot.
⚡ One-minute revision
- NLP = AI that understands human language (text and speech).
- Stages: Lexical → Syntactic → Semantic → Discourse → Pragmatic.
- Script bot = fixed script; smart bot = AI-powered and flexible.
- Normalisation: segmentation → tokenisation → remove stopwords → common case → stemming/lemmatisation.
- BoW: dictionary + document vectors. TF-IDF = TF × log(N/DF).
Done reading? Test yourself on AI Unit 6 in board-exam style.
๐ Take the Practice PaperNext: AI Unit 7 ›๐ Class 10 Hub
Comments
Post a Comment