文字からバイトへ:コードポイントと UTF-8
Agent支援エンジニアリング確認
前提条件
Day 36 を完了していること。詰まった場合は Day 35 の回復リンクで確率と Python を復習すること。
学習目標
目に見える文字、その Unicode コードポイント、その UTF-8 バイト列を区別する。測定前にバイト数を予測し、空文字列・ASCII・アクセント付き・CJK・絵文字について予測と観測の比較を記録する。
手順
文字は読者に見えるもの、コードポイントはその Unicode 番号(U+XXXX と表記)、エンコーディングはコードポイントをバイト列に変換する規則である。必須作業:予測してから python3 -c "s='猫aé😀'; print(len(s), [hex(ord(c)) for c in s], list(s.encode('utf-8')))" を実行し、各予測と比較する。任意作業:同じ文字列を UTF-16 でエンコードし、バイト数を比較する。
演習と予測記録
s='猫aé😀' と空文字列 '' について、len(s)、コードポイント、UTF-8 バイト列を予測する。次に Python で検証し、なぜバイト数がコードポイント数と異なるかを説明する。
UTF-8 は ASCII に 1 バイト、多くのアクセント付きラテン文字に 2 バイト、CJK に 3 バイト、多くの絵文字に 4 バイトを使う。ord() はコードポイントを返し、.encode('utf-8') はバイト列を返す。
s='猫aé😀' は 4 個のコードポイント ― U+732B(猫)、U+0061(a)、U+00E9(é)、U+1F600(😀)― だが UTF-8 では 10 バイトになる:猫→[231,140,171]、a→[97]、é→[195,169]、😀→[240,159,152,128]。空文字列はコードポイント 0、バイト 0。UTF-8 の長さはコードポイントの範囲に依存するため、文字数とバイト数は一般に一致しない。
空文字列を含むすべての探索文字列で、書いた予測が観測された ord() と encode() の出力と一致し、コードポイント範囲から UTF-8 バイト数への対応規則を述べられること。
公式ソースリソースと権利証跡
Python Unicode HOWTO
該当セクション・範囲: Sections: Definitions of Character, Code Point, and Encodings (UTF-8)
検証バージョンと日付: Python 3.12 documentation (Verified: 2026-10-30)
Normative reference for Python 3 code points, encodings, and variable-length UTF-8 byte behavior used in every tokenization step.
Basic Python strings and byte literals
The lesson defines every required Unicode concept locally and all commands run offline.
比較対象候補リソースと選定評価 (2)
Exhaustive normative standard but far too deep for a first contact; the Python HOWTO teaches the same code point/encoding distinction in executable form.
Strong motivation and history but pre-Python-3 and not runnable; retained as optional background reading only.
minbpe: Minimal Byte-Pair Encoding tokenizer (Let's build the GPT Tokenizer)
該当セクション・範囲: README exercise flow and minbpe/base.py; lecture segment on Unicode code points vs raw bytes
検証バージョンと日付: commit 1acefe9 (Verified: 2026-10-30)
Standard teaching reference connecting Unicode code points, UTF-8 bytes, and subword token ids for GPT-style tokenizers.
Basic Python and strings
The lesson re-implements the same algorithms locally in pure Python with verified outputs.
比較対象候補リソースと選定評価 (3)
Production BPE with Rust core and regex pre-tokenization; selected as the production contrast source rather than the primary teaching implementation.
High-quality BPE coverage but frames tokenization through library APIs; minbpe exposes the raw merge loop for hand tracing.
Industrial tokenizer trainer supporting BPE and unigram models; C++ core hides the teaching mechanics.