Tokenizing text data
When we deal with text, we need to break it down into smaller pieces for analysis. To do this, tokenization can be applied. Tokenization is the process of dividing text into a set of pieces, such as words or sentences. These pieces are called tokens. Depending on what we want to do, we can define our own methods to divide the text into many tokens. Let's look at how to tokenize the input text using NLTK.
Create a new Python file and import the following packages:
from nltk.tokenize import sent_tokenize, \
word_tokenize, WordPunctTokenizer
Define the input text that will be used for tokenization:
# Define input text
input_text = "Do you know how tokenization works? It's actually \
quite interesting! Let's analyze a couple of sentences and \
figure it out."
Divide the input text into sentence tokens:
# Sentence tokenizer
print("\nSentence tokenizer:")
print(sent_tokenize(input_text))
Divide the input text into word tokens:
# Word tokenizer
print("\nWord tokenizer:")
print(word_tokenize(input_text))
Divide the input text into word tokens using the WordPunct
tokenizer:
# WordPunct tokenizer
print("\nWord punct tokenizer:")
print(WordPunctTokenizer().tokenize(input_text))
The full code is given in the file tokenizer.py
. If you run the code, you will get the following output:

Figure 1: Tokenizers output
The sentence tokenizer divides the input text into sentences. Two-word tokenizers behave differently when it comes to punctuation. For example, the word "It's" is divided differently by the punct tokenizer than by the regular tokenizer.