0% found this document useful (0 votes)
5 views29 pages

Text Pre-Processing Techniques in NLP

The document outlines a series of practical experiments focused on text preprocessing techniques in natural language processing (NLP), including tokenization, stop-word removal, lemmatization, and morphological analysis. It details the implementation of various models such as N-Gram and POS tagging, emphasizing their significance in understanding and processing text data. Each practical exercise includes theoretical explanations, algorithms, and conclusions on the effectiveness of the techniques discussed.

Uploaded by

hadapharshad66
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)
5 views29 pages

Text Pre-Processing Techniques in NLP

The document outlines a series of practical experiments focused on text preprocessing techniques in natural language processing (NLP), including tokenization, stop-word removal, lemmatization, and morphological analysis. It details the implementation of various models such as N-Gram and POS tagging, emphasizing their significance in understanding and processing text data. Each practical exercise includes theoretical explanations, algorithms, and conclusions on the effectiveness of the techniques discussed.

Uploaded by

hadapharshad66
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

Sr. No.

Title of Experiment
Apply text Pre-Processing techniques : Tokenization and Filtration & script
1
validation
Apply text Pre-Processing techniques : Stop-word removal ,
2
Lemmatization / Stemming
3 Perform Morphological Analysis and word generation
4 Implementation of the N-Gram model
5 Implementation of the POS taggers with input text
6 Perform chunking for the input text
7 Implementation of the Named Entity Recognizer for input text
8 Implementation of text Similarity Recognizer for choosen text Document
9 Exploratory Data Analysis of given text (WordCloud)

Practical no. 1

Aim :- Apply text Pre-Processing techniques : Tokenization and Fileration &


script validation.

Theory :

Text preprocessing is a foundational step in natural language processing (NLP)


that involves transforming raw text into a clean and structured format suitable for
analysis or machine learning models. It typically begins with converting all text to
lowercase to ensure uniformity and removing unwanted elements such as
punctuation, numbers, and special characters that may not contribute meaningful
information. Tokenization is then applied to split the text into individual words or
sentences. Common stop words like "the", "is", and "and" are often removed to
reduce noise and focus on the more meaningful components of the text. To further
normalize the data, techniques like stemming and lemmatization are used to reduce
words to their root forms—though stemming simply chops off word endings, while
lemmatization uses linguistic knowledge to return valid base words. Additional steps
may include handling negations, expanding contractions (e.g., "can't" to "cannot"),
and filtering out non-alphabetic tokens.

Text Pre-processing Techniques:


[Link] means the process of breaking down a text into smaller units called
tokens. These tokens can be either words, phrases, symbols, or even individual
characters, depending on the specific task or context. The purpose of tokenization is
dividing the text into meaningful components that can be processed more easily by a
computer algorithm.
In simpler terms, imagine you have a sentence: “The quick brown fox jumps over the
lazy dog.” Tokenization would break down this sentence into individual words:
“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”. Each of these
words is now a token, which can be analyzed, counted, or manipulated separately.
Now we’ll talk about what exactly is Script-Validation.
2. Script-Validation
Script validation refers to the process of verifying whether a given script or code
meets certain criteria or requirements. This validation can involve checking various
aspects of the script, such as syntax correctness, adherence to coding standards,
security vulnerabilities, or the presence of specific features or keywords. It has
techniques such as: Syntax checking, security validation, compliance checking,
feature validation etc.
Let’s move on to the 3rd technique i.e Filtration.
3. Filtration of Data
Data filtration, also known as data filtering, is the process of selecting or excluding
specific subsets of data from a larger dataset based on certain criteria or conditions.
The goal of data filtration is to extract relevant information or isolate specific patterns
within the dataset that meet predefined criteria.
Let’s us imagine that you have a large dataset containing information about
customer transactions at a retail store. Data filtration would involve selecting only
those transactions that meet certain conditions, such as transactions made by
customers who spent more than $100 or transactions that occurred within a specific
time frame.
Data filtration involves various operations such as: filtering by values, filtering
by conditions & filtering by patterns.
Algorithm:
1. Take the Input text
2. Convert text into Lowercase
[Link] Noise
[Link] text into tokens(word)
5. Output the clean token
Conclusion :
Practical no. 2

Aim: Apply text Pre-Processing techniques : Stop-word removal ,


Lemmatization/Stemming

Theory

Stop-word Removal
Stop-word removal is a fundamental preprocessing technique in natural
language processing (NLP) where common words that occur frequently in a language
but carry minimal meaningful information are removed from the text. These words
include articles, prepositions, pronouns, conjunctions, and auxiliary verbs such as
“the”, “is”, “in”, “and”, “on”, “at”, and “was”. Since these words do not significantly
contribute to the meaning of a sentence, especially in tasks like text classification or
information retrieval, removing them helps reduce dimensionality and improve
computational efficiency. However, the importance of stop words can vary depending
on the specific NLP task; for instance, they may be retained in sentiment analysis or
question answering where context is crucial.
Stemming
Stemming is the process of reducing a word to its root or base form, typically
by removing suffixes or prefixes. The root word obtained through stemming may not
always be a valid word in the language. For example, "playing", "played", and
"plays" can all be reduced to "play", while "running" might be reduced to "run" or
even "runn" depending on the algorithm. The most commonly used stemming
algorithm is the Porter Stemmer, which applies a set of heuristic rules to trim words.
Stemming is useful in applications like search engines or topic modeling where exact
word forms are less important than the general concept. However, it can be overly
aggressive and may lead to inaccurate base forms.
Lemmatization
Lemmatization is a more sophisticated text normalization technique that
reduces a word to its lemma, or dictionary base form, using linguistic knowledge
such as a word’s part of speech and meaning. Unlike stemming, lemmatization
always produces a valid word. For example, "was" becomes "be", "better" becomes
"good", and "running" becomes "run". Lemmatization often requires a language
model or dictionary (like WordNet) to determine the correct base form. It is more
accurate than stemming but also more computationally intensive. Lemmatization is
particularly useful when the grammatical correctness and meaning of words are
important in the analysis, such as in machine translation, parsing, and question
answering.

Algorithm:
[Link] input text
[Link] input into Lowercase and tokenize it
[Link] stop words
[Link] Stemming
[Link] Lemmatization
[Link] all result
Conclusion
Together, stop-word removal, stemming, and lemmatization are essential
techniques in text preprocessing. They help clean and normalize text, reduce
redundancy, and improve the efficiency and effectiveness of downstream NLP tasks.
Choosing between stemming and lemmatization depends on the specific application,
with stemming being faster but less precise, and lemmatization being more accurate
but slower.
Practical No. 3

Aim:Perform Morphological Analysis and word generation

Theory:

Morphological Analysis and Word Generation – Theory


Morphology is a branch of linguistics that studies the structure of words and
how they are formed from smaller meaningful units called morphemes. A morpheme
is the smallest unit of meaning in a language. Words can be made up of a single
morpheme, such as “book,” or multiple morphemes, like “unhappiness,” which
consists of the parts “un,” “happy,” and “ness.” In natural language processing,
morphological analysis is an important task that helps computers understand the
structure and meaning of words by breaking them down into their morphemes.
Morphological analysis is the process of identifying and analyzing the
morphemes in a word. It helps to understand the root or base of the word as well as
its prefixes and suffixes. This type of analysis plays a key role in many language
processing tasks such as machine translation, speech recognition, information
retrieval, and text-to-speech systems.
The main components of morphological analysis include the root or base word,
which carries the core meaning; prefixes, which are morphemes added before the
root; and suffixes, which are morphemes added after the root. There are two main
types of morphemes: inflectional morphemes and derivational morphemes.
Inflectional morphemes modify a word’s tense, number, or degree without changing
its essential meaning or part of speech, such as adding “-s” for plural or “-ed” for past
tense. Derivational morphemes create new words and can change the part of speech,
for example, adding “-ness” to “happy” to form “happiness.”
For example, the word “unhappiness” can be broken down into the prefix “un-,” the
root “happy,” and the suffix “-ness.” This morphological structure helps explain the
meaning of the word as the state of not being happy.
Morphological analysis is important in natural language processing because it
helps machines understand the grammatical structure of words, supports
lemmatization and stemming, aids in language translation and word sense
disambiguation, reduces the size of the vocabulary by grouping related words, and
improves information retrieval by matching different word forms.
Morphological generation is the reverse process of morphological analysis. It
involves creating different forms of a word based on its root by adding appropriate
prefixes, suffixes, or inflections depending on grammatical context such as tense,
number, or degree. For example, the root word “play” can generate forms like
“plays,” “played,” “playing,” “playable,” and “player.” This process is important in
applications like natural language generation, grammar checking, speech synthesis,
and predictive text input.
Morphological analysis and generation have many practical applications.
Search engines use them to match user queries with relevant documents despite
differences in word forms. Spell checkers and grammar correction tools rely on these
processes to detect and suggest corrections for incorrect word forms. Machine
translation systems use them to accurately translate words across languages. Tasks
such as text summarization and sentiment analysis also benefit from understanding
the structure and meaning of words.
There are several approaches and tools used for morphological analysis and
generation. Rule-based approaches apply linguistic rules to analyze and generate
words. Finite-state transducers are commonly used in building morphological
analyzers. Stemmers, such as the Porter Stemmer, reduce words to their stem forms
by chopping suffixes. Lemmatizers, which use vocabulary and morphological rules,
return the base or dictionary form of a word. Recently, machine learning and deep
learning models have been employed to learn morphological patterns from large
datasets.

