Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Text Preprocessing: Tokenization

Lesson 6 of 39 in the free Natural Language Processing notes on Siksha Sarovar, written by Rohit Jangra.

Text Preprocessing — Tokenization

Raw text is unstructured and noisy. Before any NLP model can use it, text must go through a preprocessing pipeline. The first step is almost always tokenization.

What is Tokenization?

Tokenization is the process of splitting text into smaller units called tokens — usually words, subwords, or sentences.

TypeSplits IntoExample
Word tokenizationIndividual words/punctuation"NLP is fun!" → ["NLP", "is", "fun", "!"]
Sentence tokenizationIndividual sentences"Hi. How are you?" → ["Hi.", "How are you?"]
Subword tokenizationWord pieces (used by modern LLMs)"unhappiness" → ["un", "happiness"]

Word Tokenization

import nltk
from nltk.tokenize import word_tokenize

text = "Mr. Sharma isn't going to Delhi; he's flying to Mumbai."
tokens = word_tokenize(text)
print(tokens)
# ['Mr.', 'Sharma', 'is', "n't", 'going', 'to', 'Delhi',
#  ';', 'he', "'s", 'flying', 'to', 'Mumbai', '.']

Notice how word_tokenize correctly:

  • Keeps "Mr." together (does not split on the period after an abbreviation)
  • Splits "isn't" into "is" + "n't" (handles contractions)
  • Treats punctuation (";", ".") as separate tokens

Sentence Tokenization

from nltk.tokenize import sent_tokenize

paragraph = "NLP is exciting. It powers chatbots, search engines, and more! Are you ready to learn?"
sentences = sent_tokenize(paragraph)
print(sentences)
# ['NLP is exciting.', 'It powers chatbots, search engines, and more!', 'Are you ready to learn?']

Why Simple .split() Is Not Enough

text = "Dr. Riya's report costs Rs. 1,50,000."
print(text.split())
# ['Dr.', "Riya's", 'report', 'costs', 'Rs.', '1,50,000.']
# Wrong: "Dr." kept with the period attached, punctuation not separated,
# and numbers with commas are mangled -- naive .split() has no linguistic rules.

Whitespace, Punctuation & Regex-based Tokenizers

from nltk.tokenize import RegexpTokenizer

tokenizer = RegexpTokenizer(r'\w+')   # keeps only alphanumeric sequences
print(tokenizer.tokenize("NLP's great, isn't it?"))
# ['NLP', 's', 'great', 'isn', 't', 'it']

Challenges in Tokenization

  1. Abbreviations: "U.S.A." — is each period a sentence boundary?
  2. Contractions: "don't", "I'll" — split into how many tokens?
  3. Hyphenated words: "state-of-the-art" — one token or four?
  4. Numbers with punctuation: "3.14", "1,000", dates like "01/02/2026"
  5. No word boundaries: languages like Chinese/Japanese have no spaces between words at all — tokenization requires a dictionary or statistical model.
  6. Social media text: hashtags, emojis, "@mentions", "gr8", "lol" need special handling.

Tokenization sets the foundation for every downstream step — an incorrect tokenizer choice propagates errors through the entire pipeline.