VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/07/getting-started-with-nlp-using-nltk-library/

โ‡ฑ NLP using NLTK Library | NLTK Library for Natural Language Processing


India's Most Futuristic AI Conference Is Back โ€“ Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Reading list

Getting started with NLP using NLTK Library

Ajay Vardhan Last Updated : 30 Jul, 2021
7 min read
This article was published as a part of the Data Science Blogathon 

Introduction:

1010010   01101001   01110100   01101000   01101001  01101011   01100001

Did you understand the above binary code? If yes, then youโ€™re a computer. If no, then youโ€™re a Human. ๐Ÿ™‚

I know itโ€™s a difficult task for us to understand binary code just like computers because binary code is a Machine Understandable Language. Likewise, even computers donโ€™t understand human language. So, how to make computers understand human language? The answer is Natural Language Processing. With the help of NLP, we can teach computers to understand Human Language.

These days NLP is getting a lot of attention, with the launch of Githubโ€™s Copilot, which was developed using OpenAIโ€™s GPT-3 language model.

This article covers the basics of NLP and techniques used to process the data using NLP library NLTK.

So, without further ado, letโ€™s get started.

Human language is highly ambiguous โ€ฆ It is also ever changing and evolving. People are great at producing language and understanding language, and are capable of expressing, perceiving, and interpreting very elaborate and nuanced meanings. At the same time, while we humans are great users of language, we are also very poor at formally understanding and describing the rules that govern language.

                        โ€• Page 1, Neural Network Methods in Natural Language Processing, 2017.

Table of Contents:

  1. What is Natural Language Processing(NLP)?
  2. Applications of NLP
  3. Libraries for NLP
  4. Installation of NLTK Library
  5. Data Preprocessing using NLTK

1. What is Natural Language Processing (NLP)?

Natural Language Processing is an interdisciplinary field of Artificial Intelligence. It is a technique used to teach a computer to understand Human languages and also interpret just like us. It is the art of extracting information, hidden insights from unstructured text. It is a sophisticated field that makes computers process text data on a large scale.

The ultimate goal of NLP is to make computers and computer-controlled bots understand and interpret Human Languages, just as we do.

๐Ÿ‘ NLP using NLTK Library
Image by Author(Made with Canva)

According to Fortune Business Insights, the NLP is expected to grow from 20 billion dollars in 2021 to 127 billion dollars in 2028.

Natural Language Processing is further classified into:

  1. Natural Language Understanding
  2. Natural Language Generation

If youโ€™re looking for an online course to master NLP, then check out Analytics Vidya Course.

2. Applications of NLP:

Natural Language Processing is powering many industries with its advanced Deep Learning Algorithms like transformers, language models(GPT-3), RNNs, LSTMs, and many more.

NLP is used in,

  • Sentimental Analysis
  • Chatbots
  • Virtual Assistants
  • Speech Recognition
  • Machine Translation
  • Advertise Matching
  • Information Extraction
  • Grammatical error detection
  • Fake news detection
  • Text Summarize

3. Libraries for NLP

Here are some of the libraries for leveraging the power of Natural Language Processing.

  1. Natural Language Toolkit (NLTK)
  2. spaCY
  3. Gensim
  4. Standford CoreNLP
  5. TextBlob

In this article, I will use the NLTK library to demonstrate Text Data Preprocessing.

4. Installation of NLTK Library

NLTK is a standard python library that provides a set of diverse algorithms for NLP. It is one of the most used libraries for NLP and Computational Linguistics.

Now, let us see how to install the NLTK library.

For windows, open a command prompt and run the below command:

pip install nltk

For mac/Linux, open the terminal and run the below command:

sudo pip install -U nltk
sudo pip3 install -U nltk

To install through Anaconda, open Anaconda prompt, run the below command:

conda install -c anaconda nltk

To install in Jupyter Notebook, type the below command in a cell and click on run.

!pip install nltk

5. Data Preprocessing using NLTK:

The process of cleaning unstructured text data, so that it can be used to predict, analyze, and extract information. Real-world text data is unstructured, inconsistent. So, Data preprocessing becomes a necessary step.

The various Data Preprocessing methods are:

  • Tokenization
  • Frequency Distribution of Words
  • Filtering Stop Words
  • Stemming
  • Lemmatization
  • Parts of Speech(POS) Tagging
  • Name Entity Recognition
  • WordNet

These are some of the methods to process the text data in NLP. The list is not so exhaustive but serves as a great starting point for anyone who wants to get started with NLP.

(a) Tokenization:

The process of breaking down the text data into individual tokens(words, sentences, characters) is known as Tokenization. It is a foremost step in Text Analytics.

Itโ€™s a fundamental and foremost step in methods like Count Vectorizer and also in Deep learning-based architectures like Transformers.

Tokenization is implemented using a class tokenize in NLTK Library.

text = """The voice that navigated was definitely that of a machine, and yet you could tell that the machine was a woman, which hurt my mind a little. How can machines have genders? The machine also had an American accent. How can machines have nationalities? This can't be a good idea, making machines talk like real people, can it? Giving machines humanoid identities?"""

Letโ€™s first split up the above text into sentences using sent_tokenize().

(i) Sentence Tokenization

When text data is split into sentences, then it is sentence tokenization. It helps when text data consists of multiple paragraphs.

from nltk.tokenize import sent_tokenize
text_to_sentence = sent_tokenize(text)
print(text_to_sentence)

Now, the given text is tokenized into sentences.

(ii) Word Tokenization:

When text data is split into individual words, then it is word tokenization. It is implemented using  word_tokenize().

from nltk.tokenize import word_tokenize
tokenized_word = word_tokenize(text)
print(tokenized_word)

