To a computer, "초파리" ("fruit fly") is a number from the very start. Every character has an assigned number (Unicode), and that number is stored as bytes. To feed a sentence into a neural network, you split it into tokens, give each token an ID, turn each ID into a vector that carries meaning (an embedding), and add information about order. We compute every step ourselves on a small Korean corpus of 36 sentences.
Hangul syllable code = 0xAC00 + (initial × 21 + medial) × 28 + finalUnicode Standard §3.12
Splitting by character gives too many tokens; splitting by word cannot handle words never seen before. BPE finds the middle ground by repeatedly merging the two pieces that most often appear next to each other in the corpus.
After 60 BPE merges, collecting the tokens that appear in the corpus gives a vocabulary. Each token gets the number of its slot in the vocabulary. A token that is not in the vocabulary gets the "unknown" ID.
IDs 54 and 55 are neighboring numbers, but their meanings have nothing to do with each other. So each ID gets a vector with several dimensions. The goal is for words used in similar ways to get nearby coordinates. Here we built an 8-dimensional vector for each of the 55 words from "how often two words appear together within ±2 positions in the same sentence".
The idea that "words that appear with similar words have similar meanings" is old. A representative early method is latent semantic analysis (LSA), which reduces a word–document occurrence table with singular value decomposition to find hidden semantic structure [R2]. This page takes log(1 + count) of the table of how often words appear together, and extracts 8 dimensions with eigendecomposition. Today's language models learn embeddings as part of training to predict the next token. The position information of Section 5 is also added to these vectors.
If you put tokens into a bag and count them without order (a bag of tokens), the order disappears. In Korean, particles mark each word's role, so the meaning often stays the same even when the word order changes — but not always.
Text → tokens → IDs → vectors (+ position) → Transformer (3-8) → probability of the next token. The simplest language model looks only at the previous word and counts the next word. We compute it ourselves with this corpus and see what a Transformer does in addition.
Both code examples use only libraries preinstalled in Colab. After "Download notebook (.ipynb)", open it in Colab with File → Upload notebook, or copy the code and paste it into a new notebook cell.
Computes Unicode and UTF-8, the Hangul jamo formula, BPE training and application, token IDs, embeddings built from co-occurrence counts, and previous-word probabilities without any NLP library. To use your own data, change CORPUS.
Uses a production library to turn sentences into a "sentences × tokens" matrix, compares word-level and character-piece-level splitting, and finds similar sentences with TF-IDF and latent semantic analysis.
These are prompts to paste into AI assistants such as ChatGPT, Qwen, or Claude. Pressing "Copy" attaches the chosen code after the prompt. Replace the parts in 〔 〕 with your own situation.
An AI's answer is only a starting point. Always check any papers, figures, or library functions it suggests against the original sources and official documentation. If you used AI help for a competition entry or paper, disclose the fact and extent of its use according to the rules of the organizers and the journal.
This is the same dictionary as the yellow dotted underlines in the text. The same terms are used throughout the series. Click a section name to jump to where the term first appears.
Analogies are a first step to understanding, but they can also seed misunderstanding. Fruit fly and brain analogies are marked by strength. Direct evidence that they actually do the same thing · Partial only some properties are similar · None no known counterpart
| Concept | Everyday analogy | Fruit fly / brain analogy | Where the analogy breaks down |
|---|---|---|---|
| Unicode | An ID number given to every character in the world | None | The number only tells characters apart; it says nothing about how similar two characters are |
| BPE | Making an abbreviation for words often used together ("초파리" as one chunk) | None | People make abbreviations by looking at meaning, but BPE merges by counts alone. Pieces unrelated to meaning also appear |
| Token ID | A library book's call number | None | Call numbers group books by subject, but token IDs are just Korean alphabetical order or order of appearance |
| Embedding | Coordinates on a map: nearby neighborhoods are similar | Partial The fruit fly's mushroom body represents an odor as an activity pattern (a vector) across many Kenyon cells | The mushroom body spreads an odor widely to make it easy to tell apart (sparse), while an embedding packs it densely into a few hundred dimensions. They are also built differently |
| Bag of tokens | Cutting a sentence into word cards and putting them in an envelope | None | Taking the cards out of the envelope does not tell you the original order. In Korean, the particle cards give some clues |
| Next-token probability | Guessing the next word in a word-chain game | Partial There is research showing that the brain also predicts upcoming speech sounds | The brain's predictions are tied to meaning, situation and bodily experience. The model computes only from statistics of text pieces |
E-1 About the author · E-2 Academic references · E-3 Figure sources
A guide for instructors, on-the-job (OJT) trainers, and tutors. Times are estimates; adjust them to your actual class.
| Audience | University students and practitioners who want to work with text data such as customer inquiries, reviews and documents. Anyone curious about how language models take in text |
|---|---|
| Prerequisites | The idea of vectors and cosine similarity. Knowing position information from 3-8 makes Sections 5 and 6 easier |
| Learning objectives | ① Explain character → Unicode → bytes ② Explain how BPE builds a token vocabulary and how it handles unseen words ③ Explain why IDs, embeddings and position information are each needed |
| Estimated time | Reading 35–45 min · Demo 20 min · Colab practice 25 min (Code 1 15 min, Code 2 10 min) |
Intro 5 min (Section 1, 4-byte emoji) → Section 2 BPE 15 min → Sections 3 and 4 15 min → Section 5 dog bites man 5 min → Section 6 5 min → Check questions 5 min.
Build a corpus from 30 sentences of your team's documents 15 min → change CORPUS in Code 1 and recompute BPE and embeddings 30 min → find similar inquiries with Code 2 25 min → discussion on "unknown" tokens and handling personal data 20 min.
Beginners: Sections 1, 2 and 4 only. Intermediate learners: have them do three BPE merges by hand (count word frequencies → find the most frequent pair), then compare with the rule list in Section 2.
| Section | Questions to pause and ask | Hands-on tips |
|---|---|---|
| 1 Unicode | "Why do texts with the same number of characters take different amounts of storage?" | Order: Korean sentence → "Hello 초파리" → emoji sentence |
| 2 Tokenization | "Why was the Korean ending '한다' merged first?" | 0 → 60 merges, then the sentence with an unseen word |
| 3 Token IDs | "Does it mean anything that ID 54 is bigger than ID 5?" | Point out the "unknown" token in "AI가 꽃을 본다" ("AI sees a flower") |
| 4 Embedding | "We never taught it 'insect', so why did the insects cluster together?" | Compare the kinds of neighbors in the order 초파리는 → 신경망은 |
| 5 Position info | "Korean word order is free, so why is position information needed?" | Press the three comparisons in turn |
| 6 Model processing | "What is the weakness of a model that looks only at the previous word?" | Compare the probabilities for "초파리는" and "꿀벌은" |
| Misconception | Correct explanation | Scene to show |
|---|---|---|
| Token = word | A token is a piece determined by the corpus. It may be a character, a word, or something in between | Section 2 |
| One Hangul character is 1 byte | In UTF-8, a Hangul syllable is 3 bytes | Section 1 |
| Close token IDs mean close meanings | An ID is just a slot position in the vocabulary. Meaning is carried by the embedding | Sections 3 and 4 |
| Close embeddings mean synonyms | Words that are used together also become close. What "similar" means depends on how the embedding is built | Section 4 "신경망은" |
| The model understands text like a person | It computes the probability of the next token with IDs and vectors. Whether it understands is a separate question | Section 6 |
0xAC00 + (0 × 21 + 0) × 28 + 1 = 0xAC01.
3 Hangul characters × 3 + 1 space + 4 for the emoji = 14 bytes.
For every word in the corpus, it counts each pair of adjacent pieces, weighted by how many times the word appears, and adds them up. The pair with the largest count is merged into one.
At the word level it is not in the vocabulary, so the whole word becomes "unknown". With BPE it is split into known pieces, like ["초파리", "들", "이"].
Both are the same: {dog:1, bites:1, man:1}. To tell the order apart, position information is needed.
Add "나비는 꽃을 좋아한다" ("the butterfly likes flowers") and "나비는 날개가 있다" ("the butterfly has wings") to CORPUS in Code 1 and run it again. Check the nearest words to "나비는" (butterfly) and write down why, connecting it to the explanation in Section 4.
Change the number of merges in bpe_train in Code 1 to 10, 30, 60 and 100, and make a table of vocabulary size and average number of tokens per sentence. Explain the trade-off between vocabulary size and token count, and give your reasons for which value you would choose for business documents.
| Beginners | The jamo formula in Section 1 and Section 3 can be skipped without breaking the flow |
|---|---|
| Advanced | Have them research and compare tokenization methods other than BPE (unigram language model, morphological-analyzer-based). Check the papers they find in the original |
| Further learning | Sangsun Ahn, lecture notes for “Data Science for Business” (data science in general) · STEP [K-Digital] AI Natural Language Processing (directly linked to this part) · STEP [K-Digital] Fintech Data Analysis Basics (linked to 3-6) |
| Materials for instructors | Grading criteria for the check questions and sample answers for the assignments are on the answer key for educators (3-5) |
You've read the “3-5 Text” part to the end. Everyone in the lab has a word to sum it up for you.