Algorithm:
[Link] raw text
[Link] and lowercase formation
[Link] and generating base root
[Link] Morphological Varients
[Link] word generation on sample root

Conclusion:
Morphological analysis plays a fundamental role in Natural Language Processing
(NLP) by focusing on the structure and forms of words. It enables the decomposition
of words into their smallest meaningful units, known as morphemes. These
morphemes can be stems, prefixes, suffixes, or root words, and understanding them
allows for more efficient and accurate processing of text data.
Practical No. 4

Aim: Implementation of the N-Gram model

Theory:
Introduction to the N-Gram Model
The N-Gram model is a type of probabilistic language model used in Natural
Language Processing (NLP) to predict the next word or character in a sequence, based
on the previous N-1 items. The basic idea is to model the conditional probability of a
word given the previous words in a sequence. For example, given a sentence, the model
will predict the likelihood of a word occurring based on the preceding words.
The model’s name comes from the fact that it works with sequences of N elements,
where:
• Unigram (N=1): A single word.

• Bigram (N=2): A pair of consecutive words.

• Trigram (N=3): A triplet of consecutive words.

• And so on...
Probabilistic Nature of N-Grams
The central idea behind N-Grams is that the probability of the nth word in a
sequence can be predicted using the previous N-1 words. Mathematically, this is
represented as:
P(wn∣w1,w2,...,wn−1)≈P(wn∣wn−1,wn−2,...,wn−(N−1))
In simpler terms, the probability of the word wn occurring depends on the prior N-1
words. For instance, in a Bigram model, the probability of word wn given the word
wn−1 is:
P(wn∣wn−1)=Count(wn−1)Count(wn−1,wn)
Where:
• Count(w_{n-1}, w_n) is the count of the bigram (wn−1,wn) in the corpus.

• Count(w_{n-1}) is the count of the word wn−1 in the corpus.

The goal of the N-Gram model is to estimate the likelihood of a word given its context
(previous words), thus helping in text generation, machine translation, and other NLP
tasks.
Types of N-Gram Models
• Unigram Model: A unigram model treats each word as independent, ignoring
the context of the preceding words. This model is simple but often inaccurate for
real-world applications, as it doesn’t capture any dependency between words.
• Bigram Model: A bigram model uses the immediate previous word to predict
the next word. For instance, it calculates the probability of word wn occurring
given word wn−1.
• Trigram Model: A trigram model considers the two preceding words to predict
the next word. It uses a window of three words to compute the probability of the
next word.
• Higher-order N-Grams: Larger N-Grams, such as 4-grams or 5-grams,
consider longer sequences of words to predict the next word. While they tend to
be more accurate, they also require much larger datasets and more computational
resources.