(b) Frequency Distribution of words:

We can generate the frequency distribution of words in a text by using the FreqDist() function in NLTK. Even results can be plotted using the matplotlib plot() function.

from nltk.probability import FreqDist
freq_dist_of_words = FreqDist(tokenized_word)
print(freq_dist_of_words)

most_common() is the function used to print the most frequent words.

freq_dist_of_words.most_common(5)
import matplotlib.pyplot as plt
freq_dist_of_words.plot(30,cumulative=False)
plt.show()

(c) Filtering Stop Words:

Stop words are used to filter some words which are repetitive and donโ€™t hold any information. For example, words like โ€“ {that these, below, is, are, etc.} donโ€™t provide any information, so they need to be removed from the text. Stop Words are considered as Noise. NLTK provides a huge list of stop words.

import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
stop_words=set(stopwords.words("english"))
print(stop_words)

Removing Stop words:

text = 'Learn to lose your destiny to find where it leads you'
filtered_text = []
tokenized_word = word_tokenize(text)
for each_word in tokenized_word:
 if each_word not in stop_words:
 filtered_text.append(each_word)
print('Toxenized list with stop words: {}'.format(tokenized_word))
print('Toxenized list with out stop words: {}'.format(filtered_text))

(d) Stemming:

Stemming is a process of normalization, in which words are reduced to their root word (or) stem. Spacy doesnโ€™t support stemming, so we need to use the NLTK library.

Types of stemming:

  1. Porter Stemmer
  2. Snowball Stemmer
from nltk.stem import PorterStemmer
pstemmer = PorterStemmer()
words = ['happy', 'happier', 'happiest', 'happiness', 'breathing','fairly','eating']
for word in words:
 print(word + '--->' + pstemmer.stem(word))

As we can see above, the words are reduced to their stem word, but one thing we can notice is that the porter stemmer is not giving good results. So, we will use the Snowball stemmer, which is better when compared with porter stemmer.

from nltk.stem.snowball import SnowballStemmer
snow_stem = SnowballStemmer(language='english')
words = ['happy', 'happier', 'happiest', 'happiness', 'breathing','fairly','eating']
for word in words:
 print(word + '--->' + snow_stem.stem(word))

(e) Lemmatization:

Like stemming, lemmatization is also used to reduce the word to their root word. Lemmatizing gives the complete meaning of the word which makes sense. It uses vocabulary and morphological analysis to transform a word into a root word.

from nltk.stem.wordnet import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
text = "Life will always have problems and pressures."
lemmatized_words_list = []
tokenized_word = word_tokenize(text)
for each_word in tokenized_word:
 lem_word = lemmatizer.lemmatize(each_word)
 lemmatized_words_list.append(lem_word)
print('Text with Stop Words: {}'.format(tokenized_word))
print('Lemmatized Words list {}'.format(lemmatized_words_list))

(f) Parts of Speech(pos) tagging: 

Parts of Speech(POS) tagging is the process of identifying parts of speech of a sentence. It is used to identify nouns, verbs, adjectives, adverbs, etc., and tag each word. There are many tagging sets available in NLTK,  but I will be using Universal Tag Set.

import nltk
nltk.download('universal_tagset')
text = "I'm going to meet M.S. Dhoni."
tokenized_word = word_tokenize(text)
nltk.pos_tag(tokenized_word, tagset='universal')
text = "I'm going to meet M.S. Dhoni."
tokenized_word = word_tokenize(text)
nltk.pos_tag(tokenized_word, tagset='universal')

(g) Named Entity Recognition:

Named Entity Recognition is used to identify names of organizations, people, and geographic locations in the text and tag them to the text.

import nltk
nltk.download('maxent_ne_chunker')
nltk.download('words')
text = "Sundar Pichai, the CEO of Google Inc. is walking in the streets of California."
tokenized_word = word_tokenize(text)
tags = nltk.pos_tag(tokenized_word, tagset='universal')
entities = nltk.chunk.ne_chunk(tags, binary=False)
print(entities)

(h) WordNet:

WordNet is a huge collection of words with meanings just like a traditional dictionary, used to generate synonyms, antonyms of words.

from nltk.corpus import wordnet
synonym = wordnet.synsets("AI")
print(synonym)

Print definition using WordNet:

print(synonym[1].definition())

Print examples using WordNet:

print(synonym[1].examples())

End Notes:

These concepts are good enough for beginners to get started with Natural Language Processing.

 

About the Author:

Hello, Iโ€™m Ajay Vardhan Reddy, pursuing my bachelorโ€™s in Computer Science and Engineering from Vignana Bharathi Institute of Technology, Hyderabad. Iโ€™m a Co-founder of the Machine Learning Forum โ€“ EpsilonPi, an aspiring Machine Learning Engineer, full-stack web developer, social media marketer, and also a competitive programmer. Iโ€™m a content creator on Instagram with the user name @googlers_tech.

You can connect me:

Thank you folks for reading. Happy Learning!

The media shown in this article are not owned by Analytics Vidhya and are used at the Authorโ€™s discretion.

Login to continue reading and enjoy expert-curated content.

Free Courses

Build a Document Retriever Search Engine with LangChain

โ€‹Learn to create a document retrieval search engine using LangChain. โ€‹

Coding a ChatGPT-style Language Model From Scratch in Pytorch

Build a ChatGPT-style language model using PyTorch.

Naive Bayes from Scratch

Master Naรฏve Bayes for ML: Build classifiers, analyze data, and apply Bayes.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
๐Ÿ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
๐Ÿ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

๐Ÿ‘ Popup Banner
๐Ÿ‘ AI Popup Banner