Showing posts with label Text Processing. Show all posts
Showing posts with label Text Processing. Show all posts

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

 


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'll 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).



Featured Post

Extra Challenge: Using References Between Documents

  ๐ŸŽฏ ๐Ÿ’ก Extra Challenge: Using References Between Documents Here's an  Extra Challenge  designed to build on the original MongoDB mode...

Popular Posts