DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Instant Integrations With API and Logic Automation
  • Iptables Basic Commands for Novice
  • Traceroute Command in Python and How to Read a Traceroute
  • Automate Sign-In To Any Website Using Selenium Web-Driver

Trending

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • AI's Dilemma: When to Retrain and When to Unlearn?
  1. DZone
  2. Coding
  3. Languages
  4. NLTK Hello World Python Example

NLTK Hello World Python Example

Learn how to get started with natural language processing (NLP) with (Natural Language Toolkit), a platform to work with human language using Python language.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Updated Oct. 09, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, you will learn about getting started with natural language processing (NLP) with (Natural Language Toolkit), a platform to work with human languages using Python language. The post is titled hello world because it helps you get started with NLTK while also learning some important aspects of processing language. In this post, the following will be covered:

  • Install/Set up NLTK
  • Common NLTK commands for language processing operations

Install/Set up NLTK

This is what you need to do set up NLTK.

  • Make sure you have Python latest version set up as NLTK requires Python version 3.5, 3.6, 3.7, or 3.8 to be set up. In Jupyter notebook, you could execute command such as !python -version to know the version.
  • If you have got Anaconda set up, you can get started with executing command such as import nltk
  • In case you don't have Anaconda set up, you can execute the following command and get started. The command works well with Unix / Mac.
Java
 




x


 
1
# Pip install
2
#
3
pip install nltk
4
#
5
# Import NLTK
6
#
7
import nltk



You could get started with practicing NLTK commands by downloading the book collection comprising of several books. Here is what you need to execute:

Java
 




xxxxxxxxxx
1


 
1
#
2
# NLTK Book Download
3
#
4
nltk.download()



Executing the above command will open up a utility where you could select book and download. Here is how it looks like:

Select the book and click download. Once the download is complete, you could execute the following command to load the book.

Java
 




xxxxxxxxxx
1


 
1
#
2
# Load the books
3
#
4
from nltk.book import *



This is how it would look like by executing the above command.


Common NLTK Commands/Methods for Language Processing

Here are some of the common NLTK commands vis-a-vis their utility:

  • nltk.word_tokenize(): Tokenize the sentence in words and punctuations. This includes the duplicate words and punctuations as well.
Java
 




xxxxxxxxxx
1
15


 
1
import nltk
2
#
3
# Sentence
4
#
5
intro = 'My name is Ajitesh Shukla. I work in HighRadius. I live in Hyderabad.'
6
#
7
# Tokenize using word_tokenize method
8
#
9
tokens = nltk.word_tokenize(intro)
10
#
11
#
12
print(tokens)
13
#
14
#
15
print(set(tokens))



Here is how the output would look like:

  • concordance() vs similar() vs common_contexts():These are three methods which could be invoked on nltk.text.Text objects in order to find the following:
    • concordance: Find the context in which a word/token occurred. The output is a set of different sentences where the word occurred.
    • similar: Find the similar context in which the word occurred. Basically, find the sentences that consist same surrounding words (left and right).
    • common_contexts: Find the pair of common surrounding words of the tokens passed to common_contexts method.

We will try and understand with one of the text (text7 - Wall Street Journal) loaded from nltk.book. In the example below, common_contexts output is to_the and to_their. This implies that to_the and to_their occurred around both the words, finance and improve. If the output of common_contexts would have been null/empty, the output of method similar would also have been null/empty.

  • FreqDist(): FreqDist() method counts the frequence of occurence of tokens (words or punctuations) and returns a FreqDist object representing a JSON object having key-value pair of key and number of occurrences. This method is present as part of nltk.probability package. You can use plot method on the instance of FreqDist to draw the plot.
Java
 




xxxxxxxxxx
1
17


 
1
import nltk
2
#
3
# Sentence
4
#
5
intro = 'My name is Ajitesh Shukla. I write blogs on Vitalflux.com. I live in Hyderabad. I love writing blogs. I also have good expertise in cloud computing. I am also good in AWS.'
6
#
7
# Tokenize using word_tokenize method
8
#
9
tokens = nltk.word_tokenize(intro)
10
#
11
# Create an instance of FreqDist
12
#
13
freqdist = FreqDist(tokens)
14
#
15
# Draw the frequency distribution of tokens
16
#
17
freqdist.plot()



Here is how the output plot would look like:

  • Printing tokens (words) satisfying some property such as token length: Here is a piece of code which can be used to print all words which meet certain criteria. For example, length of word > 5. Note the last statement in the code given below:
Java
 




xxxxxxxxxx
1
13


 
1
import nltk
2
#
3
# Sentence
4
#
5
intro = 'My name is Ajitesh Shukla. I write blogs on Vitalflux.com. I live in Hyderabad. I love writing blogs. I also have good expertise in cloud computing. I am also good in AWS.'
6
#
7
# Tokenize using word_tokenize method
8
#
9
tokens = nltk.word_tokenize(intro)
10
#
11
# Condition to filter words meeting criteria
12
#
13
long_words = [words for words in tokens if len(words) > 5]



This is what will be printed

Conclusions

Here is the summary of what you learned in this post related to NLTK set up and some common methods:

  • NLTK is a Python library to work with human languages such as English.
  • NLTK provides several packages used for tokenizing, plots, etc.
  • Several useful methods such as concordance, similar, common_contexts can be used to find words having context, similar contexts.
Python (language) Command (computing)

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Instant Integrations With API and Logic Automation
  • Iptables Basic Commands for Novice
  • Traceroute Command in Python and How to Read a Traceroute
  • Automate Sign-In To Any Website Using Selenium Web-Driver

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!