Skip to content
James Bower
James Bower

Quantitative AI Security

  • Tools
  • Audit
  • Guides
  • Courses
  • Newsletter
James Bower

Quantitative AI Security

Unlocking The Secrets Of NLP: A Deep Dive Into Text Tokenization

James Bower, September 12, 2009April 21, 2023

Introduction:

So what is NLP text tokenization anyways? Well, text tokenization is the process of breaking down a piece of text into smaller units, called tokens. Think about it like this, imagine you have a long sentence or a paragraph, and you want to understand it better. To do this, you might separate the words or even the letters to study them more closely. In computer terms, these smaller pieces are called “tokens.” Tokenization helps computers read and understand text, just like you and I do.

Here’s an example: Let’s say we have a sentence, “I love the care bears.” To tokenize this sentence, we would break it up into smaller pieces like this: “I”, “love”, “the”, “care”, “bears”. Now, we have five sweet and gentle tokens that we can send into our dark AI abyss below.

These tokens are the building blocks of language and serve as the foundation for various NLP tasks that we may want to perform. So, in this tutorial, I’ll be will discussing different tokenization techniques using a very popular Python library called NLTK (Natural Language Toolkit), for tokenization. I’ll will also provide examples of tokenizing text with Python using NLTK and cover advanced tokenization techniques if I feel up to it.

Why is NLP Text Tokenization Important?

Tokenization is crucial in NLP because it enables computers to make sense of human language by breaking it down into manageable chunks. This allows for more accurate and efficient NLP tasks, such as text summarization, sentiment analysis, and information extraction.

Tokenization Techniques

Word Tokenization

Word tokenization involves breaking text into individual words based on whitespace characters, such as spaces, tabs, and line breaks, or punctuation marks. So exactly what we saw in our care bears example above. RIP Care Bears

Sentence Tokenization

Sentence tokenization focuses on breaking text into sentences, typically using punctuation marks, such as periods, question marks, and exclamation points, as well as capitalization cues. So depending on your objective you may want to break down your text into either words or sentences.

Using NLTK (Natural Language Toolkit)

The NLTK (Natural Language Toolkit) is a popular Python library for NLP tasks, including text tokenization and the only one I’ve worked with at this time. The toolkit provides a pretty comprehensive suite of tools for processing and analyzing text data.

Dataset

For our text file that I’ll be using for our testing, typically called a “corpus” in NLP terms. I’ll be using the Senate Intelligence text file I created in a previous blog post here:

How to Convert Threat Intelligence PDF into Text

Or just keep following along with the code as it’s included.

NLP Text Tokenizing with NLTK

Installing NLTK

So to begin using NLTK in Python 2.6, you first need to install the library. This can be done using the following command:

pip install nltk

Word Tokenization Example

To perform word tokenization, I’m going to use the nltk.word_tokenize() function:

First I’ll download my text file to use.

wget https://www.dropbox.com/s/gsk1gieyihg3od0/threat_assessment.txt?dl=0 -O threat_assessment.txt
import nltk
nltk.download('punkt')
f = open('threat_assessment.txt','r')
text = f.read(1000) # I only want to experiment with the first page or so.
#print (text)
word_tokens = nltk.word_tokenize(text)
print (word_tokens)
f.close()

You get an output like this.

['Annual', 'Threat', 'Assessment', 'of', 'the', 'Intelligence', 'Community', 'for', 'the', 'Senate', 'Select', 'Committee', 'on', 'Intelligence', 'Dennis', 'C.', 'Blair', etc]

Sentence Tokenization Example

For sentence tokenization, I’ll use the nltk.sent_tokenize() function:

import nltk
nltk.download('punkt')
f = open('threat_assessment.txt','r')
text = f.read(10000)
sentence_tokens = nltk.sent_tokenize(text)
#f.close()
print (sentence_tokens)

And this is what I got.

