#
Tokenization 101
#
Necessary Imports and installs
%%bash
pip3 install -U transformers --q
from collections import Counter
import matplotlib.pyplot as plt
from transformers import AutoTokenizer
SAMPLE_SIN = """වර්ෂය පුරාවට ලැබුනු අතිරික්ත ජලය සුරක්ෂිතව තබාගැනීමේ මූලික පරමාර්ථයෙන් වාරි කර්මාන්ත කෙරහි\
ශ්රී ලාංකිකයන් නැඹුරැ වූ බැව් අප මූලිකව අවබෝධකර ගත්තෙමු.එහිදී ජලපහරක් ස්වාභාවිකව පිහිටි උස් භූමි දෙකකට කොටු \
කරමින් එක් පසකින් පමණක් ලී දඬු,කොල අතු වැනි දෑ හරස් කොට පූර්ව යුගයේ වාරි නිර්මාණ සිදු කළද,ඒවායේ වූ තාවකාලික \
බව හේතුවෙන් ක්රම ක්රමයෙන් එම නිර්මාණයන් වෙත දේශීය ඉංජිනේරැ දැනුම භාවිත කරමින් වඩා සංකීර්ණ වූ නිර්මාණ බවට \
ඒවා පරිවර්තනය කළේය.මෙලෙස ක්රමයෙන් ලාංකීය වාරි නිර්මාණ ලොව අන් කිසිඳු රටක දක්නට නොවන ආකාරයෙන් \
සුවිසල් හා විශ්මිත වූ නිර්මාණයන්ගේ තත්වයට වර්ධනය විය. විශේෂයෙන්ම තාක්ෂණයෙන් උසස් යැයි කියා ගන්නා වත්මන් ඉංජිනේරැ \
තාක්ෂණයන්ට පවා හසු නොවන්නා වූ ක්රමවේදයක් අපගේ අතීත වාරි නිර්මාණ ශිල්පීන් සතු වූ බැව් ඇතැම් වාරි නිර්මාණ අපට පසක් කරයි. \
උදාහරණ ලෙස, මින්නේරිය වැව,කලා වැව,පරාක්රම සමුද්රය ආදී වැව් යෝධ ඇළ ආදී වූ නිර්මාණ පෙන්වා දිය හැක.""".replace("\u200d","")
SAMPLE_EN = """GPT-2 is a transformers model pretrained on a very large corpus of English data \
in a self-supervised fashion. This means it was pretrained on the raw texts only, with no humans \
labelling them in any way (which is why it can use lots of publicly available data) with an \
automatic process to generate inputs and labels from those texts. More precisely, \
it was trained to guess the next word in sentences."""
BERT_EN = "prajjwal1/bert-small"
BERT_SIN = "Ransaka/SinhalaRoberta"
SINHALA_TOKENIZER = AutoTokenizer.from_pretrained(BERT_SIN)
ENGLISH_TOKENIZER = AutoTokenizer.from_pretrained(BERT_EN)
tokenizer_config.json: 0%| | 0.00/106 [00:00<?, ?B/s]
tokenizer.json: 0%| | 0.00/1.09M [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/53.0 [00:00<?, ?B/s]
Tokenization stands as a foundational principle in natural language processing (NLP), encompassing the division of a text into smaller entities referred to as tokens. These tokens may take the form of words, characters, or subwords, contingent on the chosen tokenization approach. Upon completion of the tokenization process, each token is associated with a unique identifier. To sum up, the tokenization process functions as a means of transforming textual data into a numerical format.
#
Why We Need Tokenization
Text Understanding: Tokenization enables computers to understand and process human language more effectively by breaking down complex sentences into simpler units.
Feature Extraction: In NLP tasks, tokens serve as features for machine learning models, allowing them to learn patterns and relationships within the data. (Model can't process raw text)
#
Different Types of Tokenization
#
Word-based Tokenization
Word-based tokenization involves breaking down text into individual words. Each word becomes a separate token.
For example:
Input: "Tokenization is essential for NLP."
Tokens: ["Tokenization", "is", "essential", "for", "NLP", "."]
len(SAMPLE_SIN)
839
word_counter = dict(Counter(SAMPLE_SIN.split(" ")))
word_counter = {k: v for k, v in sorted(word_counter.items(), key=lambda item: item[1], reverse=True)}
word_counter
len(word_counter) #vocab size
111
#
Character-based Tokenization
Character-based tokenization breaks text into individual characters. Each character becomes a separate token.
For example:
Input: "Tokenization is essential for NLP."
Tokens: ["T", "o", "k", "e", "n", "i", "z", "a", "t", "i", "o", "n", " ", "i", "s", " ", "e", "s", "s", "e", "n", "t", "i", "a", "l", " ", "f", "o", "r", " ", "N", "L", "P", "."]
characters = set(list(SAMPLE_SIN))
len(characters) #vocab size
50
#
Subword-based Tokenization
Subword-based tokenization involves breaking down text into smaller units that may represent meaningful subwords or partial words. This method is particularly useful for handling rare or out-of-vocabulary words.
For example:
Input: "Tokenization is essential for NLP."
Tokens: ["Token", "iza", "tion", " is", " es", "sen", "tial", " for", " N", "L", "P", "."]
Frequently occured words should not be split into smaller subwords, but rare words should be decomposed into subwords.
Tokenization --> Token + iza + tion (decomposed into subwords)
for --> for (Not splited into subwords)
tokenize_func = lambda text,tokenizer: [tokenizer.convert_ids_to_tokens(token) for token in tokenizer.encode(text) if token not in (tokenizer.sep_token_id,tokenizer.cls_token_id)]
SAMPLE_SIN[:120]
'වර්ෂය පුරාවට ලැබුනු අතිරික්ත ජලය සුරක්ෂිතව තබාගැනීමේ මූලික පරමාර්ථයෙන් වාරි කර්මාන්ත කෙරහි ශ්රී ලාංකිකයන් නැඹුරැ වූ බැව්'
" + ".join(tokenize_func(SAMPLE_SIN[:120],SINHALA_TOKENIZER))
'වර්ෂය + පුරාවට + ලැබුනු + අතිරික්ත + ජලය + සුරක්ෂිතව + තබාගැනීමේ + මූලික + පරමාර්ථය + ##ෙන් + වාරි + කර්මාන්ත + කෙර + ##හි + ශ්රී + ලාංකිකයන් + නැඹුර + ##ැ + වූ + බැව්'
SAMPLE_EN[:120]
'GPT-2 is a transformers model pretrained on a very large corpus of English data in a self-supervised fashion. This means'
" + ".join(tokenize_func(SAMPLE_EN[:120],ENGLISH_TOKENIZER))
'gp + ##t + - + 2 + is + a + transformers + model + pre + ##train + ##ed + on + a + very + large + corpus + of + english + data + in + a + self + - + supervised + fashion + . + this + means'
#
Summary
#
Next Video: Tokenization with Hugging Face Transformers Library
In the upcoming video of this series, we will explore the practical implementation of tokenization using the Hugging Face Transformers Library. This powerful library provides pre-trained models and tokenizers that can be seamlessly integrated into your NLP projects.
Stay tuned!