Before we get into tokenization, let’s first take a look at what spaCy is. spaCy is a popular library used in Natural Language Processing (NLP). It’s an object-oriented library that helps with processing and analyzing text. We can use spaCy to clean and prepare text, break it into sentences and words and even extract useful information from the text using its various tools and functions. This makes spaCy a great tool for tasks like tokenization, part-of-speech tagging and named entity recognition.
What is Tokenization?
Tokenization is the process of splitting a text or a sentence into segments, which are called tokens. These tokens can be individual words, phrases, or characters depending on the tokenization method used. It is the first step of text preprocessing and is used as input for subsequent processes like text classification, lemmatization and part-of-speech tagging. This step is essential for converting unstructured text into a structured format that can be processed further for tasks such as sentiment analysis, named entity recognition and translation.
Example of Tokenization
This is the sentence: “I love natural language processing!”
After tokenization: [“I”, “love”, “natural”, “language”, “processing”, “!”]
Each token here represents a word or punctuation mark, making it easier for algorithms to process and analyze the text.
Implementation of Tokenization using Spacy Library
Python
import spacy
# Creating blank language object then
# tokenizing words of the sentence
nlp = spacy.blank("en")
doc = nlp("GeeksforGeeks is a one stop\
learning destination for geeks.")
for token in doc:
print(token)
Output:
GeeksforGeeks
is
a
one
stop
learning
destination
for
geeks
.
We can also add functionality in tokens by adding other modules in the pipeline using spacy.load().
Python
nlp = spacy.load("en_core_web_sm")
nlp.pipe_names
Output:
['tok2vec', 'tagger', 'parser', 'attribute_ruler', 'lemmatizer', 'ner']
Here is an example to show what other functionalities can be enhanced by adding modules to the pipeline.
Python
import spacy
# loading modules to the pipeline.
nlp = spacy.load("en_core_web_sm")
# Initialising doc with a sentence.
doc = nlp("If you want to be an excellent programmer \
, be consistent to practice daily on GFG.")
# Using properties of token i.e. Part of Speech and Lemmatization
for token in doc:
print(token, " | ",
spacy.explain(token.pos_),
" | ", token.lemma_)
Output:
If | subordinating conjunction | if
you | pronoun | you
want | verb | want
to | particle | to
be | auxiliary | be
an | determiner | an
excellent | adjective | excellent
programmer | noun | programmer
, | punctuation | ,
be | auxiliary | be
consistent | adjective | consistent
to | particle | to
practice | verb | practice
daily | adverb | daily
on | adposition | on
GFG | proper noun | GFG
. | punctuation | .
In the example above, we utilized part-of-speech (POS) tagging and lemmatization through the spaCy NLP modules. This allowed us to obtain the POS for each word and convert each token to its base form through lemmatization. Prior to loading the NLP model with “en_core_web_sm”, we would not have had access to this functionality. The en_core_web_sm model is essential as it provides the necessary linguistic features, such as tokenization, POS tagging and lemmatization, enabling these advanced NLP capabilities.
Read More:
Similar Reads
What is Tokenization?
Tokenization is a fundamental process in Natural Language Processing (NLP) that involves breaking down a stream of text into smaller units called tokens. These tokens can range from individual characters to full words or phrases, depending on the level of granularity required. By converting text int
5 min read
Rule-Based Tokenization in NLP
Natural Language Processing (NLP) is a subfield of artificial intelligence that aims to enable computers to process, understand, and generate human language. One of the critical tasks in NLP is tokenization, which is the process of splitting text into smaller meaningful units, known as tokens. Dicti
4 min read
Tokenize text using NLTK in python
To run the below python program, (NLTK) natural language toolkit has to be installed in your system.The NLTK module is a massive tool kit, aimed at helping you with the entire Natural Language Processing (NLP) methodology.In order to install NLTK run the following commands in your terminal. sudo pip
3 min read
Subword Tokenization in NLP
Subword Tokenization is a Natural Language Processing technique(NLP) in which a word is split into subwords and these subwords are known as tokens. This technique is used in any NLP task where a model needs to maintain a large vocabulary and complex word structures. The concept behind this, frequent
5 min read
Dictionary Based Tokenization in NLP
Natural Language Processing (NLP) is a subfield of artificial intelligence that aims to enable computers to process, understand, and generate human language. One of the critical tasks in NLP is tokenization, which is the process of splitting text into smaller meaningful units, known as tokens. Dicti
5 min read
Text Analysis Using Turicreate
What is Text Analysis? Text is a group of words or sentences.Text analysis is analyzing the text and then extracting information with the help of text.Text data is one of the biggest factor that can make a company big or small.For example On E-Commerce website people buy things .With Text Analysis t
3 min read
Spam Classification using OpenAI
The majority of people in today's society own a mobile phone, and they all frequently get communications (SMS/email) on their phones. But the key point is that some of the messages you get may be spam, with very few being genuine or important interactions. You may be tricked into providing your pers
6 min read
Build a QnA ChatBot using Gemini Pro
A chatbot is a computer program designed to simulate human conversation, usually through text or voice interactions. They use natural language processing (NLP) and machine learning algorithms to understand and respond to user queries, providing a personalized experience. Gemini is an AI model made b
5 min read
Tokenization with the SentencePiece Python Library
Tokenization is a crucial step in Natural Language Processing (NLP), where text is divided into smaller units, such as words or subwords, that can be further processed by machine learning models. One of the most popular tools for tokenization is the SentencePiece library, developed by Google. This v
5 min read
Construct a Tokens Object Using Quanteda in R
One of the most basic processes in the case of text analysis is tokenization, which means breaking down text into manageable units like words or phrases for further examination. The R quanteda package provides a strong and flexible framework to do this very important step. This is possible through t
3 min read