['Annual Threat Assessment of the  \n\nIntelligence Community \n\nfor the Senate Select Committee on Intelligence \n\nDennis C. Blair \n\nDirector of National Intelligence \n\n12 February 2009 \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\x0cFebruary 2009 \n\nSENATE SELECT COMMITTEE ON \nINTELLIGENCE \n\nFEBRUARY 2009  \n\nINTELLIGENCE COMMUNITY \nANNUAL THREAT ASSESSMENT \n\nUNCLASSIFIED \nSTATEMENT FOR THE RECORD \n\nChairman Feinstein, Vice Chairman Bond, Members of the \nCommittee, thank you for the invitation to offer my assessment \nof threats to US national security.',

Advanced Tokenization Techniques

Custom Tokenization Using Regular Expressions

To create a custom tokenizer using regular expressions, use the nltk.RegexpTokenizer() function and provide a pattern:

import nltk
nltk.download('punkt')
tokenizer = nltk.RegexpTokenizer(r'\w+') # Mess around with the regex here.
tokenizer.tokenize(text)

And messing with the regex gives us this.

['Annual',
 'Threat',
 'Assessment',
 'of',
 'the',
 'Intelligence',
 'Community',
 'for',
 'the',
 'Senate',
 'Select',
 'Committee',
 'on',
 'Intelligence',

Tokenizing Non-English Languages

So I threw this one in here as well but haven’t played with a lot before. I didn’t have a great example of non-english text to show but thought the code was at least worth something. So to tokenize text in non-English languages, I’m looking at you Russia! You have to load the appropriate tokenizer for that language. First, import the nltk.data module and then load the language-specific tokenizer:

import nltk.data
other_language_tokenizer = nltk.data.load('tokenizers/punkt/PY3/[language of your choice].pickle')

And thats that! I hope you have found this useful. Also make sure to keep coming back to my blog as I’m going to be putting out more text analytics and NLP posts here:

https://www.jamesbower.com/category/nlp/

And as always, thank you for taking the time to read this. If you have any comments, questions, or critiques, please reach out to me on our FREE ML Security Discord Server – HERE

NLP Text Analytics NLTK

Post navigation

Previous post
Next post

Related Posts

NLP

Applying Game Theory For Use In Cyber Deception

June 28, 2019November 9, 2023

The idea of using game theory to “manipulate” an attacker implies influencing their strategic choices to our advantage. Here are my thoughts on how game theory might benefit us in such an attempt:

Read More
NLP

Decision Intelligence and Cyber Espionage: Navigating the Digital Shadows Now

August 31, 2023September 1, 2023

In the digital age, where secrets are coded and hidden in binary, what’s the key to uncovering the truths buried deep within cyberspace? Unraveling the answer: Decision Intelligence, the ace up the sleeve for cyber operatives worldwide. Introduction Cyber espionage, the stealth art of the 21st century. While some seek…

Read More
Text Analytics

How to Convert Threat Intelligence PDF into Text

September 2, 2009April 21, 2023

Here’s your guide to effortlessly convert threat intelligence from cumbersome PDFs (blah to work with), into easily accessible text, streamlining your cybersecurity analysis and response like never before! I’m sure like me you’ve been struggling to convert threat intelligence PDFs into text? Well I hear you friend. As you know…

Read More
  • Twitter
  • GitHub
  • YouTube
  • LinkedIn

Videos

https://www.youtube.com/watch?v=FEb8KZoEyzI&t=1291s

Categories

  • Active Defense
  • Anomaly Detection
  • AWS
  • Books
  • Business
  • CTF
  • Decision Intelligence
  • Deep Learning
  • Detection Engineering
  • EDA
  • Entity Recognition
  • Health
  • Linux
  • LLM
  • Machine Learning
  • Memory Forensics
  • NIDS
  • NLG
  • NLP
  • Operating Systems
  • Productivity
  • Python
  • Quantitative Security
  • Security Tools
  • Sentiment Analysis
  • Tech
  • Text Analytics
  • Threat Hunting
  • Threat Intelligence
  • Topic Modeling
  • UEBA

Recent Posts

  • Detection Rule Staleness: How AI Can Solve This Now
  • How to Think About Low-Latency Development with Python
  • Managing Noise In AI Security Machine Learning
  • Dimensionality Reduction: Find the Best Method for Your Data
  • Improved Anomaly Detection in Cybersecurity using Model Cascading

Tags

Apache BERT Boot2Root classification Conda Data Augmentation ddos Diet Dirb Ensemble ETL featured Firewall FreeBSD GPT-3 Hacking Hacking Challenge Htop Kioptrix LiME LLM low-latency Miniconda Mod_jk Monitoring NetworkMiner Nikto NLP NLTK Nmap Nokia 770 OpenAI OpenBSD PCAP pdfminer Peak Performance PF recon Security SSM Tomcat Volatility VSCode Word2Vec xgboost

Archives

  • April 2026
  • November 2024
  • October 2024
  • June 2024
  • October 2023
  • September 2023
  • August 2023
  • May 2023
  • April 2023
  • February 2023
  • March 2020
  • June 2019
  • July 2018
  • May 2018
  • August 2017
  • June 2017
  • February 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • November 2015
  • August 2015
  • June 2015
  • February 2015
  • September 2014
  • August 2014
  • July 2014
  • January 2014
  • June 2013
  • January 2012
  • September 2009
  • August 2009
  • September 2008
  • September 2006
©2026 James Bower | WordPress Theme by SuperbThemes