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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • How an AI Software Factory Works: From Developer Intent to Production
  • Designing Frontend Applications for Failure, Not Just Success
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • RAG Architecture Patterns: Proxy, Caching, and Routing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Oct. 09, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. 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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook