Tokenization breaks text into smaller pieces like words or sentences. This helps computers understand and work with language step by step.
When you want to count how many words are in a text message.
When you need to split a paragraph into sentences to analyze each one separately.
When preparing text data for a chatbot to understand user input.
When cleaning text before translating it to another language.
When building a search engine that finds documents by words.
Syntax
NLP
from nltk.tokenize import word_tokenize, sent_tokenize
text = "Your text here."
words = word_tokenize(text)
sentences = sent_tokenize(text)
Use word_tokenize to split text into words.
Use sent_tokenize to split text into sentences.
Examples
This splits the text into words: ['Hello', 'world', '!']
NLP
from nltk.tokenize import word_tokenize
text = "Hello world!"
words = word_tokenize(text)
print(words)
This splits the text into sentences: ['Hello world!', 'How are you?']
NLP
from nltk.tokenize import sent_tokenize
text = "Hello world! How are you?"
sentences = sent_tokenize(text)
print(sentences)
Sample Model
This program splits the text into words and sentences, then prints both lists.
NLP
from nltk.tokenize import word_tokenize, sent_tokenize
text = "Machine learning is fun. It helps computers learn from data!"
words = word_tokenize(text)
sentences = sent_tokenize(text)
print("Words:", words)
print("Sentences:", sentences)
OutputSuccess
Important Notes
Tokenization depends on language rules, so results may vary for different languages.
Some punctuation marks are treated as separate tokens.
Make sure to install NLTK and download the required data with nltk.download('punkt') before running.
Summary
Tokenization splits text into words or sentences.
It is the first step in many language tasks.
Use libraries like NLTK for easy tokenization.
Practice
(1/5)
1. What is the main purpose of tokenization in natural language processing?
easy
A. To remove stop words from text
B. To translate text into another language
C. To split text into smaller units like words or sentences
D. To generate new sentences from text
Solution
Step 1: Understand tokenization
Tokenization means breaking text into smaller pieces such as words or sentences.
Step 2: Identify the main goal
The main goal is to prepare text for further processing by splitting it into tokens.
Final Answer:
To split text into smaller units like words or sentences -> Option C
Quick Check:
Tokenization = splitting text [OK]
Hint: Tokenization means cutting text into pieces [OK]
Common Mistakes:
Confusing tokenization with translation
Thinking tokenization removes words
Believing tokenization generates new text
2. Which of the following Python code snippets correctly tokenizes a sentence into words using NLTK?
easy
A. from nltk.tokenize import word_tokenize
sentence = 'Hello world!'
tokens = word_tokenize(sentence)
B. import nltk
sentence = 'Hello world!'
tokens = nltk.split(sentence)
C. from nltk.tokenize import sent_tokenize
sentence = 'Hello world!'
tokens = sent_tokenize(sentence)
D. sentence = 'Hello world!'
tokens = sentence.split_words()
Solution
Step 1: Check correct import and function
The correct function to tokenize words in NLTK is word_tokenize from nltk.tokenize.
Step 2: Verify code correctness
from nltk.tokenize import word_tokenize
sentence = 'Hello world!'
tokens = word_tokenize(sentence) imports word_tokenize and applies it correctly to the sentence.
Final Answer:
from nltk.tokenize import word_tokenize\nsentence = 'Hello world!'\ntokens = word_tokenize(sentence) -> Option A
Quick Check:
Use word_tokenize for word splitting [OK]
Hint: Use word_tokenize from nltk.tokenize for words [OK]
Common Mistakes:
Using sent_tokenize for word tokenization
Calling non-existent split_words() method
Using nltk.split which does not exist
3. What will be the output of this Python code using NLTK?
from nltk.tokenize import sent_tokenize
text = 'Hello world! How are you?'
sentences = sent_tokenize(text)
print(sentences)
medium
A. ['Hello world!', 'How are you?']
B. ['Hello world! How are you?']
C. ['Hello', 'world!', 'How', 'are', 'you?']
D. ['Hello world', 'How are you']
Solution
Step 1: Understand sent_tokenize function
sent_tokenize splits text into sentences based on punctuation.
Step 2: Apply sent_tokenize to the text
The text has two sentences: 'Hello world!' and 'How are you?'.
Final Answer:
['Hello world!', 'How are you?'] -> Option A
Quick Check:
sent_tokenize splits sentences correctly [OK]
Hint: sent_tokenize splits text at sentence ends [OK]
Common Mistakes:
Confusing sent_tokenize with word_tokenize output
Expecting no split for multiple sentences
Ignoring punctuation as sentence boundary
4. Identify the error in this code snippet for word tokenization using NLTK: