Natural Language Processing with Python: Analyze and Understand Text Using NLP

← Back to Home

Part 6: Natural Language Processing (NLP) with Python



What Is Natural Language Processing (NLP)?

NLP is a branch of AI that helps computers understand, interpret, and generate human language. It powers:

  • Search engines
  • Translation apps
  • Chatbots
  • Voice assistants

Tools We shall Use

  • NLTK – Natural Language Toolkit
  • TextBlob – Simple text analysis
  • spaCy – Fast and industrial-strength NLP

Install them with:

pip install nltk textblob spacy
python -m textblob.download_corpora
python -m nltk.downloader punkt


✍️ Step-by-Step: Basic Text Analysis with TextBlob

✅ Step 1: Create a Simple Analyzer

from textblob import TextBlob

text = "Python is a powerful language for machine learning."
blob = TextBlob(text)

print("Words:", blob.words)
print("Sentences:", blob.sentences)


💬 Step 2: Sentiment Analysis

text = "I love working with Python, but debugging can be frustrating."
blob = TextBlob(text)
print(blob.sentiment)

Output:

Sentiment(polarity=0.25, subjectivity=0.6)
  • Polarity ranges from -1 (negative) to +1 (positive)
  • Subjectivity ranges from 0 (objective) to 1 (subjective)


Tokenization and Lemmatization with NLTK

import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer

nltk.download('punkt')
nltk.download('wordnet')

text = "Cats are running faster than the dogs."
tokens = word_tokenize(text)
lemmatizer = WordNetLemmatizer()

lemmas = [lemmatizer.lemmatize(token.lower()) for token in tokens]
print("Lemmatized Tokens:", lemmas)


Named Entity Recognition with spaCy

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple is looking at buying a startup in the UK for $1 billion.")

for entity in doc.ents:
    print(entity.text, "-", entity.label_)


Mini Project: Simple Sentiment Classifier

def get_sentiment(text):
    blob = TextBlob(text)
    polarity = blob.sentiment.polarity
    if polarity > 0:
        return "Positive"
    elif polarity < 0:
        return "Negative"
    else:
        return "Neutral"

print(get_sentiment("I love AI and machine learning!"))  # Positive
print(get_sentiment("I hate bugs in my code."))          # Negative


Practice Challenge

  1. Ask the user for input and return sentiment

  2. Try building a chatbot that responds based on detected sentiment

  3. Use spaCy to extract named entities from a paragraph



🎓 What You’ve Learned:

  • How to tokenize and lemmatize text
  • Perform sentiment analysis
  • Use NLP libraries like NLTK, TextBlob, and spaCy
  • Build a simple sentiment classifier


🧭 What’s Next?

In Part 7, we’ll tackle Computer Vision using OpenCV and Deep Learning. You’ll learn how to analyze and classify images using Convolutional Neural Networks (CNNs).