Algorithm:
1. Take text input
[Link] text into lowercase
[Link] text into tokens (word)
[Link] function to generate N-grams
[Link] Unigram , bigram, triagrams
[Link] all N-grams
[Link] how often each bigram appears
[Link] 5 most Common bigrams
Conclusion
The N-Gram model is a simple yet powerful tool for modeling language based
on the frequency of word sequences. It is widely used in many NLP tasks like text
generation, machine translation, and speech recognition. However, due to its
limitations in handling long-range dependencies and data sparsity, N-Grams are often
supplemented or replaced by more sophisticated models like neural networks (e.g.,
RNNs and Transformers) for more advanced tasks.
Practical No. 5

Aim: Implementation of the POS taggers with input text

Theory:
Part-of-speech (POS) tagging is the process of assigning a part-of-speech label
to each word in a sentence. These labels represent the syntactic category of the word,
such as noun, verb, adjective, adverb, pronoun, etc. POS tagging is a fundamental
task in Natural Language Processing (NLP) as it helps in understanding the
grammatical structure of a sentence and in identifying relationships between words.
The POS tags are based on a predefined set of grammatical categories that words
belong to. For instance:
• Nouns: Represent people, places, things, or concepts (e.g., "dog", "city").

• Verbs: Represent actions or states of being (e.g., "run", "is").

• Adjectives: Describe or modify nouns (e.g., "beautiful", "fast").

• Adverbs: Modify verbs, adjectives, or other adverbs (e.g., "quickly", "very").

• Pronouns: Replace nouns in a sentence (e.g., "he", "they").

• Prepositions: Show relationships between words in a sentence (e.g., "in",


"on").
• Conjunctions: Connect words, phrases, or clauses (e.g., "and", "but").

The Need for POS Tagging


POS tagging is crucial for several reasons:
• Syntax and Structure: Helps in understanding sentence structure and
grammatical relationships between words.
• Information Retrieval: Enhances search engines by distinguishing between
homographs (words that are spelled the same but have different meanings
depending on their part of speech).
• Machine Translation: Assists in translating words correctly, as word meanings
can change based on their POS.
• Named Entity Recognition (NER): Helps in identifying named entities, like
persons or organizations, which often require POS tags to determine their role
in a sentence.
• Sentiment Analysis: Helps identify the emotional tone of sentences by tagging
adjectives and adverbs that indicate sentiment.
POS Tagging Approaches
There are several approaches to POS tagging, including:
• Rule-Based Tagging: This method uses a set of hand-crafted rules to assign
POS tags based on patterns observed in the context of the word. For example,
if a word follows a determiner (like "a", "the"), it is likely a noun.
• Statistical Tagging: Statistical models, such as Hidden Markov Models
(HMM), assign POS tags based on the probability of a tag sequence. These
models use training data to calculate the probability of tag transitions and tag
emissions (i.e., how likely a word is to have a particular tag).
• Machine Learning-based Tagging: Algorithms like Decision Trees,
Conditional Random Fields (CRFs), and Neural Networks are trained on
labeled data to predict POS tags. These models learn from the data and can
generalize to unseen text.
• Hybrid Approaches: Some systems combine multiple approaches, using both
rule-based and statistical methods to improve accuracy.
Conclusion
Part-of-speech tagging is an essential task in Natural Language Processing that
aids in understanding the structure and meaning of sentences. It helps in numerous
NLP applications like machine translation, sentiment analysis, and information
extraction. Although challenges like ambiguity and language-specific differences
exist, POS tagging continues to be a foundational component in the analysis and
processing of natural language text.
Practical No. 6

Aim:Perform chunking for the input text

Theory:
What is Chunking?
Chunking, also known as shallow parsing, is the process of segmenting and
labeling multi-token sequences into syntactically related groups called chunks. These
chunks are usually phrases, such as noun phrases (NP), verb phrases (VP),
prepositional phrases (PP), etc. Unlike full parsing, which aims to create a complete
syntactic tree structure, chunking focuses only on identifying and labeling these
major syntactic units or phrases in a sentence.
For example:
• Sentence: "The quick brown fox jumps over the lazy dog."

• Chunking:

• Noun Phrase (NP): "The quick brown fox"


• Verb Phrase (VP): "jumps"
• Prepositional Phrase (PP): "over the lazy dog"
Chunking helps identify meaningful units in the text, which is useful for tasks
like information extraction, question answering, and named entity recognition.
Importance of Chunking in NLP
• Simplification: It reduces the complexity of full parsing by identifying key
phrases without building a detailed syntactic tree.
• Understanding Structure: Helps in understanding the higher-level syntactic
structure of sentences by breaking them into manageable units.
• Information Extraction: Allows for easy extraction of key pieces of
information from the text, such as names, dates, and locations, by chunking
noun phrases (NP) and prepositional phrases (PP).
• Facilitates Other Tasks: Chunking is often used as a preprocessing step in
tasks like named entity recognition (NER) and relation extraction.
Types of Chunking
• Noun Phrase Chunking (NP Chunking): Involves identifying noun phrases.
A noun phrase typically consists of a noun along with its modifiers.
• Example: "The quick brown fox" → Noun Phrase (NP)
• Verb Phrase Chunking (VP Chunking): Involves identifying verb phrases. A
verb phrase typically consists of a main verb and its auxiliaries.
• Example: "is running" → Verb Phrase (VP)
• Prepositional Phrase Chunking (PP Chunking): Identifies prepositional
phrases, which start with a preposition and often include a noun phrase.
• Example: "under the table" → Prepositional Phrase (PP)
• Adjective Phrase Chunking (ADJP): Identifies adjective phrases.

• Example: "very tall" → Adjective Phrase (ADJP)


• Adverbial Phrase Chunking (ADVP): Identifies adverbial phrases.

• Example: "quite slowly" → Adverbial Phrase (ADVP)


4. How Chunking Works
Chunking can be performed using different approaches:
• Rule-Based Chunking: Uses predefined rules, often in the form of regular
expressions, to identify chunks based on patterns in the sentence. For example,
a rule might specify that a noun phrase starts with a determiner (e.g., "the", "a")
followed by an adjective and a noun.
• Statistical Chunking: Uses machine learning models to predict chunk
boundaries based on annotated training data. Models like Hidden Markov
Models (HMMs) and Conditional Random Fields (CRFs) are often used for
this task.
• Neural Network-based Chunking: More recently, deep learning techniques,
such as Recurrent Neural Networks (RNNs) and Transformers, have been
employed to improve chunking accuracy by capturing long-term dependencies
in the text.

Conclusion
Chunking is a critical process in Natural Language Processing that helps break
down sentences into meaningful syntactic units such as noun phrases, verb phrases,
and prepositional phrases. It plays an essential role in tasks like information
extraction, named entity recognition, and machine translation.
Practical No. 7
Aim: Implementation of the Named Entity Recognizer for input text
Theory :
Introduction to Named Entity Recognition (NER)
Named Entity Recognition (NER) is a subtask of Information Extraction
that aims to identify and classify named entities in text into predefined categories.
These categories generally include persons, organizations, locations, dates, and
miscellaneous entities such as money, percentages, or other domain-specific terms.
For example:
• Sentence: "Apple Inc. was founded by Steve Jobs in Cupertino in 1976."

• NER Output:

• Apple Inc. → Organization

• Steve Jobs → Person

• Cupertino → Location

• 1976 → Date

NER is a crucial task in NLP because it helps extract meaningful information


