0% found this document useful (0 votes)
16 views38 pages

NLP Lab Practical

Natural Language Processing (NLP) combines computer science, linguistics, and machine learning to enable machines to understand and generate human language. The document outlines the NLP processing pipeline, challenges in language understanding, and various techniques such as tokenization, stop-word removal, stemming, and lemmatization. It also discusses applications of NLP including sentiment analysis, text classification, named entity recognition, and intent detection, emphasizing the importance of integrating NLP into applications for enhanced automation and user experience.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views38 pages

NLP Lab Practical

Natural Language Processing (NLP) combines computer science, linguistics, and machine learning to enable machines to understand and generate human language. The document outlines the NLP processing pipeline, challenges in language understanding, and various techniques such as tokenization, stop-word removal, stemming, and lemmatization. It also discusses applications of NLP including sentiment analysis, text classification, named entity recognition, and intent detection, emphasizing the importance of integrating NLP into applications for enhanced automation and user experience.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Natural Language

Processing (NLP)

Natural Language Processing (NLP) is a transformative field that


enables machines to process, understand, and generate human
language. It sits at the intersection of computer science, linguistics,
and machine learning, combining computational power with
linguistic theory to bridge the gap between human communication
and machine understanding.
Language Understanding Challenges
Ambiguity in Language Synonyms & Polysemy Grammar Variation
Words and phrases often carry Different words can mean the Language structure varies
multiple meanings depending same thing, while single words widely across dialects, speakers,
on context, making can have multiple distinct and contexts, creating parsing
interpretation complex. meanings. challenges.

Context Dependence Real-World Noise


Meaning often relies on Typos, slang, abbreviations, and
surrounding text, cultural informal language introduce
knowledge, and situational significant data quality issues.
awareness.

Example: "Time flies like an arrow" vs "Fruit flies like a banana" — identical structure, entirely different meanings.
NLP Processing Pipeline
Text Acquisition(Collecting/Input)
Collect raw text from various sources like documents, user input, or APIs.

Text Preprocessing
Clean and normalize text through tokenization, stop-word removal, and stemming.

Feature Representation
Convert processed text into numerical vectors machines can understand.

Model Processing
Apply machine learning models to extract insights or make predictions.

Output Generation
Produce actionable results like classifications, entities, or responses.

# Python Example: Simple Pipeline


import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
text = "NLP makes language processable"
tokens = nltk.word_tokenize([Link]()) #(“NLP”, “makes” ,“language” ,“processable")
vectorizer = TfidfVectorizer()
features = vectorizer.fit_transform([text])
Text Acquisition Sources

User Input Forms Email & Chat Logs


Direct text entry from web forms, surveys, and Communication records from email systems and
application interfaces. messaging applications.

Documents
API Sources
Structured files including PDFs, Word documents, and text files.
Real-time text streams from web services, news
feeds, and third-party platforms.

Social Media
Posts, comments, and messages from platforms like
Twitter, Reddit, and Facebook.

# Python Example: Reading Text Sources


import PyPDF2
import requests

# From file
with open('[Link]', 'r') as f:
text = [Link]()

