6.2 Text Normalization, Text Processing Algorithms (Bow, TF-IDF), Sentiment Analysis

Ever wondered how Amazon or Flipkart instantly sorts thousands of product reviews into “customers loved it” and “customers hated it”, without a single human reading each one? That’s AI reading text, and by the end of this article you’ll know exactly how it’s done. In Part 1 of Chapter 6, we learned what NLP is, why human language is so tricky for machines, and how a five-stage pipeline: Lexical, Syntactic, Semantic, Discourse Integration, Pragmatic takes a computer from raw text to real understanding.

6.1 Introduction to NLP, its Applications & Chatbots (AI Course; Class 10)

There is one very practical question: computers only understand numbers. So how does it read and understand a sentence? That’s exactly what we solve today, ending with a understanding of a NLP case study: Sentiment Analysis.

Text Normalization

Humans interact with each other easily as our natural languages are convenient and effortless for us. But for computers, our languages are complex. Since a computer’s language is numerical, the very first step is to simplify our text before converting it to numbers. That simplification process is called Text Normalization.

We’ll work through this on a corpus, the term used for the whole textual data across multiple documents. Here are the steps:

Stemming and Lemmatization, both remove affixes to find a word’s root but only Lemmatization guarantees a real word.

Once we’ve normalized our text down to clean tokens, it’s time to convert those tokens into numbers, starting with the “Bag of Words” algorithm.

Bag of Words(BoW)

Bag of Words (BoW) is an NLP model that extracts features out of text for use in machine learning algorithms. It works by finding the occurrence of each word and constructing a vocabulary for the corpus. The word “bag” is deliberate, it means the sequence of words doesn’t matter. All we need are the unique words and their frequency.

Here’s the step-by-step approach, worked through with an example:

Step 1: Collect and pre-process the data.

  • Document 1: “Aman and Avni are stressed”
  • Document 2: “Aman went to a therapist”
  • Document 3: “Avni went to download a health chatbot”

After text normalization, these become:

  • Document 1: [aman, and, avni, are, stressed]
  • Document 2: [aman, went, to, a, therapist]
  • Document 3: [avni, went, to, download, a, health, chatbot]

Step 2: Create a Dictionary

Create a dictionary by listing every unique word across all three documents

[aman, and, avni, are, stressed, went, to, a, therapist, download, health, chatbot]

Step 3 : Create Document Vectors

For each document create document vectors by placing a 1 under a word if it occurs (incrementing if it occurs again), and a 0 if it doesn’t. We have create a document vector for this corpus as shown below:

The Bag of Words gives us two things: a vocabulary for the corpus, and the frequency of each word in each document. But there’s a problem: the tokens have become numbers, but every word is still being treated as equally important. That’s where TF-IDF comes in.

TF-IDF: Term Frequency & Inverse Document Frequency

Think about a book on air pollution. The words “air” and “pollution” would occur frequently and they’re valuable, because they tell us what the document is about. But now imagine 10 documents, each on a totally different topic, one on women’s empowerment, another on unemployment, and so on. Would “air” and “pollution” still be the most frequent words across all of them? Almost certainly not. So which words would be the most frequent across all ten? Words like “and,” “this,” “is,” “the”, words that appear everywhere but tell us nothing about what any specific document is about. These are called “stop words”, and they’re usually removed at the pre-processing stage.

Plotted against occurrence, words fall into three bands as shown below:

 

TF-IDF stands for “Term Frequency” and “Inverse Document Frequency”, and it helps us identify the actual value of each word. It’s also the same basic idea a review-sorting system leans on: a word like “and” appears everywhere and tells you nothing, but a word like “defective” or “excellent” appears rarely and tells you a great deal.

Term Frequency (TF) is the frequency of a word in one document, exactly the numbers already sitting in our document vector table.
Document Frequency (DF) is the number of documents a word occurs in, regardless of how many times.

Inverse Document Frequency (IDF) puts document frequency in the denominator, with total number of documents as the numerator:

IDF(word) = Total Documents / Document Frequency of word.

The formula for any word W becomes:

TFIDF(W) = TF(W) × log(IDF(W))

In Conclusion:

Words that occur in all documents with high term frequencies get the lowest TF-IDF values, and are effectively stop words. For a word to have a high TF-IDF value, it needs a high term frequency but a low document frequency. It means, it matters a lot to one specific document, but isn’t a common word across the whole corpus. The higher the value, the more important the word is to that corpus.

Applications of TF-IDF

Various applications where TF-IDF is helpful is:

  1. Document Classification: Classifying the type and genre of a document.
  2. Topic Modelling: Predicting the topic for a corpus.
  3. Information Retrieval Systems: Extracting the important information out of a corpus.
  4. Stop Word Filtering: Removing unnecessary words from a text body.

I have discussed about NLP and its algorithms in detail in the article mentioned below. Click on this link to learn in detail.

https://nidhibansal1902.medium.com/natural-language-processing-jigsaw-unintended-bias-in-toxicity-classification-case-study-efbc6ff2796b

Python Libraries for NLP

There are two widely used python Libraries for NLP:

1. NLTK (Natural Language Tool Kit)

A Python package with functions and modules to work with human language data. It provides easy-to-use interfaces for text processing, classification, tokenization, stemming, tagging, parsing, and semantic reasoning.

2. SpaCy

An open-source NLP library offering tokenization, part-of-speech tagging, named entity recognition, dependency parsing, and more .

Sentiment Analysis

Sentiment Analysis is an NLP technique used to analyze whether a given piece of textual data is positive, negative, or neutral. This is exactly how AI reads a review, a tweet, or a comment and tells you, in an instant, how the person behind it actually feels.

Examples:

Few examples of Sentiment Analysis are as follow:

Customer Service:  Customers sentiment analysis automatically detects emotions as customers interact with products, services, or brands.
Voice of the Customer: It helps in analyzing customer feedback to measure the gap between what customers expect and what they actually experience.

Sentiment Analysis with Orange Data Mining

Refer to article 4.2 for detailed sentiment analysis with Orange Data Mining tool. Below is the link for Video.

Test Yourself

  1. Discuss all steps in Text Normalization?
  2. What distinguishes lemmatization from stemming? explain with the help of example.
  3. What is the primary purpose of the Bag of Words model?
  4. What is the primary purpose of TF-IDF in text processing?
  5. What are the words called with negligible value that are often removed during preprocessing?

Stay Tuned!!

Stay tuned for further content where we will deep dive in to AI Agents and explore AI in depth,

Click below for the YouTube video for Part 2 of Unit 6:

For any queries and clarification, kindly email us at datasciencehorizon@gmail.com

Keep learning and keep implementing!!

Leave a Comment

Your email address will not be published. Required fields are marked *