ICS 582Lecture 02المحاضرة 02Part 05الجزء 05
Corpora, Unicode and UTF-8المدونات النصية وUnicode وUTF-8
What a corpus is and why it must be documented, how Unicode names every character with a code point, and how UTF-8 packs code points into bytes that any model can consume.ما هي المدونة النصية (corpus) ولماذا يجب توثيقها، وكيف يسمي Unicode كل حرف بنقطة ترميز (code point)، وكيف يحزم UTF-8 نقاط الترميز في بايتات يستطيع أي نموذج استهلاكها.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 29-37
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Your research project processes Arabic, and Arabic is where text representation stops being a detail. One letter has four visual shapes, one sound has several hamza spellings, legacy keyboards emit presentation forms, and every letter costs two bytes. A tokenizer that does not know this splits words in places no linguist would recognize.
مشروعك البحثي يعالج العربية، والعربية هي المكان الذي يتوقف فيه تمثيل النص عن كونه تفصيلًا ثانويًا. فللحرف الواحد أربعة أشكال مرئية، وللصوت الواحد عدة كتابات للهمزة، ولوحات المفاتيح القديمة تُخرج أشكال العرض (presentation forms)، وكل حرف يكلّف بايتين. والمقطِّع (tokenizer) الذي لا يعرف ذلك يقسم الكلمات في مواضع لا يمكن لأي لغوي أن يتعرّف عليها.
This part builds the foundation under every tokenizer count in the rest of the course. First it asks what a corpus actually is and what you owe a reader who will trust a model trained on it. Then it descends from characters to numbers (code points), from numbers to bytes (UTF-8), and finally explains why bytes are the floor that byte-level BPE in part 7 stands on. Every corpus you use or build for the project must carry a data statement to be publishable, and every byte count you will ever see in a tokenizer log traces back to the four-row mapping table taught here.
يبني هذا الجزء الأساس الذي تقوم عليه كل أعداد المقطِّع في بقية المقرر. فهو يسأل أولًا: ما المدونة النصية فعلًا، وما الذي تدين به لقارئ سيثق بنموذج دُرِّب عليها؟ ثم ينزل من الحروف إلى الأعداد (نقاط الترميز)، ومن الأعداد إلى البايتات (UTF-8)، ويشرح أخيرًا لماذا البايتات هي الأرضية التي يقوم عليها BPE على مستوى البايت في الجزء 7. كل مدونة تستخدمها أو تبنيها للمشروع يجب أن يصحبها بيان بيانات (data statement) حتى تكون قابلة للنشر، وكل عدد بايتات ستراه يومًا في سجل مقطِّع يعود إلى جدول التحويل ذي الصفوف الأربعة الذي يُدرَّس هنا.
By the end you canفي نهاية هذا الجزء ستستطيع
- Name the variation dimensions of a corpus and list the fields of a data statement or datasheet.سمِّ أبعاد التباين في المدونة النصية (corpus)، واذكر حقول بيان البيانات (data statement) أو صحيفة البيانات (datasheet).
- Distinguish code point, glyph and encoding, and explain why saying Unicode is an encoding is wrong.ميّز بين نقطة الترميز (code point) والرسم الحرفي (glyph) والترميز (encoding)، واشرح لماذا يُعدّ القول إن Unicode ترميزٌ قولًا خاطئًا.
- Apply NFC, NFD and NFKC to precomposed and decomposed Latin and Arabic examples and say which tokenization bug each prevents.طبّق NFC وNFD وNFKC على أمثلة لاتينية وعربية مركّبة مسبقًا ومفكّكة، وبيّن أي خلل في التقطيع (tokenization) يمنعه كل منها.
- Encode any code point into UTF-8 bytes by hand using the four-row table, and decode bytes back.رمّز أي نقطة ترميز إلى بايتات UTF-8 يدويًا باستخدام الجدول ذي الصفوف الأربعة، وفُكّ ترميز البايتات عائدًا إلى نقطة الترميز.
- Explain why bytes remove the unknown-character problem and what they cost.اشرح لماذا تُزيل البايتات مشكلة الحرف المجهول، وما الثمن الذي تدفعه مقابل ذلك.
Two datasets both labelled "Arabic corpus" can be almost unrelated. One is Modern Standard Arabic newswire from 2005, written by trained journalists in a formal register. The other is Gulf-dialect tweets from 2024, full of Arabizi, emoji and code switching into English. A model trained on the first will stumble on the second, and a vocabulary learned from either will miss most of the other. Same language label, different domain, genre, time, demographics and variety.
مجموعتا بيانات تحملان كلتاهما وسم "مدونة عربية" قد لا تكاد تجمعهما صلة. الأولى أخبار بالعربية الفصحى المعاصرة من عام 2005، كتبها صحفيون مدرَّبون بأسلوب رسمي. والثانية تغريدات باللهجة الخليجية من عام 2024، مليئة بالعربيزي والرموز التعبيرية (emoji) والتبديل اللغوي إلى الإنجليزية. النموذج المدرَّب على الأولى سيتعثر في الثانية، والمفردات المتعلَّمة من أيٍّ منهما ستفوّت معظم ما في الأخرى. الوسم اللغوي نفسه، لكن المجال والنوع والزمن والخصائص الديموغرافية والتنوع اللغوي كلها مختلفة.
A Corpus is a structured collection of texts, often annotated, drawn from sources such as books, news, web crawls, social media and speech transcripts. The word "structured" matters: a corpus is a deliberate sample with metadata, not a pile of files. Jurafsky and Martin describe the dimensions along which such samples vary as language and variety (including code switching), genre, the demographics of the writer or speaker, and time. The table below adds domain, which the slides list separately, and gives an Arabic example for each.
المدونة النصية corpus مجموعة منظّمة من النصوص، كثيرًا ما تكون موسومة، مأخوذة من مصادر مثل الكتب والأخبار وعمليات الزحف على الويب ووسائل التواصل الاجتماعي ونصوص الكلام المفرَّغة. وكلمة "منظّمة" مهمة: فالمدونة عيّنة مقصودة مصحوبة ببيانات وصفية (metadata)، وليست كومة من الملفات. ويصف Jurafsky وMartin الأبعاد التي تتباين فيها هذه العينات بأنها: اللغة والتنوع اللغوي (بما في ذلك التبديل اللغوي code switching)، والنوع (genre)، والخصائص الديموغرافية للكاتب أو المتحدث، والزمن. ويضيف الجدول أدناه المجال (domain)، الذي تذكره الشرائح منفصلًا، ويعطي مثالًا عربيًا لكل بعد.
| Dimensionالبعد | Question it answersالسؤال الذي يجيب عنه | Arabic exampleمثال عربي |
|---|---|---|
| Language and varietyاللغة والتنوع اللغوي | Which language, dialect or register, and is there code switching?أي لغة أو لهجة أو مستوى لغوي (register)، وهل فيها تبديل لغوي؟ | Modern Standard Arabic newswire versus Najdi tweets, or Arabizi in Latin lettersأخبار بالعربية الفصحى المعاصرة مقابل تغريدات باللهجة النجدية، أو العربيزي بالحروف اللاتينية |
| Genreالنوع (genre) | What kind of text is it?أي نوع من النصوص هو؟ | Quranic commentary, a parliamentary transcript, a product reviewتفسير قرآني، أو محضر جلسة برلمانية، أو مراجعة منتج |
| Timeالزمن | When was it written or spoken?متى كُتب أو قيل؟ | Pre-2011 news versus post-2020 social media, where vocabulary shiftedأخبار ما قبل 2011 مقابل وسائل التواصل الاجتماعي بعد 2020، حيث تغيّرت المفردات |
| Demographicsالخصائص الديموغرافية | Who wrote or spoke it, by age, gender, region, education?من كتبه أو قاله، بحسب العمر والجنس والمنطقة والتعليم؟ | Teenage gamers in Riyadh versus retired teachers in Cairoمراهقون من محبي ألعاب الفيديو في الرياض مقابل معلمين متقاعدين في القاهرة |
| Domainالمجال (domain) | What is it about?عمّ يتحدث؟ | Medical records, legal contracts, football commentaryسجلات طبية، أو عقود قانونية، أو تعليق على مباريات كرة القدم |
Models inherit their corpusالنماذج ترث مدونتها
A language model is a compressed description of its training corpus, so it inherits the corpus's biases and blind spots. Bender and Friedman argue that systems perform best for the populations represented in their training data, so bias and exclusion arise when that data does not match the people the system is deployed for, and the fix begins with knowing what the data contains. Without that knowledge, a result on a benchmark cannot be interpreted: a 95% accuracy on 2005 newswire says nothing about 2024 tweets. To interpret any result you need the metadata below.
النموذج اللغوي وصف مضغوط لمدونة تدريبه، ولذلك يرث تحيزاتها ونقاطها العمياء. ويرى Bender وFriedman أن الأنظمة تعمل بأفضل أداء مع الفئات الممثَّلة في بيانات تدريبها، فينشأ التحيز والإقصاء حين لا تطابق تلك البيانات الناس الذين يُنشر النظام لخدمتهم، والعلاج يبدأ بمعرفة ما تحتويه البيانات. ومن دون هذه المعرفة لا يمكن تفسير نتيجة على معيار قياس (benchmark): فدقة 95% على أخبار 2005 لا تقول شيئًا عن تغريدات 2024. ولتفسير أي نتيجة تحتاج إلى البيانات الوصفية أدناه.
Metadata a corpus must carry before its results can be interpretedالبيانات الوصفية التي يجب أن تحملها المدونة قبل أن يمكن تفسير نتائجها
- Collection procedureإجراء الجمع
- How texts were sampled or crawled, and what was filtered outكيف أُخذت عينات النصوص أو زُحف عليها، وما الذي استُبعد بالتصفية
- Time periodالفترة الزمنية
- When the texts were produced, not only when they were collectedمتى أُنتجت النصوص، لا متى جُمعت فقط
- Languages and varietiesاللغات والتنوعات اللغوية
- Which languages, dialects and registers, and how much code switchingأي اللغات واللهجات والمستويات اللغوية، وما مقدار التبديل اللغوي
- Speaker or author demographicsالخصائص الديموغرافية للمتحدثين أو الكتّاب
- Age, gender, region, education and any other known attributesالعمر والجنس والمنطقة والتعليم وأي سمات أخرى معروفة
- Licensing and privacyالترخيص والخصوصية
- Who may use the data, for what, and whether consent was obtainedمن يحق له استخدام البيانات، ولأي غرض، وهل أُخذت الموافقة
Data statements and datasheetsبيان البيانات (data statement) وصحيفة البيانات (datasheet)
Two papers turned that metadata list into a standard document. Bender and Friedman (2018) proposed the data statement for NLP datasets, a short structured write-up with lettered sections from curation rationale through language variety, speaker and annotator demographics, speech situation and text characteristics. Gebru and colleagues (2021) proposed the datasheet for any machine learning dataset, borrowing the idea from electronics: a component datasheet lists operating ranges and test conditions, so a dataset should too. The slides merge the two into six fields.
حوّلت ورقتان علميتان قائمة البيانات الوصفية تلك إلى وثيقة قياسية. اقترح Bender وFriedman (2018) بيان البيانات data statement لمجموعات بيانات NLP، وهو وصف قصير منظّم بأقسام مرقّمة بالحروف تمتد من مبررات الانتقاء (curation rationale) مرورًا بالتنوع اللغوي والخصائص الديموغرافية للمتحدثين والموسِّمين، وموقف الكلام، وخصائص النص. واقترح Gebru وزملاؤه (2021) صحيفة البيانات (datasheet) لأي مجموعة بيانات في تعلم الآلة، مستعيرين الفكرة من الإلكترونيات: فصحيفة بيانات المكوِّن الإلكتروني تذكر نطاقات التشغيل وظروف الاختبار، وكذلك ينبغي لمجموعة البيانات أن تفعل. وتدمج الشرائح الاثنين في ستة حقول.
The six datasheet fields, with the matching sections in the two papersحقول صحيفة البيانات الستة، مع الأقسام المقابلة لها في الورقتين
- Motivation and intended useالدافع والاستخدام المقصود
- Why the corpus was built, by whom, funded how, and what tasks it is meant for (Gebru: motivation and uses; Bender and Friedman: A, curation rationale)لماذا بُنيت المدونة، ومن بناها، وكيف مُوّلت، وما المهام المقصودة بها (Gebru: الدافع والاستخدامات؛ Bender وFriedman: A، مبررات الانتقاء)
- Compositionالتركيب
- What the instances are, how many, which languages, varieties and demographics they cover (Gebru: composition; Bender and Friedman: B to F)ما الحالات (instances)، وكم عددها، وأي اللغات والتنوعات اللغوية والخصائص الديموغرافية تغطيها (Gebru: التركيب؛ Bender وFriedman: من B إلى F)
- Collection processعملية الجمع
- How and when the texts were gathered, by crawl, purchase or recording, and who did it (Gebru: collection process; Bender and Friedman: I, provenance)كيف ومتى جُمعت النصوص، بالزحف أو الشراء أو التسجيل، ومن قام بذلك (Gebru: عملية الجمع؛ Bender وFriedman: I، المصدر والمنشأ)
- Preprocessing and annotationالمعالجة المسبقة والتوسيم
- Cleaning, filtering, tokenization and labelling steps, and the annotators' guidelines and demographics (Gebru: preprocessing, cleaning, labeling; Bender and Friedman: D)خطوات التنظيف والتصفية والتقطيع والتوسيم، وإرشادات الموسِّمين وخصائصهم الديموغرافية (Gebru: المعالجة المسبقة والتنظيف والتوسيم؛ Bender وFriedman: D)
- Ethical considerationsالاعتبارات الأخلاقية
- Consent, privacy, sensitive content and the populations who could be harmed by a model trained on it (Gebru: uses and maintenance)الموافقة والخصوصية والمحتوى الحساس والفئات التي قد يتضرر أفرادها من نموذج مدرَّب عليها (Gebru: الاستخدامات والصيانة)
- Distribution constraintsقيود التوزيع
- License, access terms, versioning and who maintains the release (Gebru: distribution and maintenance)الترخيص وشروط الوصول وإدارة الإصدارات ومن يتولى صيانة الإصدار (Gebru: التوزيع والصيانة)
The payoff is transparency, reproducibility and responsible use. Transparency because a reader can judge whether your corpus matches their population. Reproducibility because someone else can rebuild or extend the sample. Responsible use because a datasheet states what the data must not be used for, which is the only place such a limit can live once the files are downloaded.
العائد هو الشفافية وقابلية إعادة الإنتاج والاستخدام المسؤول. الشفافية لأن القارئ يستطيع أن يحكم هل تطابق مدونتك الفئة التي يعمل عليها. وقابلية إعادة الإنتاج لأن شخصًا آخر يستطيع إعادة بناء العينة أو توسيعها. والاستخدام المسؤول لأن صحيفة البيانات تنص على ما يجب ألا تُستخدم فيه البيانات، وهي المكان الوحيد الذي يمكن أن يبقى فيه مثل هذا القيد بعد تنزيل الملفات.
Recall
Name four fields of a datasheet, and state the one-sentence reason a corpus needs one.اذكر أربعة حقول من صحيفة البيانات، واذكر في جملة واحدة لماذا تحتاج المدونة إلى صحيفة بيانات.
Quick checkاختبار سريع
Which datasheet field explains why the corpus was created?أي حقول صحيفة البيانات يشرح سبب إنشاء المدونة؟
Type the word café on two computers. On a Mac the é arrives as one unit, U+00E9. On a system with a decomposed input method it arrives as two, the plain letter U+0065 followed by a combining acute accent U+0301. On screen the two strings are pixel for pixel identical. In a program they are unequal, they have different lengths, and a tokenizer trained on one will split the other into pieces it never saw during training. Arabic has the same trap: a legacy keyboard emits the lam-alef ligature U+FEFB as one character, while a modern one emits U+0644 then U+0627.
اكتب كلمة café على حاسوبين. على جهاز Mac يصل الحرف é وحدةً واحدة، U+00E9. وعلى نظام يستخدم طريقة إدخال مفكَّكة يصل وحدتين: الحرف العادي U+0065 متبوعًا بعلامة الحدة المركِّبة (combining acute accent) U+0301. على الشاشة السلسلتان متطابقتان بكسلًا ببكسل. أما في البرنامج فهما غير متساويتين، وطولاهما مختلفان، والمقطِّع المدرَّب على إحداهما سيقسم الأخرى إلى قطع لم يرها أثناء التدريب قط. وللعربية الفخ نفسه: لوحة المفاتيح القديمة تُخرج رباط اللام ألف U+FEFB حرفًا واحدًا، بينما تُخرج الحديثة U+0644 ثم U+0627.
To reason about this you need three separate words. Unicode assigns every character a Code point, an integer between 0 and 0x10FFFF written with a U+ prefix in hexadecimal, such as U+0061 for the letter a. That gives 1,114,112 possible positions, and the standard covers Latin, Cyrillic, Arabic, Han, emoji and over 170 other scripts within them. A Glyph is the rendered shape a font draws for a code point. The Unicode core specification is explicit that an abstract character should not be confused with a glyph: the code point is the identity, the glyph is one of many possible pictures of it.
لتفكر في هذا تحتاج إلى ثلاث كلمات منفصلة. يعيّن Unicode لكل حرف نقطة ترميز code point، وهي عدد صحيح بين 0 و0x10FFFF يُكتب بالنظام الست عشري مع البادئة U+، مثل U+0061 للحرف a. وهذا يعطي 1,114,112 موضعًا ممكنًا، ويغطي المعيار ضمنها اللاتينية والسيريلية والعربية والهان (Han) والرموز التعبيرية وأكثر من 170 نظام كتابة آخر. أما الرسم الحرفي glyph فهو الشكل الذي يرسمه الخط لنقطة الترميز. والمواصفة الأساسية لـ Unicode صريحة في أن الحرف المجرد لا ينبغي أن يُخلط بالرسم الحرفي: نقطة الترميز هي الهوية، والرسم الحرفي صورة واحدة من صور كثيرة ممكنة لها.
The mapping runs many to many in both directions. One glyph can stand for several code points: the é above is one shape and two encodings, and Latin A (U+0041), Cyrillic А (U+0410) and Greek Α (U+0391) look identical in most fonts. One code point can also have several glyphs: the Arabic letter beh U+0628 is drawn as isolated, initial, medial or final form depending on its neighbours, yet the text stores a single code point and the font chooses the shape.
العلاقة متعدد إلى متعدد في الاتجاهين. فقد يمثّل رسم حرفي واحد عدة نقاط ترميز: الحرف é أعلاه شكل واحد بترميزين، والحرف اللاتيني A (U+0041) والسيريلي А (U+0410) واليوناني Α (U+0391) تبدو متطابقة في معظم الخطوط. وقد يكون لنقطة ترميز واحدة عدة رسوم حرفية: حرف الباء U+0628 يُرسم منفصلًا أو في أول الكلمة أو وسطها أو آخرها بحسب جيرانه، ومع ذلك يخزّن النص نقطة ترميز واحدة ويختار الخط الشكل.
ASCII: the first 128 code pointsASCII: أول 128 نقطة ترميز
The oldest slice of Unicode is ASCII, a 7-bit code with 128 positions defined in RFC 20, which occupies code points U+0000 to U+007F unchanged. The slide shows a fragment of the table in hexadecimal and decimal. The version below adds the binary column, because the binary reveals a fact you will use every time you write a case-folding rule.
أقدم شريحة في Unicode هي ASCII، وهو ترميز بسبع بتات فيه 128 موضعًا، معرَّف في RFC 20، ويشغل نقاط الترميز من U+0000 إلى U+007F دون تغيير. وتعرض الشريحة جزءًا من الجدول بالست عشري والعشري. والنسخة أدناه تضيف العمود الثنائي، لأن التمثيل الثنائي يكشف حقيقة ستستخدمها كلما كتبت قاعدة لتوحيد حالة الأحرف.
| Characterالحرف | Hexست عشري | Decimalعشري | Binaryثنائي |
|---|---|---|---|
| << | 3C3C | 6060 | 0011 11000011 1100 |
| == | 3D3D | 6161 | 0011 11010011 1101 |
| >> | 3E3E | 6262 | 0011 11100011 1110 |
| ?? | 3F3F | 6363 | 0011 11110011 1111 |
| @@ | 4040 | 6464 | 0100 00000100 0000 |
| AA | 4141 | 6565 | 0100 00010100 0001 |
| BB | 4242 | 6666 | 0100 00100100 0010 |
| CC | 4343 | 6767 | 0100 00110100 0011 |
| \\ | 5C5C | 9292 | 0101 11000101 1100 |
| ]] | 5D5D | 9393 | 0101 11010101 1101 |
| ^^ | 5E5E | 9494 | 0101 11100101 1110 |
| __ | 5F5F | 9595 | 0101 11110101 1111 |
| `` | 6060 | 9696 | 0110 00000110 0000 |
| aa | 6161 | 9797 | 0110 00010110 0001 |
| bb | 6262 | 9898 | 0110 00100110 0010 |
| cc | 6363 | 9999 | 0110 00110110 0011 |
Compare A at 0100 0001 with a at 0110 0001. They differ in exactly one bit, bit 5, which is worth 0x20 or 32. Every uppercase letter sits 32 positions before its lowercase partner, so Case folding in ASCII is a single OR with 0x20, which is why it is so fast and why it breaks the moment text leaves ASCII. Arabic has no case, and Greek final sigma has no simple bit rule, so Unicode defines case folding as a table lookup instead.
قارن A عند 0100 0001 بـ a عند 0110 0001. يختلفان في بت واحد بالضبط، البت 5، وقيمته 0x20 أي 32. كل حرف كبير يقع قبل نظيره الصغير بـ 32 موضعًا، لذا فإن توحيد حالة الأحرف case folding في ASCII عملية OR واحدة مع 0x20، ولهذا هو سريع جدًا، ولهذا أيضًا ينكسر لحظة خروج النص عن ASCII. فالعربية لا حالة فيها، وحرف سيغما الختامي اليوناني لا قاعدة بتية بسيطة له، ولذلك يعرّف Unicode توحيد الحالة بالبحث في جدول بدلًا من ذلك.
Normalization: one canonical spelling for each characterالتطبيع: كتابة قياسية واحدة لكل حرف
Back to café. Unicode calls U+00E9 the precomposed form and U+0065 U+0301 the decomposed form, and declares them canonically equivalent: they must be treated as the same character. Arabic has the same pair. Alef with hamza above, U+0623, decomposes canonically to bare alef U+0627 plus the combining hamza above U+0654, as the Unicode code chart records. A different, weaker relation is compatibility equivalence: the lam-alef ligature U+FEFB is compatible with U+0644 U+0627, and the ligature fi (U+FB01) with the two letters f and i. Compatibility variants look different or carry formatting, so folding them loses information.
لنعد إلى café. يسمي Unicode الشكل U+00E9 الشكل المركَّب مسبقًا (precomposed)، والشكل U+0065 U+0301 الشكل المفكَّك (decomposed)، ويعلن أنهما متكافئان قانونيًا canonically equivalent: يجب أن يُعاملا بوصفهما الحرف نفسه. وللعربية الزوج نفسه. فالألف مع همزة فوقها، U+0623، تتفكك قانونيًا إلى ألف مجردة U+0627 مع الهمزة المركِّبة فوقها U+0654، كما يسجّل جدول رموز Unicode. وثمة علاقة أخرى أضعف هي التكافؤ التوافقي (compatibility equivalence): فرباط اللام ألف U+FEFB متوافق مع U+0644 U+0627، والرباط fi (U+FB01) متوافق مع الحرفين f وi. والمتغيرات التوافقية تبدو مختلفة أو تحمل تنسيقًا، لذا فإن طيّها يُفقد معلومات.
Unicode normalization converts text to one of four standard forms defined in Unicode Standard Annex 15. The D forms decompose, the C forms decompose and then recompose, and the K forms also apply the lossy compatibility mappings. The table gives the result for é, for the lam-alef ligature and for the fi ligature, with the UTF-8 byte count of each result so you can see that normalization changes lengths, not only identities.
تطبيع Unicode (Unicode normalization) يحوّل النص إلى أحد أربعة أشكال قياسية معرَّفة في الملحق 15 من معيار Unicode (UAX 15). أشكال D تفكّك، وأشكال C تفكّك ثم تعيد التركيب، وأشكال K تطبّق أيضًا تحويلات التوافق التي تُفقد معلومات. ويعطي الجدول النتيجة للحرف é ولرباط اللام ألف ولرباط fi، مع عدد بايتات UTF-8 لكل نتيجة، لترى أن التطبيع يغيّر الأطوال، لا الهويات فقط.
| Formالشكل | Operationالعملية | é (either spelling)é (بأيٍّ من الكتابتين) | ﻻ U+FEFBﻻ U+FEFB | fi U+FB01fi U+FB01 |
|---|---|---|---|---|
| NFDNFD | Canonical decompositionتفكيك قانوني | U+0065 U+0301 (3 bytes)U+0065 U+0301 (3 بايتات) | U+FEFB (3 bytes)U+FEFB (3 بايتات) | U+FB01 (3 bytes)U+FB01 (3 بايتات) |
| NFCNFC | Canonical decomposition, then canonical compositionتفكيك قانوني، ثم تركيب قانوني | U+00E9 (2 bytes)U+00E9 (بايتان) | U+FEFB (3 bytes)U+FEFB (3 بايتات) | U+FB01 (3 bytes)U+FB01 (3 بايتات) |
| NFKDNFKD | Compatibility decompositionتفكيك توافقي | U+0065 U+0301 (3 bytes)U+0065 U+0301 (3 بايتات) | U+0644 U+0627 (4 bytes)U+0644 U+0627 (4 بايتات) | f i (2 bytes)f i (بايتان) |
| NFKCNFKC | Compatibility decomposition, then canonical compositionتفكيك توافقي، ثم تركيب قانوني | U+00E9 (2 bytes)U+00E9 (بايتان) | U+0644 U+0627 (4 bytes)U+0644 U+0627 (4 بايتات) | f i (2 bytes)f i (بايتان) |
Worked exampleمثال محلول
NFD and NFC on a Latin and an Arabic letterNFD وNFC على حرف لاتيني وحرف عربي
Decompose éفكّك é
NFD(U+00E9) looks up the canonical decomposition and returns U+0065 U+0301, base letter then combining mark. Two code points, three UTF-8 bytes.يبحث NFD(U+00E9) عن التفكيك القانوني ويعيد U+0065 U+0301، الحرف الأساسي ثم العلامة المركِّبة. نقطتا ترميز، وثلاثة بايتات في UTF-8.Recompose éأعد تركيب é
NFC(U+0065 U+0301) first decomposes (no change) and then composes the base with its mark, returning U+00E9. One code point, two bytes. Both input spellings end in the same place.يفكّك NFC(U+0065 U+0301) أولًا (دون تغيير) ثم يركّب الحرف الأساسي مع علامته، فيعيد U+00E9. نقطة ترميز واحدة، وبايتان. وكلتا كتابتي الإدخال تنتهيان في المكان نفسه.Decompose alef with hamzaفكّك الألف المهموزة
NFD(U+0623) returns U+0627 U+0654, and NFC folds that pair back to U+0623. Bare alef U+0627 stays bare under every form: it is a different letter, not a variant spelling.يعيد NFD(U+0623) التسلسل U+0627 U+0654، ويطوي NFC هذا الزوج عائدًا إلى U+0623. أما الألف المجردة U+0627 فتبقى مجردة تحت كل الأشكال: إنها حرف مختلف، لا كتابة بديلة.Fold the ligatureاطوِ الرباط
NFC(U+FEFB) is unchanged, three bytes. NFKC(U+FEFB) returns U+0644 U+0627, two letters and four bytes, because the ligature is only compatibility-equivalent.يبقى NFC(U+FEFB) دون تغيير، ثلاثة بايتات. أما NFKC(U+FEFB) فيعيد U+0644 U+0627، حرفين وأربعة بايتات، لأن الرباط متكافئ توافقيًا فقط.Resultالنتيجة
NFC is the safe default for storage, because it is lossless and shortest. NFKC is the matching form, because it also folds ligatures, fullwidth digits and superscripts, at the price of erasing them.NFC هو الخيار الافتراضي الآمن للتخزين، لأنه لا يُفقد معلومات وهو الأقصر. وNFKC هو شكل المطابقة، لأنه يطوي أيضًا الأربطة والأرقام كاملة العرض (fullwidth) والأحرف العلوية (superscripts)، على حساب محوها.
Here is the tokenizer bug normalization prevents. Suppose a BPE vocabulary is learned on NFC text, so "café" is one frequent token. Now NFD input arrives. The tokenizer sees c a f e U+0301, finds no merge that includes a lone combining mark, and emits "cafe" plus a separate accent token that no training word ever contained. Equality tests, dictionary lookups and Tokenization all fail in the same way, and all three are fixed by normalizing once, at ingestion, before anything else runs. Note that alef normalization in Arabic NLP toolkits, which folds U+0623, U+0625 and U+0622 into bare alef, is an application rule on top of Unicode, not one of the four forms.
هذا هو خلل المقطِّع الذي يمنعه التطبيع. افترض أن مفردات BPE تعلّمت على نص بصيغة NFC، فصارت "café" وحدة (token) واحدة متكررة. ثم وصل إدخال بصيغة NFD. يرى المقطِّع c a f e U+0301، ولا يجد أي دمج يتضمن علامة مركِّبة منفردة، فيُخرج "cafe" ومعها وحدة منفصلة للعلامة لم تحتوها أي كلمة في التدريب. واختبارات التساوي والبحث في القواميس والتقطيع tokenization تفشل كلها بالطريقة نفسها، وكلها تُصلَح بالتطبيع مرة واحدة عند الاستيعاب، قبل أن يعمل أي شيء آخر. ولاحظ أن تطبيع الألف في أدوات معالجة العربية، الذي يطوي U+0623 وU+0625 وU+0622 إلى ألف مجردة، قاعدة تطبيقية فوق Unicode، وليس أحد الأشكال الأربعة.
Recall
Define code point and glyph, and give one example of each direction of the many-to-many mapping.عرّف نقطة الترميز والرسم الحرفي، وأعطِ مثالًا واحدًا لكل اتجاه من اتجاهي العلاقة متعدد إلى متعدد.
Recall
Name two normalization forms and one tokenization bug that normalization prevents.سمِّ شكلين من أشكال التطبيع وخللًا واحدًا في التقطيع يمنعه التطبيع.
Quick checkاختبار سريع
Which statement correctly separates Unicode from UTF-8?أي العبارات تفصل بشكل صحيح بين Unicode وUTF-8؟
Quick checkاختبار سريع
NFC applied to the sequence U+0065 U+0301 produces what?ماذا ينتج عن تطبيق NFC على التسلسل U+0065 U+0301؟
The Arabic word كتاب has four letters, and Python's len agrees: four. Encode it and count the bytes: eight. The English word "Hi" is two and two. The difference is the whole content of this concept. Every Code point above U+007F needs more than one byte, and the rule that decides how many, and which bits go where, is small enough to learn by heart.
كلمة كتاب العربية فيها أربعة حروف، والدالة len في Python توافق: أربعة. رمّزها وعُدّ البايتات: ثمانية. أما الكلمة الإنجليزية "Hi" فاثنان واثنان. هذا الفرق هو مضمون هذا المفهوم كله. فكل نقطة ترميز فوق U+007F تحتاج إلى أكثر من بايت، والقاعدة التي تحدد كم بايتًا، وأي البتات تذهب إلى أين، صغيرة بما يكفي لتحفظها عن ظهر قلب.
UTF-8 is a variable-length encoding form that maps each Unicode scalar value (a code point that is not a surrogate) to a sequence of one to four bytes. It is defined in the Unicode core specification and, for the Internet, in RFC 3629. Code points below 0x80 take one byte, and most other characters take two, three or four. The slide's mapping table is the exact rule, reproduced here with the payload capacity of each row.
UTF-8 صيغة ترميز متغيرة الطول تحوّل كل قيمة عددية في Unicode (scalar value، أي نقطة ترميز ليست بديلًا surrogate) إلى سلسلة من بايت واحد إلى أربعة بايتات. وهي معرَّفة في المواصفة الأساسية لـ Unicode، ولأغراض الإنترنت في RFC 3629. نقاط الترميز تحت 0x80 تأخذ بايتًا واحدًا، ومعظم الحروف الأخرى تأخذ اثنين أو ثلاثة أو أربعة. وجدول التحويل في الشريحة هو القاعدة الدقيقة، وهو منقول هنا مع سعة الحمولة لكل صف.
| Rangeالنطاق | Code point bitsبتات نقطة الترميز | Payload bitsبتات الحمولة | Byte 1البايت 1 | Byte 2البايت 2 | Byte 3البايت 3 | Byte 4البايت 4 |
|---|---|---|---|---|---|---|
| U+0000 to U+007Fمن U+0000 إلى U+007F | 0xxxxxxx0xxxxxxx | 77 | 0xxxxxxx0xxxxxxx | |||
| U+0080 to U+07FFمن U+0080 إلى U+07FF | 00000yyy yyxxxxxx00000yyy yyxxxxxx | 1111 | 110yyyyy110yyyyy | 10xxxxxx10xxxxxx | ||
| U+0800 to U+FFFFمن U+0800 إلى U+FFFF | zzzzyyyy yyxxxxxxzzzzyyyy yyxxxxxx | 1616 | 1110zzzz1110zzzz | 10yyyyyy10yyyyyy | 10xxxxxx10xxxxxx | |
| U+010000 to U+10FFFFمن U+010000 إلى U+10FFFF | 000uuuuu zzzzyyyy yyxxxxxx000uuuuu zzzzyyyy yyxxxxxx | 2121 | 11110uuu11110uuu | 10uuzzzz10uuzzzz | 10yyyyyy10yyyyyy | 10xxxxxx10xxxxxx |
Read the templates as two kinds of byte. The first byte of every sequence is the lead byte, and its run of leading ones announces the length: 0 means one byte, 110 two, 1110 three and 11110 four. Every later byte is a continuation byte and starts 10, leaving six payload bits. The payload capacities are 7, 11, 16 and 21 bits, and the last is no accident: the largest code point U+10FFFF is exactly a 21-bit number, so four bytes suffice for the whole codespace.
اقرأ القوالب بوصفها نوعين من البايتات. البايت الأول في كل سلسلة هو البايت القائد lead byte، وسلسلة الآحاد في بدايته تعلن الطول: 0 تعني بايتًا واحدًا، و110 اثنين، و1110 ثلاثة، و11110 أربعة. وكل بايت بعده بايت استمرار (continuation byte) يبدأ بـ 10، فيترك ست بتات للحمولة. وسعات الحمولة هي 7 و11 و16 و21 بتًا، والأخيرة ليست مصادفة: فأكبر نقطة ترميز U+10FFFF عدد من 21 بتًا بالضبط، لذا تكفي أربعة بايتات لفضاء الترميز كله.
Worked exampleمثال محلول
Encode the Arabic letter beh, U+0628رمّز حرف الباء العربي، U+0628
Write the code point in binaryاكتب نقطة الترميز بالثنائي
0x0628 is 0000 0110 0010 1000. Its value 1576 lies between 0x80 and 0x7FF, so it takes the two-byte row with 11 payload bits.0x0628 هي 0000 0110 0010 1000. وقيمتها 1576 تقع بين 0x80 و0x7FF، لذا تأخذ صف البايتين ذا 11 بت حمولة.Take the low 11 bitsخذ أدنى 11 بتًا
Drop the five leading zeros: 11000 101000. The template is 110yyyyy 10xxxxxx, so yyyyy = 11000 and xxxxxx = 101000.احذف الأصفار الخمسة البادئة: 11000 101000. القالب هو 110yyyyy 10xxxxxx، لذا yyyyy = 11000 وxxxxxx = 101000.Fill the templatesاملأ القوالب
Byte 1 is 110 + 11000 = 11011000 = D8. Byte 2 is 10 + 101000 = 10101000 = A8.البايت 1 هو 110 + 11000 = 11011000 = D8. والبايت 2 هو 10 + 101000 = 10101000 = A8.Resultالنتيجة
U+0628 encodes as D8 A8, two bytes.تُرمَّز U+0628 إلى D8 A8، أي بايتين.
Worked exampleمثال محلول
Encode the grinning face emoji, U+1F600رمّز الرمز التعبيري للوجه المبتسم، U+1F600
Pick the rowاختر الصف
0x1F600 is above 0xFFFF, so it takes the four-byte row with 21 payload bits: 0 0001 1111 0110 0000 0000.0x1F600 أكبر من 0xFFFF، لذا تأخذ صف الأربعة بايتات ذا 21 بت حمولة: 0 0001 1111 0110 0000 0000.Split into u, z, y, x groupsقسّم البتات إلى المجموعات u وz وy وx
uuuuu = 00001, zzzz = 1111, yyyyyy = 011000, xxxxxx = 000000. The five u bits straddle two bytes: the top three go into byte 1 and the low two into byte 2, exactly as the template 11110uuu 10uuzzzz shows.uuuuu = 00001، zzzz = 1111، yyyyyy = 011000، xxxxxx = 000000. وبتات u الخمس تمتد على بايتين: الثلاث العليا تذهب إلى البايت 1 والاثنتان الدنيا إلى البايت 2، تمامًا كما يُظهر القالب 11110uuu 10uuzzzz.Fill the templatesاملأ القوالب
Byte 1: 11110 + 000 = F0. Byte 2: 10 + 01 + 1111 = 10011111 = 9F. Byte 3: 10 + 011000 = 98. Byte 4: 10 + 000000 = 80.البايت 1: 11110 + 000 = F0. البايت 2: 10 + 01 + 1111 = 10011111 = 9F. البايت 3: 10 + 011000 = 98. البايت 4: 10 + 000000 = 80.Resultالنتيجة
U+1F600 encodes as F0 9F 98 80, four bytes.تُرمَّز U+1F600 إلى F0 9F 98 80، أي أربعة بايتات.
Worked exampleمثال محلول
Encode the letter a, U+0061رمّز الحرف a، U+0061
Pick the row and copyاختر الصف وانسخ البتات
0x61 is below 0x80, so the template is 0xxxxxxx and the seven payload bits 1100001 sit under a leading zero: 01100001.0x61 أصغر من 0x80، لذا القالب هو 0xxxxxxx، وبتات الحمولة السبع 1100001 توضع بعد صفر بادئ: 01100001.Resultالنتيجة
U+0061 encodes as 61, the same number as its ASCII code. Nothing changed.تُرمَّز U+0061 إلى 61، وهو العدد نفسه في رمز ASCII. لم يتغير شيء.
| Characterالحرف | Code pointنقطة الترميز | Rangeالنطاق | Bytesالبايتات | UTF-8 (hex)UTF-8 (ست عشري) |
|---|---|---|---|---|
| aa | U+0061U+0061 | U+0000 to U+007Fمن U+0000 إلى U+007F | 11 | 6161 |
| éé | U+00E9U+00E9 | U+0080 to U+07FFمن U+0080 إلى U+07FF | 22 | C3 A9C3 A9 |
| بب | U+0628U+0628 | U+0080 to U+07FFمن U+0080 إلى U+07FF | 22 | D8 A8D8 A8 |
| €€ | U+20ACU+20AC | U+0800 to U+FFFFمن U+0800 إلى U+FFFF | 33 | E2 82 ACE2 82 AC |
| 中中 | U+4E2DU+4E2D | U+0800 to U+FFFFمن U+0800 إلى U+FFFF | 33 | E4 B8 ADE4 B8 AD |
| ﻻﻻ | U+FEFBU+FEFB | U+0800 to U+FFFFمن U+0800 إلى U+FFFF | 33 | EF BB BBEF BB BB |
| 😀😀 | U+1F600U+1F600 | U+010000 to U+10FFFFمن U+010000 إلى U+10FFFF | 44 | F0 9F 98 80F0 9F 98 80 |
Now do it live. The simulator below encodes whatever you type, one character at a time, and shows the row, the payload bits, the templates and the resulting bytes. It preloads Hi كتاب 😀: nine code points, ten UTF-16 units and sixteen bytes. The € 中 preset exercises the three-byte row, the only one with a 1110 lead byte. Switch the normalization control to NFD with the café preset, or to NFKC with the ligature preset, and watch the byte count change before a single byte is encoded.
جرّبها الآن مباشرة. المحاكي أدناه يرمّز كل ما تكتبه، حرفًا حرفًا، ويعرض الصف وبتات الحمولة والقوالب والبايتات الناتجة. وهو يُحمَّل مسبقًا بالنص Hi كتاب 😀: تسع نقاط ترميز، وعشر وحدات UTF-16، وستة عشر بايتًا. والإعداد المسبق € 中 يختبر صف الثلاثة بايتات، وهو الصف الوحيد ذو البايت القائد 1110. حوّل عنصر التحكم في التطبيع إلى NFD مع الإعداد المسبق café، أو إلى NFKC مع الإعداد المسبق للرباط، وراقب عدد البايتات يتغير قبل أن يُرمَّز بايت واحد.
- HU+0048U+0000 to U+007F
1 byte, 7 payload bits1 بايت، 7 بتات حمولةbits 100100048010010000xxxxxxx - iU+0069U+0000 to U+007F
1 byte, 7 payload bits1 بايت، 7 بتات حمولةbits 110100169011010010xxxxxxx - ␠U+0020U+0000 to U+007F
1 byte, 7 payload bits1 بايت، 7 بتات حمولةbits 010000020001000000xxxxxxx - كU+0643U+0080 to U+07FF
2 bytes, 11 payload bits2 بايتات، 11 بتات حمولةbits 11001000011D9 8311011001110yyyyy1000001110xxxxxx - تU+062AU+0080 to U+07FF
2 bytes, 11 payload bits2 بايتات، 11 بتات حمولةbits 11000101010D8 AA11011000110yyyyy1010101010xxxxxx - اU+0627U+0080 to U+07FF
2 bytes, 11 payload bits2 بايتات، 11 بتات حمولةbits 11000100111D8 A711011000110yyyyy1010011110xxxxxx - بU+0628U+0080 to U+07FF
2 bytes, 11 payload bits2 بايتات، 11 بتات حمولةbits 11000101000D8 A811011000110yyyyy1010100010xxxxxx - ␠U+0020U+0000 to U+007F
1 byte, 7 payload bits1 بايت، 7 بتات حمولةbits 010000020001000000xxxxxxx - 😀U+1F600U+010000 to U+10FFFF
4 bytes, 21 payload bits4 بايتات، 21 بتات حمولةbits 000011111011000000000F0 9F 98 801111000011110uuu1001111110uuzzzz1001100010yyyyyy1000000010xxxxxx
Dim cells are the fixed prefix bits of each byte template. Accent cells are the payload bits copied from the code point, left to right. Switch to NFD or NFKC to watch a precomposed letter or a ligature change its byte count before encoding.الخلايا الباهتة هي بتات البادئة الثابتة في قالب كل بايت. والخلايا باللون المميز هي بتات الحمولة المنسوخة من نقطة الترميز من اليسار إلى اليمين. بدّل إلى NFD أو NFKC لترى كيف يتغير عدد بايتات حرف مركّب مسبقًا أو حرف مدمج (ligature) قبل الترميز.
Decoding, and why the prefixes matterفك الترميز، ولماذا تهم البادئات
The prefixes are what make UTF-8 safe to read from any position. A decoder that sees a byte starting 0 knows it is a complete ASCII character. A byte starting 110, 1110 or 11110 announces one, two or three continuation bytes to follow. A byte starting 10 can never begin a character, so a decoder dropped into the middle of a stream steps backwards at most three bytes to find a lead byte. RFC 3629 lists this as a design property (character boundaries are easily found from anywhere in an octet stream), Jurafsky and Martin call it self-synchronizing, and it is why a corrupted byte damages one character rather than the rest of the file.
البادئات هي ما يجعل قراءة UTF-8 آمنة من أي موضع. فمفكِّك الترميز الذي يرى بايتًا يبدأ بـ 0 يعرف أنه حرف ASCII كامل. والبايت الذي يبدأ بـ 110 أو 1110 أو 11110 يعلن أن بعده بايت استمرار واحدًا أو اثنين أو ثلاثة. أما البايت الذي يبدأ بـ 10 فلا يمكن أن يبدأ حرفًا أبدًا، لذا فإن المفكِّك الذي يُلقى في منتصف تدفق يرجع إلى الخلف ثلاثة بايتات على الأكثر ليجد بايتًا قائدًا. ويذكر RFC 3629 هذا بوصفه خاصية تصميمية (حدود الحروف يسهل إيجادها من أي مكان في تدفق الثُّمانيات)، ويسميها Jurafsky وMartin المزامنة الذاتية (self-synchronizing)، وهي سبب أن البايت التالف يُفسد حرفًا واحدًا لا بقية الملف.
Worked exampleمثال محلول
Decode D8 A8 back to a code pointفكّ ترميز D8 A8 عائدًا إلى نقطة ترميز
Read the lead byteاقرأ البايت القائد
D8 is 11011000. It starts 110, so expect exactly one continuation byte and keep the five bits after the prefix: 11000.D8 هو 11011000. يبدأ بـ 110، لذا توقّع بايت استمرار واحدًا بالضبط، واحتفظ بالبتات الخمس التي بعد البادئة: 11000.Read the continuation byteاقرأ بايت الاستمرار
A8 is 10101000. It starts 10 as required, so keep the six bits after the prefix: 101000.A8 هو 10101000. يبدأ بـ 10 كما هو مطلوب، لذا احتفظ بالبتات الست التي بعد البادئة: 101000.Concatenateاربط البتات
11000 + 101000 = 11000101000 = 0x628.اربط الجزأين: 11000 + 101000 = 11000101000 = 0x628.Resultالنتيجة
U+0628, the letter beh. Had the second byte started with anything other than 10, the decoder would report an error at that exact position.U+0628، حرف الباء. ولو بدأ البايت الثاني بأي شيء غير 10 لأبلغ المفكِّك عن خطأ في ذلك الموضع بالضبط.
Three consequences follow from the table and are worth stating explicitly. First, backward compatibility with ASCII: every code point below 0x80 becomes the single byte with the same value, so an ASCII file is already a valid UTF-8 file and a UTF-8 file that happens to contain only ASCII is byte for byte an ASCII file. Second, bytes below 0x80 never appear inside a multibyte sequence, so a program that searches for a space or a newline byte will never find one in the middle of an Arabic letter. Third, some byte values are simply impossible: C0, C1 and F5 to FF never occur in valid UTF-8, because they would encode overlong sequences or code points past U+10FFFF (RFC 3629, section 1, with the syntax that rules them out in section 4).
ثلاث نتائج تترتب على الجدول وتستحق أن تُذكر صراحة. أولًا، التوافق مع ASCII: كل نقطة ترميز تحت 0x80 تصبح بايتًا واحدًا بالقيمة نفسها، لذا فإن ملف ASCII هو أصلًا ملف UTF-8 صالح، وملف UTF-8 لا يحتوي إلا ASCII هو ملف ASCII بايتًا ببايت. ثانيًا، البايتات تحت 0x80 لا تظهر أبدًا داخل سلسلة متعددة البايتات، لذا فإن البرنامج الذي يبحث عن بايت مسافة أو سطر جديد لن يجده أبدًا في منتصف حرف عربي. ثالثًا، بعض قيم البايتات مستحيلة ببساطة: C0 وC1 ومن F5 إلى FF لا تظهر أبدًا في UTF-8 صالح، لأنها سترمّز سلاسل مطوّلة (overlong) أو نقاط ترميز بعد U+10FFFF (RFC 3629، القسم 1، مع الصياغة التي تستبعدها في القسم 4).
Recall
How many UTF-8 bytes do U+0628, U+00E9, U+4E2D and U+1F600 need, and why?كم بايتًا في UTF-8 تحتاج U+0628 وU+00E9 وU+4E2D وU+1F600، ولماذا؟
Recall
Encode ñ, U+00F1, by hand.رمّز ñ، U+00F1، يدويًا.
Recall
Why is UTF-8 backward compatible with ASCII, and how does a decoder find where a character starts?لماذا يتوافق UTF-8 مع ASCII، وكيف يجد مفكِّك الترميز بداية الحرف؟
Quick checkاختبار سريع
How many bytes does UTF-8 use for the Arabic letter beh, U+0628?كم بايتًا يستخدم UTF-8 لحرف الباء العربي، U+0628؟
Quick checkاختبار سريع
A decoder reads a byte whose top bits are 10. What does it conclude?يقرأ مفكِّك الترميز بايتًا بتاته العليا 10. ماذا يستنتج؟
A tokenizer trained with a character vocabulary in 2020 meets the melting face emoji 🫠, added as U+1FAE0 in Unicode 14 the following year. It has never seen the character, so it emits the unknown token. The same happens with an Arabizi spelling like "ktaab" typed into a word-level vocabulary, or any typo, any new script, any rare Han character. Now imagine the base symbols were bytes instead. Every UTF-8 byte is one of exactly 256 values, all of them already in the vocabulary, so there is nothing left to be unknown.
مقطِّع دُرِّب بمفردات من الحروف عام 2020 يصادف الرمز التعبيري للوجه الذائب 🫠، الذي أُضيف بوصفه U+1FAE0 في Unicode 14 في العام التالي. لم يرَ هذا الحرف قط، فيُخرج وحدة المجهول (unknown token). ويحدث الشيء نفسه مع كتابة بالعربيزي مثل "ktaab" تُكتب في مفردات على مستوى الكلمة، أو مع أي خطأ إملائي، أو أي نظام كتابة جديد، أو أي حرف هان نادر. والآن تخيّل أن الرموز الأساسية كانت بايتات بدلًا من ذلك. كل بايت في UTF-8 هو واحد من 256 قيمة بالضبط، وكلها موجودة أصلًا في المفردات، فلا يبقى شيء يمكن أن يكون مجهولًا.
That is Byte-level tokenization: treat the UTF-8 bytes of the text, not its characters, as the atoms. Radford and colleagues motivated it in the GPT-2 paper with the numbers. A Code point base vocabulary would start at over 130,000 symbols before a single merge (about 160,000 in Unicode 17), while a byte-level base needs only 256. Jurafsky and Martin draw the consequence: because there are only 256 possible byte values, there will be no unknown tokens, and the <UNK> symbol becomes unnecessary. Arbitrary scripts, emoji and typos all decompose into bytes the model has seen.
هذا هو التقطيع على مستوى البايت byte-level tokenization: عامِل بايتات UTF-8 للنص، لا حروفه، بوصفها الذرّات. وقد برّره Radford وزملاؤه في ورقة GPT-2 بالأرقام. فمفردات أساسية مبنية على نقاط الترميز ستبدأ بأكثر من 130,000 رمز قبل أي دمج (نحو 160,000 في Unicode 17)، بينما تحتاج القاعدة على مستوى البايت إلى 256 فقط. ويستخلص Jurafsky وMartin النتيجة: لأنه لا توجد إلا 256 قيمة ممكنة للبايت، فلن تكون هناك وحدات مجهولة unknown، ويصبح الرمز <UNK> غير ضروري. فأنظمة الكتابة أيًّا كانت والرموز التعبيرية والأخطاء الإملائية تتفكك كلها إلى بايتات رآها النموذج.
| Propertyالخاصية | Character vocabularyمفردات الحروف | Byte vocabularyمفردات البايتات |
|---|---|---|
| Base symbolsالرموز الأساسية | About 160,000 assigned characters (over 130,000 when GPT-2 was written)نحو 160,000 حرف مُعيَّن (أكثر من 130,000 وقت كتابة ورقة GPT-2) | Exactly 256 byte values256 قيمة بايت بالضبط |
| Unknown input possibleهل يمكن أن يكون الإدخال مجهولًا | Yes, any unseen characterنعم، أي حرف لم يُرَ من قبل | No, every byte is in the baseلا، كل بايت موجود في القاعدة |
| Base units for كتابالوحدات الأساسية لكلمة كتاب | 4 characters4 حروف | 8 bytes8 بايتات |
| Interpretability of a base unitقابلية تفسير الوحدة الأساسية | A letter you can readحرف يمكنك قراءته | Half of an Arabic letterنصف حرف عربي |
| Structureالبنية | Given by the scriptيعطيها نظام الكتابة | Recovered by learned mergesتستعيدها الدمجات المتعلَّمة (merges) |
The price is structure. Bytes do not know where a character ends. The word كتاب becomes eight opaque symbols, and the letter ب is D8 A8, two units that mean nothing on their own. A sentence of Arabic is roughly twice as many base units as Latin text of the same length, which the simulator's كتاب preset shows as 4 characters against 8 bytes. Left alone, a byte model would spend its capacity relearning the UTF-8 table. So bytes are never used alone: they are combined with learned merges that glue frequent byte sequences back into characters, then into subwords. Jurafsky and Martin note that BPE over bytes rediscovers the two- and three-byte UTF-8 sequences early, and that it can also learn invalid sequences straddling a character boundary, which implementations filter out. GPT-2 adds a rule that prevents merges across character categories, because naive byte merges were sub-optimal.
الثمن هو البنية. فالبايتات لا تعرف أين ينتهي الحرف. كلمة كتاب تصبح ثمانية رموز معتمة، وحرف ب هو D8 A8، وحدتان لا تعنيان شيئًا بمفردهما. والجملة العربية تساوي تقريبًا ضعف عدد الوحدات الأساسية لنص لاتيني بالطول نفسه، وهو ما يُظهره الإعداد المسبق كتاب في المحاكي: 4 حروف مقابل 8 بايتات. ولو تُرك نموذج البايتات وحده لأنفق سعته في إعادة تعلّم جدول UTF-8. لذا لا تُستخدم البايتات وحدها أبدًا: بل تُجمع مع دمجات merges متعلَّمة تلصق سلاسل البايتات المتكررة لتعيدها حروفًا، ثم وحدات دون الكلمة subwords. ويلاحظ Jurafsky وMartin أن BPE على البايتات يعيد اكتشاف سلاسل UTF-8 ذات البايتين والثلاثة بايتات مبكرًا، وأنه قد يتعلم أيضًا سلاسل غير صالحة تمتد عبر حدود الحرف، وتقوم التطبيقات بتصفيتها. ويضيف GPT-2 قاعدة تمنع الدمج عبر فئات الحروف، لأن دمج البايتات الساذج كان دون المستوى الأمثل.
Recall
What does Python return for len of the string كتاب, and for len of its UTF-8 encoding? Which number does a byte-level tokenizer start from?ماذا يعيد Python للدالة len على السلسلة كتاب، ولـ len على ترميزها بـ UTF-8؟ ومن أي الرقمين يبدأ المقطِّع على مستوى البايت؟
Quick checkاختبار سريع
How large is the base vocabulary of a byte-level tokenizer before any merges?ما حجم المفردات الأساسية للمقطِّع على مستوى البايت قبل أي دمج؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- A corpus is a situated sample that varies by domain, genre, time, demographics and variety. Models inherit its limits, so document it with a data statement or datasheet.المدونة النصية عيّنة لها سياقها، تتباين بحسب المجال والنوع والزمن والخصائص الديموغرافية والتنوع اللغوي. والنماذج ترث حدودها، لذا وثّقها ببيان بيانات أو صحيفة بيانات.
- Datasheet fields: motivation and intended use, composition, collection process, preprocessing and annotation, ethical considerations, distribution constraints.حقول صحيفة البيانات: الدافع والاستخدام المقصود، والتركيب، وعملية الجمع، والمعالجة المسبقة والتوسيم، والاعتبارات الأخلاقية، وقيود التوزيع.
- A code point is an integer from U+0000 to U+10FFFF. A glyph is the drawn shape. The two map many to many.نقطة الترميز عدد صحيح من U+0000 إلى U+10FFFF. والرسم الحرفي (glyph) هو الشكل المرسوم. والعلاقة بينهما متعدد إلى متعدد.
- ASCII is the first 128 code points, and upper and lower case letters differ by exactly 0x20.ASCII هو أول 128 نقطة ترميز، والحرف الكبير يختلف عن الصغير بمقدار 0x20 بالضبط.
- NFC and NFD make é and e plus U+0301 compare and tokenize alike. Only NFKC and NFKD also fold the ligature ﻻ into ل plus ا.يجعل NFC وNFD الحرف é والتسلسل e مع U+0301 متطابقين في المقارنة والتقطيع. أما NFKC وNFKD فوحدهما يفكّان أيضًا الرباط ﻻ إلى ل مع ا.
- UTF-8 uses 1, 2, 3 or 4 bytes with lead patterns 0, 110, 1110, 11110 and continuation bytes 10xxxxxx. Payloads hold 7, 11, 16 and 21 bits.يستخدم UTF-8 بايتًا واحدًا أو 2 أو 3 أو 4 بايتات، بأنماط بايت قائد 0 و110 و1110 و11110، وبايتات استمرار على شكل 10xxxxxx. وتسع الحمولات 7 و11 و16 و21 بتًا.
- U+0628 is D8 A8, U+00E9 is C3 A9, U+4E2D is E4 B8 AD, U+1F600 is F0 9F 98 80, and a is 61.U+0628 هو D8 A8، وU+00E9 هو C3 A9، وU+4E2D هو E4 B8 AD، وU+1F600 هو F0 9F 98 80، والحرف a هو 61.
- String length counts code points, not bytes: كتاب is 4 code points and 8 bytes.طول السلسلة يعدّ نقاط الترميز لا البايتات: كلمة كتاب 4 نقاط ترميز و8 بايتات.
- Bytes give a 256-symbol base with no unknown character. Learned merges restore the structure that bytes lose.تعطي البايتات قاعدة من 256 رمزًا بلا حرف مجهول. والدمجات المتعلَّمة (merges) تعيد البنية التي تفقدها البايتات.
Sourcesالمصادر
- Speech and Language Processing, chapter 2: Words and TokensBookJurafsky and Martin, 3rd edition draftSections 2.3 on code points, UTF-8 and its self-synchronizing property, 2.4.3 on byte-level BPE, 2.5 on corpora and datasheetsالقسم 2.3 عن نقاط الترميز وUTF-8 وخاصية المزامنة الذاتية فيه، والقسم 2.4.3 عن BPE على مستوى البايت، والقسم 2.5 عن المدونات النصية وصحف البيانات(opens in a new tab)(يفتح في تبويب جديد)
- RFC 3629: UTF-8, a transformation format of ISO 10646RFCIETFByte templates (section 3); section 1 lists ASCII compatibility, character boundaries findable from any position, and the bytes C0, C1, F5 to FF never appearingقوالب البايتات (القسم 3)؛ ويذكر القسم 1 التوافق مع ASCII، وإمكان إيجاد حدود الحروف من أي موضع، وأن البايتات C0 وC1 ومن F5 إلى FF لا تظهر أبدًا(opens in a new tab)(يفتح في تبويب جديد)
- The Unicode Standard, Version 17.0, chapter 3: ConformanceDocsUnicode ConsortiumAbstract character versus glyph (D7), codespace and code point (D9, D10), UTF-8 encoding form (D92)الحرف المجرد مقابل الرسم الحرفي (D7)، وفضاء الترميز ونقطة الترميز (D9 وD10)، وصيغة الترميز UTF-8 (D92)(opens in a new tab)(يفتح في تبويب جديد)
- Unicode Standard Annex 15: Unicode Normalization FormsDocsUnicode ConsortiumTable 1 defines NFD, NFC, NFKD and NFKCالجدول 1 يعرّف NFD وNFC وNFKD وNFKC(opens in a new tab)(يفتح في تبويب جديد)
- FAQ: UTF-8, UTF-16, UTF-32 and BOMDocsUnicode ConsortiumUnicode versus encoding forms, the 21-bit codespace, UTF-16 in Java and WindowsUnicode مقابل صيغ الترميز، وفضاء الترميز ذو 21 بتًا، وUTF-16 في Java وWindows(opens in a new tab)(يفتح في تبويب جديد)
- Arabic code chart, U+0600 to U+06FFDocsUnicode ConsortiumU+0623 decomposes canonically to U+0627 U+0654; U+0628 ARABIC LETTER BEHU+0623 تتفكك قانونيًا إلى U+0627 U+0654؛ وU+0628 هي حرف الباء العربي (ARABIC LETTER BEH)(opens in a new tab)(يفتح في تبويب جديد)
- Arabic Presentation Forms-B code chart, U+FE70 to U+FEFFDocsUnicode ConsortiumU+FEFB is compatibility-equivalent to U+0644 U+0627U+FEFB متكافئة توافقيًا مع U+0644 U+0627(opens in a new tab)(يفتح في تبويب جديد)
- RFC 20: ASCII format for network interchangeRFCIETF7-bit code, 0x5B is [, 0x5D is ], A is 0x41 and a is 0x61ترميز بسبع بتات، و0x5B هو [، و0x5D هو ]، وA هو 0x41، وa هو 0x61(opens in a new tab)(يفتح في تبويب جديد)
- Unicode HOWTODocsPython documentationlen counts code points; encode returns bytes; unicodedata.normalizeالدالة len تعدّ نقاط الترميز؛ والدالة encode تعيد بايتات؛ والدالة unicodedata.normalize(opens in a new tab)(يفتح في تبويب جديد)
- Data Statements for Natural Language Processing: Toward Mitigating System Bias and Enabling Better SciencePaperTransactions of the ACL, Bender and Friedman, 2018Schema sections A to I; systems work best for the populations represented in their training dataأقسام المخطط من A إلى I؛ والأنظمة تعمل بأفضل أداء مع الفئات الممثَّلة في بيانات تدريبها(opens in a new tab)(يفتح في تبويب جديد)
- Datasheets for DatasetsPaperCommunications of the ACM, Gebru et al., December 2021Motivation, composition, collection, preprocessing, uses, distribution, maintenance; the electronics datasheet analogyالدافع، والتركيب، والجمع، والمعالجة المسبقة، والاستخدامات، والتوزيع، والصيانة؛ وتشبيه صحيفة بيانات المكوّنات الإلكترونية(opens in a new tab)(يفتح في تبويب جديد)
- Language Models are Unsupervised Multitask LearnersPaperOpenAI, Radford et al., 2019Section 2.2: over 130,000 code point symbols versus a 256-symbol byte base; merges blocked across character categoriesالقسم 2.2: أكثر من 130,000 رمز من نقاط الترميز مقابل قاعدة بايتات من 256 رمزًا؛ ومنع الدمج عبر فئات الحروف(opens in a new tab)(يفتح في تبويب جديد)
- Usage statistics of UTF-8 for websitesArticleW3TechsUTF-8 used by 99.1% of websites with a known encoding, September 2026يستخدم UTF-8 في 99.1% من المواقع المعروف ترميزها، سبتمبر 2026(opens in a new tab)(يفتح في تبويب جديد)