# From API
response = [Link]('[Link]
api_text = [Link]()['content']
Text Preprocessing: Overview
Normalization
Standardize text case, whitespace,
and encoding for consistency.
Noise Reduction
Remove irrelevant characters, HTML
tags, and formatting artifacts.
Feature Preparation
Structure text for optimal extraction of
meaningful features.

Preprocessing transforms messy, unstructured text into clean, standardized input that machine learning models can
effectively process. This critical stage directly impacts model accuracy and performance.

Example: "Hello WORLD!!!" → "hello world" (lowercase, punctuation removed)


Tokenization
Tokenization is the foundational step of breaking text into meaningful units called tokens.
These can be individual words, subwords, or sentences depending on the analysis needs.

Word-Level Tokenization
Splits text into individual words, preserving the atomic units of meaning.

Sentence-Level Tokenization
Segments text into complete sentences, useful for understanding context and structure.

Token Boundaries
Proper boundary detection handles punctuation, contractions, and special cases correctly.

# Python Example
from [Link] import word_tokenize

text = "NLP processes language."


tokens = word_tokenize(text)
# ['NLP', 'processes', 'language', '.']

# Sentence tokenization
from [Link] import sent_tokenize
sentences = sent_tokenize(text)
Stop-Word Removal
What Are Stop Words? Dimensionality Reduction
Common words like "is," "the," "and," Removing stop words significantly
and "of" that appear frequently but reduces the feature space, making
carry little meaningful information for models more efficient and focused.
analysis.

Performance Impact
Filtering stop words improves model training speed and often enhances
accuracy by emphasizing content words.

# Python Example: Stop-Word Removal


from [Link] import stopwords
from [Link] import word_tokenize

stop_words = set([Link]('english'))
text = "This is an example of stop word removal"
tokens = word_tokenize([Link]())
filtered = [w for w in tokens if w not in stop_words]
# Result: ['example', 'stop', 'word', 'removal']
NLP Stop Word Grammar Category Example Usage

the, a, an Articles / Determiners the model, a drone

in, on, at Prepositions in Pune, on data

is, are Auxiliary / Linking Verbs data is processed

and, but Conjunctions fast but noisy

he, she, it Pronouns it works

to Infinitive Marker to train a model

of, for Prepositions analysis of data


Stemming
Stemming is a rule-based
technique that reduces words ✓ Advantages
to their root form by removing
Fast processing, simple
suffixes. It's fast and
implementation, reduces
straightforward but can
vocabulary size effectively.
produce non-dictionary words.

Common
Transformations ✗ Limitations
• running → run Can create invalid words,
• studies → studi lacks linguistic accuracy,
may over-stem or under-
• fishing → fish
stem.
• happiness → happi
Lemmatization
Vocabulary-Based Approach Stemming vs Lemmatization
Uses linguistic knowledge and dictionaries to convert Stemming: faster but crude. Lemmatization: slower but
words to their base or dictionary form (lemma). produces real words with correct meaning.

# Python Example: Lemmatization


from [Link] import WordNetLemmatizer

lemmatizer = WordNetLemmatizer()
words = ["running", "studies", "better", "geese"]
lemmas = [[Link](w, pos='v') for w in words]
# Result: ['run', 'study', 'better', 'goose']
# Note: 'better' → 'better' (already base form)
Text Representation
01 02

Purpose of Vectorization Numerical Form


Machine learning models require
Conversion
numerical input. Text Each document becomes a
vectorization transforms words vector in a high-dimensional
into numbers that preserve space where similar texts are
semantic relationships. positioned close together.
03
Representation Types
Sparse representations (BoW, TF-IDF) use mostly zeros. Dense
representations (embeddings) use compact, information-rich
vectors.

Example: "cat" might become [0.2, 0.8, 0.1, ...] in a 300-


dimensional embedding space
Bag of Words (BoW)
1 Vocabulary Creation
Build a dictionary of all unique words across all documents in the c

2 Document-Term Matrix
Each row represents a document, each column a word, cells conta

3 Key Limitations
Ignores word order and context. "Dog bites man" equals
"Man bites dog" in BoW representation.
Bag of Words is a text vectorization technique that represents a document as an unordered collection of words, ignoring:
•grammar
•word order
•syntax
and preserving only:
•word frequency
Formally, given a corpus:

Construct a vocabulary:

Each document is represented as a vector:

where:
from sklearn.feature_extraction.text import CountVectorizer

corpus = [
"AI improves expense analysis",
"AI improves stock analysis"
]

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)

print(vectorizer.get_feature_names_out())
print([Link]())
TF-IDF: Term Frequency -
Inverse Document Frequency
Term Frequency (TF)
Measures how often a word appears in a document. More frequent
terms get higher scores.

Inverse Document Frequency (IDF)


Penalizes words that appear across many documents, reducing
weight of common terms.

Importance Weighting
Combines TF and IDF to identify words that are important to
specific documents, not just frequent everywhere.

Unlike simple BoW, TF-IDF distinguishes between words that are merely
common and words that are genuinely distinctive and meaningful for
specific documents.
Embeddings: Dense Vector Representations

Dense Vectors Semantic Similarity


Compact representations (typically Words with similar meanings have
100-300 dimensions) where each similar vectors. "King" - "Man" +
dimension captures semantic "Woman" ≈ "Queen".
features.