from unstructured text and plays a role in various applications such as search engines,
question answering, and document indexing.
The Need for Named Entity Recognition
NER helps identify and categorize important information from text, providing
a more structured representation of the data. Some key reasons why NER is important
include:
• Information Extraction: Helps in extracting useful details from large bodies
of text, such as identifying product names, locations, or dates.
• Question Answering Systems: Enables the identification of answers to
questions based on recognized entities (e.g., "Who is the CEO of Microsoft?").
• Document Categorization: Facilitates classifying documents based on
recognized named entities (e.g., news articles based on locations or people).
• Search Engines: Improves the search engine by associating entities with
specific queries (e.g., retrieving all articles related to "Steve Jobs").
Types of Named Entities
Common named entity categories include:
• Persons (PER): Names of individuals, either real or fictional (e.g., "Albert
Einstein", "John Doe").
• Organizations (ORG): Names of companies, institutions, or groups (e.g.,
"Google", "United Nations").
• Locations (LOC): Names of geographical locations such as cities, countries,
or landmarks (e.g., "Paris", "Mount Everest").
• Dates (DATE): Specific dates, periods, or time-related entities (e.g., "January
1, 2023", "next week").
• Monetary values (MONEY): Expressions referring to currency or amounts of
money (e.g., "$5,000", "10 euros").
• Percentages (PERCENT): Entities representing percentages (e.g., "50%", "3.2
percent").
NER may also involve more domain-specific or contextual entity types, such as:
• Products: e.g., "iPhone 13"

• Events: e.g., "World War II"

• Works of Art: e.g., "The Mona Lisa"

• Facilities: e.g., "Golden Gate Bridge"

Algorithm:
[Link] data like text with entities labeled
[Link] the data we have collected using with the tokenization
[Link] based Algorithm for text processing
[Link] Model with labeled datasets
5. Predicting entities and apply to next text
Conclusion
Named Entity Recognition (NER) is a vital NLP task that identifies and
classifies named entities such as people, organizations, locations, and other
significant terms in text. NER is crucial in many NLP applications, including
information retrieval, question answering, document classification, and social media
monitoring.
Named Entity Recognizer [Link]
import spacy

# Load the English NLP model


nlp = [Link]("en_core_web_sm")

# Input text
text = """
We are students of the CSE department performing the basic practical demonstration of the NLP
concepts at KGCE.
Gaurav and Sujal are working on this project together.
"""

# Process the text


doc = nlp(text)

# Named Entity Recognition


print("===== Named Entity Recognition =====")
for ent in [Link]:
print([Link], "-", ent.label_)
Practical No. 8
Aim: Implement text Similarity Recognizer for the choosen text document
Theory :
What is Text Similarity Recognition?
Text Similarity Recognition is the process of evaluating how similar two
pieces of text are. This can be done at various levels—such as comparing two words,
sentences, paragraphs, or even full documents. The goal is to assign a numerical score
or label that reflects the degree of similarity between the texts.
Text similarity is a core task in Natural Language Processing (NLP) and underpins
many applications such as:
• Document classification
• Plagiarism detection
• Information retrieval
• Text summarization
• Chatbots and question answering

Types of Text Similarity


Text similarity is generally divided into two main categories:
a) Lexical Similarity (Surface-Level)
• Compares the text based on words or characters used.

• Does not consider meaning or context.


• Common techniques:
• Jaccard Similarity: Measures overlap between word sets.

• Cosine Similarity (using Bag of Words or TF-IDF).

• Edit Distance (Levenshtein Distance): Measures how many insertions,


deletions, or substitutions are required to change one text into another.
b) Semantic Similarity (Meaning-Level)
• Compares the text based on meaning or context, even if the words differ.
• Uses word embeddings or language models to capture semantic relationships.

• Common techniques:
• Word2Vec / GloVe Embeddings: Represent words as dense vectors.

• Sentence Transformers (e.g., BERT): Encode full sentences or


documents.
• Semantic Textual Similarity (STS) models.

Approaches for Text Similarity Recognition


A) Bag of Words (BoW) Model
• Converts text into a vector of word counts.
• Similarity is computed using Cosine Similarity.

• Simple and fast, but ignores grammar and word order.


B) TF-IDF (Term Frequency-Inverse Document Frequency)
• Enhances BoW by reducing the weight of common words (e.g., “the”, “is”) and
increasing the importance of rare but meaningful words.
• Also uses Cosine Similarity to measure closeness between vectors.

C) Word Embeddings
• Words are converted into high-dimensional vectors where semantically similar
words are close in space.
• Pretrained models like Word2Vec, GloVe, or FastText are commonly used.

• Texts are represented by averaging or pooling the vectors of words.


Algorithm:
[Link] the choosen text document which is going to be processed for the
similarity
[Link] text again most important step of the similarity encountering
[Link] , lower text , removal of the stop-word for more accurate result
[Link] text into numerical form (TF-IDF/Embeddings)
[Link] Similarity(Cousine Similarity)
[Link] Similarity Score between texts
Conclusion
Text Similarity Recognition is a vital component in natural language processing that helps
machines understand and compare pieces of text meaningfully. Whether using simple lexical
techniques or advanced deep learning models, the goal remains the same: to evaluate how closely
two texts are related.

[Link]

from sklearn.feature_extraction.text import TfidfVectorizer


from [Link] import cosine_similarity
doc1 = "We have the project named Blue teaming and red teaming along with the models of
machine learning "
doc2 = "This Practical/ project consist of the various libraries and their models"
doc3 = "Unlike the Modeling we have to use trained models here for to implement it inside the
project"

documents = [doc1, doc2, doc3]

vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(documents)

similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix)

print("===== Text Similarity Scores =====")


for i, score in enumerate(similarity[0]):
print(f"doc1 vs doc{i+1} = {score:.2f} \n")
Practical No. 9
Aim: Exploratory Data Analysis of given text (WordCloud)
Theory :
What is Exploratory Data Analysis (EDA)?
Exploratory Data Analysis (EDA) is an essential step in the data analysis
process. It involves examining the dataset to summarize its main characteristics, often
with visual methods. In the context of text data, EDA helps us understand the
underlying patterns, frequency of words, relationships between terms, and the general
structure of the data. This helps in making data-driven decisions for further
processing, cleaning, and modeling.
WordCloud in Text Data Analysis
A WordCloud is a powerful and intuitive visualization technique commonly
used in Exploratory Data Analysis (EDA) for text data. It allows you to visually
display the most frequently occurring words in a text corpus, with word size
proportional to their frequency. The more frequent a word is in the text, the larger it
will appear in the WordCloud.
Why use WordCloud?
• Quick Overview: It gives a quick, intuitive understanding of the prominent
words in the dataset.
• Pattern Recognition: Helps to identify common themes or topics.

• Preprocessing Insight: It assists in understanding which terms might need


further processing or filtering (e.g., removing stopwords).
Importance of WordCloud in Text Analysis
• Identifying Key Themes: WordClouds reveal the most prominent and frequent
terms in a text corpus, helping analysts identify the main themes and concepts.
• Visualizing Text Data: WordClouds offer a simple and visual representation of
textual data, which can be insightful for both technical and non-technical
audiences.
• Data Cleaning Insight: By visually inspecting the WordCloud, you can often
identify noise words (e.g., frequent but meaningless words like "the", "is",
etc.), and this can guide your text preprocessing (like stopword removal).
• Highlighting Relevance: Words with larger fonts are likely to carry more
weight in the document, which can be crucial for tasks like sentiment analysis
or document classification.
How WordCloud Works
A WordCloud generates a visualization based on the frequency of words in the
dataset. The general steps involved are:
1. Tokenization: Split the text into individual words or tokens.

2. Frequency Counting: Count the occurrences of each word in the corpus.

3. Word Weighting: Assign a weight (typically frequency) to each word.

4. Visualization: Display the words in a cloud-like pattern, with word size


proportional to its frequency.
Key Components of a WordCloud
• Word Size: Represents the frequency or importance of a word. Words that
appear more frequently will have a larger size.
• Color: Can represent additional information, such as sentiment or category,
though this is optional.
• Shape: The WordCloud can be customized to fit different shapes, such as
circles, hearts, etc., to make the visualization more engaging or relevant to the
domain.
• Rotation: Words can be rotated at random angles to make the visualization
more aesthetically appealing.
Algorithm:
[Link] input text for procesing
[Link] techniques applied to the input text
3. tokenization , lowercase , remove punctuation/number , filter stop-word
[Link] word frequencies
[Link] & display the Word Cloud.

Conclusion
WordClouds are a powerful and intuitive tool for conducting Exploratory
Data Analysis (EDA) on text data. They offer a visual representation of the most
frequent words in a dataset, which can help in understanding key themes, cleaning the
data, and guiding further analysis.

[Link] code :

from wordcloud import WordCloud, STOPWORDS


import [Link] as plt
text = """
Machine Learning + Cybersecurity Project Title: "Predictive Threat Intelligence System using
Supervised ML" - Description: Train models to classify incoming traffic or logs as benign or
malicious
based on labeled datasets. - Cybersecurity Relevance: Enables automation of threat detection
based
on behavioral patterns"""
stopwords = set(STOPWORDS)
wordcloud = WordCloud(width=800, height=400,
background_color="white",
stopwords=stopwords,
colormap="plasma").generate(text)
[Link](figsize=(10, 6))
[Link](wordcloud, interpolation="bilinear")
[Link]("off")
[Link]("Word Cloud - Text EDA")
[Link]()

You might also like