NLTK – A Powerful Assistant for Natural Language Processing
If you want to do natural language processing with Python, NLTK is definitely the go-to library. It not only comes with a ton of corpora and dictionaries, but also provides a suite of tools for text processing, making it an essential tool for NLP beginners.
Installation Tips
Installing NLTK is super simple, just one line of code:
pip install nltk
After installation, you also need to download some data packages, or else you won’t be able to do much:
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')
Tip: If you keep failing to download, it’s probably a network issue; just use a VPN!
Tokenization Made Easy
Tokenization is the process of splitting a text into individual words, which can be easily done with NLTK’s word_tokenize:
from nltk.tokenize import word_tokenize
text = "NLTK is really useful, it can handle text in no time!"
words = word_tokenize(text)
print(words) # ['NLTK', 'is', 'really', 'useful', ',', 'it', 'can', 'handle', 'text', 'in', 'no', 'time', '!']
See? Even Chinese tokenization is not a problem, although the results may not be as good as specialized Chinese tokenization tools.
Part-of-Speech Tagging
Let’s assign part-of-speech tags to each word to see whether it is a noun or a verb:
from nltk import pos_tag
text = "I love coding with Python"
tokens = word_tokenize(text)
tagged = pos_tag(tokens)
print(tagged) # [('I', 'PRP'), ('love', 'VBP'), ('coding', 'VBG'), ('with', 'IN'), ('Python', 'NNP')]
VBP is a verb, and NNP is a proper noun. These tags can seem confusing at first, but you’ll get used to them with practice.
Lemmatization is Quite Useful
Sometimes we want to reduce words to their base form, like turning running into run:
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize('running', pos='v')) # run
print(lemmatizer.lemmatize('better', pos='a')) # good
Tip: Remember to specify the pos parameter (part of speech), or the results may be inaccurate!
Stop Words Handling Tips
Stop words are high-frequency words like “的”, “了”, “the”, “is” that have little meaning:
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
text = "This is a sample sentence"
words = word_tokenize(text)
filtered_words = [word for word in words if word.lower() not in stop_words]
print(filtered_words) # ['This', 'sample', 'sentence']
Word Frequency Count Made Simple
Want to know how many times a word appears? Counter can help:
from collections import Counter
from nltk.tokenize import word_tokenize
text = "Python is amazing and Python is fun"
words = word_tokenize(text)
word_freq = Counter(words)
print(word_freq) # Counter({'Python': 2, 'is': 2, 'amazing': 1, 'and': 1, 'fun': 1})
NLTK actually has many other fun features, such as sentiment analysis and text classification. But we’ll stop here for today; if you’re interested, feel free to check out the official documentation for more examples.
When coding for text processing, don’t forget error handling. If you encounter special characters or encoding issues, your program can easily crash. Develop good habits, and try-except is essential!
With such a powerful tool like NLTK, it would be a shame not to play around with it. Go ahead and open your editor to give it a try!

Previous Reviews
◆ Gensim, a powerful tool for topic modeling!
◆ Click, a Python library for creating command-line interfaces!
◆ CuPy, a Python library for NVIDIA GPUs!