Types of Embeddings Context Awareness


Word embeddings (Word2Vec, Modern embeddings capture
GloVe), sentence embeddings contextual meaning, so "bank"
(BERT), and document differs in "river bank" vs "money
embeddings. bank".
Comparison of Text Representations
Feature Bag of Words TF-IDF Embeddings

Dimensionality Very high (vocabulary size) Very high (vocabulary size) Low (100-300)

Semantic Understanding None Limited Strong

Context Awareness No No Yes

Computational Cost Low Low High

Best Use Cases Simple classification Information retrieval, Complex understanding


document ranking tasks, similarity

Choose your representation based on task complexity, data size, and computational resources available.
NLP Task: Sentiment Analysis
Sentiment analysis determines the
Customer Feedback
emotional tone behind text,
classifying it as positive, negative, Analyze reviews and surveys
or neutral. This powerful technique to understand customer
reveals attitudes, opinions, and satisfaction.
emotions in written content.
Social Monitoring
Polarity Detection
Track brand perception and
Identifies whether text expresses public opinion on social
positive, negative, or neutral platforms.
sentiment with varying degrees of
confidence.

# Python Example: Sentiment Analysis


from textblob import TextBlob

text = "This product is amazing! I love it."


blob = TextBlob(text)
sentiment = [Link] # Returns: 0.65 (positive)
# Range: -1 (negative) to +1 (positive)
NLP Task: Text Classification
Binary Classification
1 Two-class problems like spam vs. ham, positive vs. negative, or relevant vs.
irrelevant.

Multi-Class Classification
2 Assign text to one of several categories, such as topic classification
(sports, politics, technology).

Spam Detection
3 Automatically identify unwanted emails or messages based on content
patterns and features.

Topic Categorization
4 Route documents to appropriate departments or organize content by
subject matter automatically.

Text classification powers email filtering, content moderation, document routing, and
countless other applications that require automatic categorization of text.
NLP Task: Named Entity
Recognition (NER)
Person
Names of individuals: "Steve Jobs", "Marie Curie"

Organization
Companies and institutions: "Apple", "MIT"

Location
Geographic entities: "San Francisco", "Europe"

Date
Temporal expressions: "January 2024", "tomorrow"

NER extracts structured information from unstructured text by identifying


and classifying named entities. This enables automatic extraction of key
information like who, what, where, and when from large text collections.
NLP Task: Intent Detection
User Input Entity Extraction
"Book a flight to Boston" Destination: Boston
Action: Book

1 2 3 4
Intent Classification Action Execution
Intent: book_flight Trigger flight booking workflow
Intent detection identifies what a user wants to accomplish, powering conversational AI in chatbots, virtual assistants,
and voice interfaces. It distinguishes between intents like "ask_question", "make_reservation", or "cancel_order".

Applications: Customer service bots, voice assistants (Siri, Alexa), automated support systems
Integrating NLP into Applications
Input Processing Output Handling
Receive user text from forms, Parse model results, handle
APIs, or uploaded documents. confidence scores, and manage
Clean and validate input data. edge cases with low certainty.
NLP Processing
Apply preprocessing, feature
extraction, and model inference.
Use cloud APIs or local models.

API-Based NLP Services


Cloud platforms like Google Cloud NLP, AWS Comprehend, and Azure Text Analytics provide pre-trained models
accessible via REST APIs, eliminating the need for model training and infrastructure management.

Confidence Scores & Error Handling


Always check prediction confidence. Set thresholds for automatic processing and flag low-confidence results for
human review. Handle ambiguous cases gracefully with fallback mechanisms.
NLP Output Utilization
Automation Triggers
Decision Systems
Automatically respond to queries,
Route tickets, prioritize emails,
schedule actions, or escalate issues
approve or flag content based on NLP
based on detected intents.
analysis.

Downstream Integration Personalization


Feed NLP outputs into Tailor content recommendations,
recommendation systems, analytics customize user experiences, and
dashboards, or other AI models for segment audiences by preferences.
enhanced intelligence.
NLP outputs transform raw text into actionable insights that drive business logic, improve user experiences, and
enable intelligent automation across applications.

You might also like