Machine Learning & AI · Chapter 4.5 · Intermediate level

NLP Sentiment Analysis (Detect Human Mood from Text!)

NLP सेंटीमेंट एनालिसिस (शब्दों से इंसान का मूड पहचानना!)

YouTube and Amazon receive millions of comments daily. How does AI read a Hinglish or English sentence and feel whether the user is Happy (Positive 😊) or Angry (Negative 😡)? This branch of AI (which also powers ChatGPT & Gemini!) is called NLP (Natural Language Processing)! When you call nlp_model.fit(texts, moods), the AI learns which words carry positive energy (badhiya, maza) vs negative energy (bekar, boring, waste)!

YouTube, Instagram और Amazon पर रोज़ करोड़ों कमेंट्स आते हैं। AI कैसे समझता है कि कौन सा कमेंट तारीफ़ (Positive 😊) है और कौन सा नाराज़गी (Negative 😡)? इंसानी भाषा समझने वाली इस AI तकनीक को NLP (Natural Language Processing) कहते हैं (ChatGPT भी इसी परिवार का है)! nlp_model.fit(texts, moods) से AI अच्छे और बुरे शब्दों की पहचान सीख लेता है!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

texts = [...]
List of example Hinglish/English sentences for training.
ट्रेनिंग के लिए दिए गए वाक्यों/कमेंट्स की लिस्ट।
moods = [...]
Emotion label ("Positive 😊" or "Negative 😡") for each sentence.
हर वाक्य का सही मूड ("Positive 😊" या "Negative 😡")।
nlp_model.fit(texts, moods)
Teaches the NLP AI which words express praise vs complaint.
AI को सिखाता है कि कौन से शब्द खुशी और कौन से गुस्सा दिखाते हैं।
nlp_model.predict(review)
Reads a new sentence and predicts its mood!
नए कमेंट को पढ़कर उसका मूड बताता है!

Ready example

texts  = ["Bahut badhiya course hai", "Maza aa gaya seekh kar", "Bahut bekar aur boring hai", "Time waste ho gaya"]
moods  = ["Positive 😊", "Positive 😊", "Negative 😡", "Negative 😡"]

nlp_model.fit(texts, moods)

review = "Maza aa gaya coding seekh kar"
print("Comment Mood:", nlp_model.predict(review))

What you will see: An animated Speedometer Emotion Gauge + a Live Comment Box where you can type ANY Hinglish/English comment and see detected words highlighted!

प्रिव्यू में स्पीडोमीटर इमोशन गेज (😡 ↔ 😐 ↔ 😊) और Live Comment Box खुलेगा जहाँ आप कोई भी वाक्य लिखकर AI से मूड पूछ सकते हैं!

Line by line

texts = [...] / moods = [...]
2 positive reviews and 2 negative reviews.
2 तारीफ़ वाले और 2 शिकायत वाले उदाहरण।
nlp_model.fit(texts, moods)
Builds the AI's emotion vocabulary from your examples.
उदाहरणों से शब्दों की भावना पहचानना सीखता है।
nlp_model.predict(review)
Spots 'maza' → predicts 'Positive 😊'!
'maza' शब्द पकड़कर 'Positive 😊' मूड बताता है!

Common mistake

✗ Wrong

nlp_model.predict(Bahut accha laga)

✓ Right

nlp_model.predict("Bahut accha laga")

Text comments passed to NLP models are strings, so always wrap them inside quotes "..."!

NLP मॉडल को दिया जाने वाला वाक्य एक स्ट्रिंग है, इसलिए उसे कोट्स "..." में बंद करना न भूलें!

Your task

Train nlp_model.fit(texts, moods) and print nlp_model.predict(review) — then test your own Hinglish comments in the Live Mood Meter!

nlp_model.fit(texts, moods) चलाओ और nlp_model.predict(review) प्रिंट करो — फिर दाईं ओर खुद के कमेंट्स टाइप करके टेस्ट करो!

  1. Keep texts and moods lists of equal length.
    texts और moods लिस्ट तैयार रखो।
  2. Train with nlp_model.fit(texts, moods).
    nlp_model.fit(texts, moods) चलाओ।
  3. Print nlp_model.predict(review) and click ▶ Run & Preview!
    nlp_model.predict(review) प्रिंट करके ▶ Run & Preview दबाओ!
Starter code for the task
texts  = ["Bahut badhiya course hai", "Maza aa gaya seekh kar", "Bahut bekar aur boring hai", "Time waste ho gaya"]
moods  = ["Positive 😊", "Positive 😊", "Negative 😡", "Negative 😡"]

# TODO 1: nlp_model.fit(texts, moods) se AI ko shabdon ka mood sikhao
# TODO 2: review = "Maza aa gaya coding seekh kar"
# TODO 3: print("Comment Mood:", nlp_model.predict(review))

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

What branch of AI enables computers to read human text and detect Happy/Angry sentiment?

AI की कौन-सी शाखा कंप्यूटर को इंसानी भाषा और कमेंट्स का मूड समझना सिखाती है?

  1. NLP (Natural Language Processing)
  2. CSS Grid
  3. SQL Joins
  4. 2D Physics
Show the answer

Answer: A (NLP (Natural Language Processing))

NLP (Natural Language Processing) powers sentiment analysis, translation, and chatbots!

NLP (Natural Language Processing) से ही AI इंसानी भाषा और भावनाएँ समझता है!

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.