Majid Al-RaimiTokenizers in practice and sentence segmentationالمُقسِّمات (Tokenizers) عمليا وتقسيم الجمل

ICS 582Lecture 02المحاضرة 02Part 07الجزء 07

Tokenizers in practice and sentence segmentationالمُقسِّمات (Tokenizers) عمليا وتقسيم الجمل

Pre-tokenizers, byte-level BPE, multilingual fairness, SuperBPE, the design checklist for a real tokenizer, and the related problem of finding sentence boundaries.المُقسِّمات المسبقة (pre-tokenizers)، وBPE على مستوى البايت، والعدالة بين اللغات، وSuperBPE، وقائمة التحقق لتصميم مُقسِّم حقيقي، والمشكلة المرتبطة بإيجاد حدود الجمل.

Conceptsالمفاهيم
4
Slidesالشرائح
49-55
Readingمدة القراءة
24 min24 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

Part 06 gave you the BPE algorithm on a toy corpus. This part shows what happens between that algorithm and the tokenizer inside GPT-4o or Llama 3, and why the answer matters for an Arabic NLP researcher more than for most people. An English-centric tokenizer can double or triple the cost of Arabic input and shortens the context window a model can spend on it, and every choice that causes that was made by a designer before training started.

We start from one real sentence and account for every split. That takes us through the pre-tokenizer, byte-level BPE and the reason a token can cut an Arabic letter in half. We then measure who pays for a shared vocabulary, look at SuperBPE, which lets merges cross spaces, and compress the whole design space into a checklist. The part closes with sentence segmentation, the first step of any parsing or translation pipeline, where one glyph, the period, can end a sentence, close an abbreviation or sit inside a number.

أعطاك الجزء 06 خوارزمية BPE على مدوّنة نصية صغيرة للتجربة. هذا الجزء يبيّن ما يحدث بين تلك الخوارزمية وبين الـ tokenizer الموجود داخل GPT-4o أو Llama 3، ولماذا يهمّ الجواب باحثًا في معالجة اللغة العربية أكثر مما يهمّ معظم الناس. فالـ tokenizer المتمحور حول الإنجليزية قد يضاعف تكلفة المدخلات العربية مرتين أو ثلاثًا، ويقلّص نافذة السياق التي يستطيع النموذج إنفاقها عليها، وكل قرار يسبّب ذلك اتخذه مصمّم قبل أن يبدأ التدريب.

نبدأ من جملة حقيقية واحدة ونفسّر كل قطع فيها. هذا يمرّ بنا على الـ pre-tokenizer، وعلى byte-level BPE، وعلى سبب قدرة الـ token على قطع حرف عربي إلى نصفين. ثم نقيس من يدفع ثمن المفردات المشتركة، وننظر في SuperBPE الذي يسمح لعمليات الدمج بعبور المسافات، ونضغط فضاء التصميم كله في قائمة مراجعة. ويُختم الجزء بتقسيم النص إلى جمل (sentence segmentation)، وهو الخطوة الأولى في أي خط تحليل نحوي أو ترجمة، حيث يمكن لرمز واحد، هو النقطة، أن ينهي جملة، أو يغلق اختصارًا، أو يقع داخل عدد.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Read a real tokenization such as the slide sentence and explain every split by a pre-tokenizer rule or a BPE merge.أن تقرأ تقطيعًا حقيقيًا مثل جملة الشريحة وتفسّر كل قطع فيه إما بقاعدة من قواعد الـ pre-tokenizer أو بعملية دمج (merge) في BPE.
  2. State the pros and the con of byte-level BPE with a concrete byte split of an Arabic letter.أن تذكر مزايا byte-level BPE وعيبه، مع مثال ملموس لتقطيع حرف عربي على مستوى البايتات.
  3. Compute a tokenization premium and name three costs of over-segmenting a low-resource language.أن تحسب tokenization premium وتسمّي ثلاث تكاليف لتقطيع لغة قليلة الموارد إلى أجزاء كثيرة (over-segmentation).
  4. Explain what SuperBPE changes in the BPE curriculum and what it trades away.أن تشرح ما الذي يغيّره SuperBPE في منهج تدريب BPE وما الذي يتنازل عنه مقابل ذلك.
  5. Walk the tokenizer design checklist and give one consequence for each choice.أن تمرّ على قائمة قرارات تصميم الـ tokenizer وتعطي نتيجة واحدة لكل قرار.
  6. Disambiguate a period with rule-based, statistical and Punkt-style evidence.أن تحسم معنى النقطة باستخدام القواعد اليدوية، والأدلة الإحصائية، وأدلة على طريقة Punkt.

Type the sentence Anyhow, she's seen Jane's 224123 flowers anyhow! into the tiktokenizer web app and pick GPT-4o. You get thirteen tokens, and Jurafsky and Martin use exactly this figure in SLP3 to introduce tokenizers in practice. Three of those tokens are worth staring at before any theory: Anyhow at the start is two tokens, Any and how, while the same word at the end is one token, ·anyhow. Jane's becomes ·Jane plus 's, but she's stays whole. And 224123 becomes 224 and 123, with a lone space token in front of it.

اكتب الجملة Anyhow, she's seen Jane's 224123 flowers anyhow! في تطبيق الويب tiktokenizer واختر GPT-4o. ستحصل على ثلاثة عشر token، وهذا بالضبط الشكل الذي يستخدمه Jurafsky وMartin في SLP3 لتقديم الـ tokenizers في التطبيق العملي. ثلاثة من هذه الـ tokens تستحق التأمل قبل أي نظرية: Anyhow في البداية صارت token اثنين، Any وhow، بينما الكلمة نفسها في النهاية token واحد، ·anyhow. وكلمة Jane's صارت ·Jane زائد 's، لكن she's بقيت كاملة. والعدد 224123 صار 224 و 123، ويسبقه token مستقل لمسافة وحدها.

Tokenizerالـ TokenizerVocabularyالمفرداتTokensعدد الـ TokensPiecesالقطع
o200k (GPT-4o)o200k (GPT-4o)200K200K1313Any | how | , | ·she's | ·seen | ·Jane | 's | · | 224 | 123 | ·flowers | ·anyhow | !Any | how | , | ·she's | ·seen | ·Jane | 's | · | 224 | 123 | ·flowers | ·anyhow | !
cl100k (GPT-4)cl100k (GPT-4)~100K~100K1414Any | how | , | ·she | 's | ·seen | ·Jane | 's | · | 224 | 123 | ·flowers | ·anyhow | !Any | how | , | ·she | 's | ·seen | ·Jane | 's | · | 224 | 123 | ·flowers | ·anyhow | !
gpt2gpt250,25750,2571414Any | how | , | ·she | 's | ·seen | ·Jane | 's | ·224 | 123 | ·flowers | ·any | how | !Any | how | , | ·she | 's | ·seen | ·Jane | 's | ·224 | 123 | ·flowers | ·any | how | !
The slide sentence under three real tokenizers (measured with tiktoken; · marks a leading space)جملة الشريحة تحت ثلاثة tokenizers حقيقية (قيست بـ tiktoken؛ الرمز · يدل على مسافة سابقة)

Every one of these splits has a mechanical explanation, and none of it is in the BPE trainer from part 06. A shipping BPE tokenizer is a pipeline. Hugging Face documents it as four stages: normalization, pre-tokenization, the model (BPE, unigram or WordPiece), and post-processing that adds special tokens. The slide lists the families: most modern tokenizers are BPE-like, with unigram (Kudo 2018) and WordPiece (Schuster and Nakajima 2012) as the variants, and almost all of them sit behind a regex-based Pre-tokenizer.

لكل واحد من هذه التقطيعات تفسير آلي، ولا شيء منها موجود في مدرِّب BPE من الجزء 06. الـ BPE tokenizer المستخدم في المنتجات الحقيقية هو خط معالجة. توثّقه Hugging Face في أربع مراحل: التطبيع (normalization)، ثم pre-tokenization، ثم النموذج (BPE أو unigram أو WordPiece)، ثم معالجة لاحقة (post-processing) تضيف الرموز الخاصة. وتسرد الشريحة العائلات: معظم الـ tokenizers الحديثة شبيهة بـ BPE، مع unigram (Kudo 2018) و WordPiece (Schuster and Nakajima 2012) كنسختين بديلتين، وكلها تقريبًا تعمل خلف pre-tokenizer مبني على التعابير النمطية (regex).

The pre-tokenizer sets the fencesالـ pre-tokenizer يضع الأسوار

The pre-tokenizer is a Regular expression that cuts the text into pieces before any merge is counted. GPT-2 uses one pattern, and cl100k and o200k are tightened variants of it. Its alternatives are tried left to right at every position, which is why the order in the table below matters.

الـ pre-tokenizer هو regular expression يقطع النص إلى قطع قبل أن تُحسب أي عملية دمج. يستخدم GPT-2 نمطًا واحدًا، و cl100k و o200k نسختان مشدّدتان منه. تُجرَّب بدائله من اليسار إلى اليمين عند كل موضع، ولهذا يهمّ الترتيب في الجدول التالي.

The six alternatives of the GPT-2 pre-tokenizer regex, in orderالبدائل الستة في regex الـ pre-tokenizer الخاص بـ GPT-2، بالترتيب

's|'t|'re|'ve|'m|'ll|'d
A clitic contraction becomes its own piece, so 's is split off Janeيصبح الاختصار الملحق (clitic) قطعة مستقلة، لذلك تنفصل 's عن Jane
?\p{L}+
An optional space followed by letters: a word, with its leading space glued onمسافة اختيارية تتبعها حروف: كلمة، ومسافتها السابقة ملتصقة بها
?\p{N}+
An optional space followed by any run of digits, unbounded in GPT-2; cl100k and o200k replace it with \p{N}{1,3}, three digits and no spaceمسافة اختيارية تتبعها سلسلة أرقام بأي طول، بلا حد في GPT-2؛ ويستبدلها cl100k و o200k بالنمط \p{N}{1,3}، أي ثلاثة أرقام كحد أقصى ودون مسافة
?[^\s\p{L}\p{N}]+
An optional space followed by punctuation or symbols, so ! and , are piecesمسافة اختيارية تتبعها علامات ترقيم أو رموز، لذلك تكون ! و , قطعًا مستقلة
\s+(?!\S)
Runs of whitespace that are not followed by a non-space, which keeps one space for the next wordسلاسل المسافات البيضاء التي لا يتبعها حرف غير مسافة، وهذا يترك مسافة واحدة للكلمة التالية
\s+
Any remaining whitespace, such as newlinesأي مسافات بيضاء متبقية، مثل فواصل الأسطر

On We're 350 dogs! Um, lunch? this regex yields We, 're, ·350, ·dogs, !, ·Um, ,, ·lunch, ?, the example SLP3 prints as Figure 2.15. The consequence for BPE is the single most useful rule of this part. Pair counts during training and merges during encoding are computed inside each piece. A pair whose two halves sit in different pieces is never a candidate, so the pre-tokenizer sets an upper bound: the final tokens are always parts of its pieces, never unions of them.

على الجملة We're 350 dogs! Um, lunch? ينتج هذا الـ regex القطع We، 're، ·350، ·dogs، !، ·Um، ,، ·lunch، ?، وهو المثال الذي يطبعه SLP3 في Figure 2.15. والنتيجة بالنسبة لـ BPE هي أهم قاعدة في هذا الجزء. عدّ الأزواج أثناء التدريب وتطبيق عمليات الدمج (merges) أثناء الترميز يحدثان داخل كل قطعة على حدة. الزوج الذي يقع نصفاه في قطعتين مختلفتين لا يكون مرشحًا أبدًا، لذلك يضع الـ pre-tokenizer حدًا أعلى: الـ tokens النهائية دائمًا أجزاء من قطعه، ولا تكون أبدًا اتحادًا لقطعتين أو أكثر.

Merges join symbols inside a piece. The pair e and ' straddles a fence, so it is never counted, and 's stays a separate token.عمليات الدمج تجمع الرموز داخل القطعة الواحدة. الزوج e و ' يقع على جانبي سور، لذلك لا يُعدّ أبدًا، وتبقى 's token مستقلًا.

Now the three observations explain themselves. The leading space is part of the word piece, so ·anyhow and Anyhow are different byte strings. The lowercase, space-prefixed form is common in running text and earned a merge up to a whole token; the capitalized, sentence-initial form is rarer and stops at Any plus how. Position matters because the space marker is inside the piece. Second, the Clitic 's: under GPT-2 and cl100k the first regex alternative cuts every contraction into its own piece, so ·she and 's can never merge. o200k attaches the contraction to the word alternative, so the piece is ·she's, and since she's is a very frequent English word it has a token of its own, while ·Jane's does not and falls back to ·Jane plus 's. SLP3 states the same asymmetry: clitics are segmented off proper nouns like Jane but counted as part of frequent words like she's. Third, the digits: cl100k and o200k replace ·?\p{N}+ with \p{N}{1,3}, which caps any digit run at three per piece and, because the new alternative has no optional space, leaves the space before 224123 as a piece of its own (token id 220 in both vocabularies).

الآن تفسّر الملاحظات الثلاث نفسها بنفسها. المسافة السابقة جزء من قطعة الكلمة، لذلك ·anyhow و Anyhow سلسلتا بايتات مختلفتان. الشكل الصغير المسبوق بمسافة شائع في النص الجاري، فاستحق عمليات دمج أوصلته إلى token كامل؛ أما الشكل الذي يبدأ بحرف كبير في أول الجملة فأندر، فيتوقف عند Any زائد how. الموضع يهمّ لأن علامة المسافة (space marker) موجودة داخل القطعة. ثانيًا، الـ clitic 's: في GPT-2 و cl100k يقطع البديل الأول في الـ regex كل اختصار (contraction) إلى قطعة خاصة به، فلا يمكن أبدًا دمج ·she و 's. أما o200k فيلحق الاختصار ببديل الكلمة، فتكون القطعة ·she's، وبما أن she's كلمة إنجليزية شائعة جدًا فلها token خاص بها، بينما ·Jane's ليس لها، فترجع إلى ·Jane زائد 's. ويذكر SLP3 عدم التماثل نفسه: الـ clitics تُفصل عن أسماء العلم مثل Jane لكنها تُحسب جزءًا من الكلمات الشائعة مثل she's. ثالثًا، الأرقام: يستبدل cl100k و o200k النمط ·?\p{N}+ بالنمط \p{N}{1,3}، الذي يحدّ أي سلسلة أرقام بثلاثة في القطعة الواحدة، ولأن البديل الجديد لا يتضمن مسافة اختيارية، تبقى المسافة قبل 224123 قطعة مستقلة (رقم الـ token هو 220 في المفردتين كلتيهما).

Recall

What does a pre-tokenizer do, and why do BPE merges normally not cross its pieces?ماذا يفعل الـ pre-tokenizer، ولماذا لا تعبر عمليات الدمج في BPE قطعه في العادة؟

It runs a regex that splits text into words with their leading space, digit runs, punctuation, contractions and whitespace. Pair counts and merges are computed only inside a piece, so a pair split across two pieces is never a candidate and the final tokens are always parts of pieces.
يشغّل regex يقسم النص إلى كلمات مع مسافتها السابقة، وسلاسل أرقام، وعلامات ترقيم، واختصارات، ومسافات بيضاء. عدّ الأزواج وعمليات الدمج يحدثان داخل القطعة فقط، لذلك الزوج المقسوم على قطعتين لا يكون مرشحًا أبدًا، والـ tokens النهائية دائمًا أجزاء من القطع.

Vocabulary size is a parameter, not a resultحجم المفردات معامل يُختار، لا نتيجة تخرج من الخوارزمية

The Vocabulary of a BPE tokenizer is its base symbols plus one entry per merge, and the trainer stops after k merges. Nothing in the algorithm chooses k. The slide gives a typical range of 30k to 100k; real systems now go further, and SLP3 quotes 50,000, 100,000 or even 200,000.

المفردات (vocabulary) في BPE tokenizer هي رموزه الأساسية زائد مدخل واحد لكل عملية دمج، والمدرِّب يتوقف بعد k عملية دمج. لا شيء في الخوارزمية يختار k. تعطي الشريحة مدى نموذجيًا من 30k إلى 100k؛ والأنظمة الحقيقية اليوم تذهب أبعد من ذلك، ويذكر SLP3 50,000 أو 100,000 أو حتى 200,000.

Vocabulary sizes shipped with well-known modelsأحجام المفردات التي تأتي مع نماذج معروفة

GPT-2 (byte-level BPE)
50,25750,257
BERT base (WordPiece)
30,52230,522
Llama 3 (BPE)
128K128K
GPT-4o, o200k (byte-level BPE)
200K200K

A larger vocabulary buys shorter sequences and more whole-word tokens, at the cost of a bigger embedding table and rarer training examples per token. The number is chosen, and the next concept shows that who benefits from that budget is also chosen.

المفردات الأكبر تشتري تسلسلات أقصر و tokens أكثر تمثل كلمات كاملة، مقابل جدول embeddings أكبر وأمثلة تدريب أندر لكل token. الرقم يُختار، والمفهوم التالي يبيّن أن من يستفيد من هذه الميزانية يُختار أيضًا.

Byte-level BPE: 256 symbols and no unknown characterByte-level BPE: 256 رمزًا ولا يوجد حرف مجهول

The base symbols of the toy BPE in part 06 were characters. Radford et al. (2019) explain why GPT-2 did not do that: a base vocabulary of Unicode code points would exceed 130,000 entries before a single merge, while a byte-level version needs only 256. So GPT-2 runs merges over the UTF-8 bytes of the text. That is Byte-level tokenization, and its three advantages on the slide follow from one fact: every string in every script is a sequence of bytes from a set of 256, so there is no OOV character, any script works, and noisy text with typos or stray symbols still tokenizes. GPT-2 also adds a rule that prevents merges across character categories, with an exception for spaces, so that dog., dog! and dog? do not each become a token.

الرموز الأساسية في BPE التجريبي في الجزء 06 كانت حروفًا. ويشرح Radford et al. (2019) لماذا لم يفعل GPT-2 ذلك: مفردات أساسية من نقاط الترميز (code points) في Unicode ستتجاوز 130,000 مدخل قبل أي عملية دمج، بينما النسخة القائمة على البايتات تحتاج 256 فقط. لذلك يجري GPT-2 عمليات الدمج على بايتات UTF-8 للنص. هذا هو byte-level tokenization، ومزاياه الثلاث على الشريحة تنبع من حقيقة واحدة: كل سلسلة في كل نظام كتابة هي تسلسل من بايتات تنتمي إلى مجموعة من 256، فلا يوجد حرف OOV (خارج المفردات)، وأي نظام كتابة يعمل، والنص المليء بالأخطاء الإملائية أو الرموز الشاردة يُقطَّع رغم ذلك. ويضيف GPT-2 أيضًا قاعدة تمنع الدمج عبر فئات الحروف المختلفة، مع استثناء للمسافات، حتى لا تصبح كل من dog. و dog! و dog? token مستقلًا.

The con on the slide, that tokens may be less interpretable, is not abstract for Arabic. Arabic letters live in U+0600 to U+06FF, inside the two-byte range of UTF-8, so every letter is two bytes and a merge boundary can fall between them.

العيب المذكور على الشريحة، أن الـ tokens قد تكون أقل قابلية للتفسير، ليس أمرًا مجردًا بالنسبة للعربية. فالحروف العربية تقع في المدى من U+0600 إلى U+06FF، داخل نطاق البايتين في UTF-8، لذلك كل حرف بايتان، ويمكن أن يقع حدّ الدمج بينهما.

Worked exampleمثال محلول

One Arabic word through three byte-level tokenizersكلمة عربية واحدة عبر ثلاثة tokenizers على مستوى البايت

  1. Code pointsنقاط الترميز

    كتاب is four code points: U+0643 kaf, U+062A ta, U+0627 alif, U+0628 ba.
    كتاب أربع نقاط ترميز: U+0643 الكاف، U+062A التاء، U+0627 الألف، U+0628 الباء.
  2. UTF-8 bytesبايتات UTF-8

    Each is two bytes: D9 83, D8 AA, D8 A7, D8 A8. Eight bytes, so the base encoding is eight symbols.
    كل منها بايتان: D9 83، D8 AA، D8 A7، D8 A8. ثمانية بايتات، فالترميز الأساسي ثمانية رموز.
  3. Apply each vocabulary's mergesطبّق عمليات الدمج الخاصة بكل مفردات

    Tokenizerالـ TokenizerTokensعدد الـ TokensByte groupsمجموعات البايتاتWhat a human seesما يراه الإنسان
    gpt2gpt255D9 | 83 | D8 AA | D8 A7 D8 | A8D9 | 83 | D8 AA | D8 A7 D8 | A8Kaf split in two, fourth token straddles alif and the lead byte of baالكاف مقسومة إلى نصفين، والـ token الرابع يمتد على الألف والبايت الأول من الباء
    cl100kcl100k33D9 83 | D8 AA | D8 A7 D8 A8D9 83 | D8 AA | D8 A7 D8 A8Every token is whole lettersكل token يتكون من حروف كاملة
    o200ko200k11D9 83 D8 AA D8 A7 D8 A8D9 83 D8 AA D8 A7 D8 A8The whole word is one tokenالكلمة كلها token واحد
  4. Interpretability is repaired by merges, not by the baseقابلية التفسير تصلحها عمليات الدمج، لا الأساس

    Under gpt2 the letter kaf is D9 in one token and 83 in another, and the fourth token holds alif plus half of ba. The larger vocabularies had enough Arabic in training to merge whole letters and then the whole word. The base alphabet never changed; the merge budget did.
    في gpt2 يكون حرف الكاف D9 في token و 83 في token آخر، والـ token الرابع يحمل الألف زائد نصف الباء. أما المفردات الأكبر فقد رأت في التدريب ما يكفي من العربية لتدمج حروفًا كاملة ثم الكلمة كلها. الأبجدية الأساسية لم تتغير؛ الذي تغيّر هو ميزانية الدمج.
Four letters over eight bytes. The gpt2 token brackets ignore the letter boundaries: t1 and t2 split kaf, and t4 crosses from alif into ba.أربعة حروف على ثمانية بايتات. أقواس الـ tokens في gpt2 تتجاهل حدود الحروف: t1 و t2 تقسمان الكاف، و t4 تعبر من الألف إلى الباء.

Try both ideas yourself. The playground below runs the GPT-2 regex on any sentence, replays a small merge list inside each piece and reports which merges the fences blocked, and shows the UTF-8 bytes with the token brackets measured above.

جرّب الفكرتين بنفسك. الأداة التفاعلية أدناه تشغّل regex الخاص بـ GPT-2 على أي جملة، وتعيد تطبيق قائمة دمج صغيرة داخل كل قطعة وتخبرك بعمليات الدمج التي منعتها الأسوار، وتعرض بايتات UTF-8 مع أقواس الـ tokens التي قسناها أعلاه.

InteractiveتفاعليPre-tokenize, merge, read the bytesقسّم مبدئيًا، ثم ادمج، ثم اقرأ البايتات

GPT-2 rules: digits unbounded, with the leading space attached, contractions split into their own piece. Vocabulary about 50,257 tokens.قواعد GPT-2: الأرقام بلا حد، مع إلصاق المسافة السابقة بها، والاختصارات مثل 's تُفصل في قطعة خاصة بها. حجم المفردات نحو 50,257 رمز.

Anyhow,she'sseenJane's224123flowersanyhow!
Piecesالقطع11from regexupper bound on mergesحد أعلى لعمليات الدمج
Tokensالرموز13after mergesillustrative merge listقائمة دمج توضيحية
Tokens per wordالرموز لكل كلمة1.86fertility7 whitespace words, illustrative merge list7 كلمة مفصولة بمسافات، قائمة دمج توضيحية
Bytesالبايتات48UTF-848 characters48 محرفًا

The regex is the GPT-2 pattern with the digit and contraction rules switched per preset. Non-ASCII characters enter the merge stage as their UTF-8 bytes, exactly as in byte-level BPE. The merge list is a short illustrative one, not the real vocabulary, so token counts in the Pieces and Merges views can differ from tiktoken by a token or two. The byte brackets and the token count shown with them are real measurements.التعبير النمطي هو نمط GPT-2، مع تبديل قاعدتي الأرقام والاختصارات بحسب الإعداد المختار. تدخل المحارف غير ASCII مرحلة الدمج على شكل بايتات UTF-8، تمامًا كما في byte-level BPE. قائمة الدمج هنا قصيرة وتوضيحية وليست المفردات الحقيقية، لذلك قد يختلف عدد الرموز في عرضي القطع والدمج عن tiktoken برمز أو رمزين. أما أقواس البايتات وعدد الرموز المعروض معها فهي قياسات حقيقية.

Quick checkاختبار سريع

Why do BPE merges normally stop at the boundaries a pre-tokenizer produces?لماذا تتوقف عمليات الدمج في BPE عادةً عند الحدود التي ينتجها الـ pre-tokenizer؟

Quick checkاختبار سريع

In byte-level BPE, what can happen to a two-byte character such as Arabic kaf?في byte-level BPE، ماذا يمكن أن يحدث لحرف من بايتين مثل الكاف العربية؟

Recall

Give two pros and one con of byte-level BPE, with a concrete example of the con.اذكر ميزتين وعيبًا واحدًا لـ byte-level BPE، مع مثال ملموس على العيب.

Pros: no OOV symbols, since the base vocabulary is the 256 bytes, and it works for any script and for noisy text. Con: a token can cut a multi-byte character, for example gpt2 puts the two bytes of kaf, D9 and 83, in different tokens.
المزايا: لا توجد رموز OOV، لأن المفردات الأساسية هي البايتات الـ 256، ويعمل مع أي نظام كتابة ومع النص المليء بالضجيج. العيب: قد يقطع الـ token حرفًا متعدد البايتات، فمثلًا يضع gpt2 بايتَي الكاف، D9 و 83، في tokens مختلفة.

Take one meaning and write it twice. The book is on the table. is six words and a period, and its Arabic translation الكتاب على الطاولة. is three words and a period. Run both through cl100k and the English costs seven tokens while the Arabic costs twelve. Same meaning, half the words, nearly twice the tokens.

خذ معنى واحدًا واكتبه مرتين. The book is on the table. ست كلمات ونقطة، وترجمتها العربية الكتاب على الطاولة. ثلاث كلمات ونقطة. مرّر الاثنتين عبر cl100k فتكلّف الإنجليزية سبعة tokens بينما تكلّف العربية اثني عشر. المعنى نفسه، ونصف عدد الكلمات، وتقريبًا ضعف عدد الـ tokens.

TextالنصWordsالكلماتcl100k tokenstokens في cl100ko200k tokenstokens في o200kTokens per word (cl100k)tokens لكل كلمة (cl100k)
The book is on the table.The book is on the table.6677771.171.17
الكتاب على الطاولة.الكتاب على الطاولة.331212664.04.0
One sentence, two languages, measured with tiktokenجملة واحدة بلغتين، قيست بـ tiktoken

The o200k column shows the fix: a 200K vocabulary trained on more multilingual text spent merges on Arabic, so كتاب became one token and the Arabic sentence dropped to 6 tokens. The premium comes from how the budget was spent, not from the script.

عمود o200k يبيّن الحل: مفردات بحجم 200K دُرّبت على نص متعدد اللغات أكثر أنفقت عمليات دمج على العربية، فصارت كتاب token واحدًا ونزلت الجملة العربية إلى 6 tokens. الـ premium ينتج عن طريقة إنفاق الميزانية، لا عن نظام الكتابة نفسه.

The cause is the merge budget of the previous concept. BPE spends its k merges where the frequencies are, and English dominates the training text of most large models. English words therefore become single tokens while Arabic words fragment into letter-sized byte tokens. SLP3 puts it plainly: multilingual tokenizers tend to use most of their tokens for English, and other languages get poorer representations. The shared Vocabulary is a budget, and a shared budget favors whoever was most frequent when it was allocated. That is the slide's first two bullets in one sentence: shared vocabularies favor high-resource languages, so low-resource languages are over-segmented into longer sequences.

السبب هو ميزانية الدمج من المفهوم السابق. ينفق BPE عمليات الدمج الـ k حيث توجد الترددات العالية، والإنجليزية تهيمن على نصوص تدريب معظم النماذج الكبيرة. لذلك تصبح الكلمات الإنجليزية tokens مفردة، بينما تتفتت الكلمات العربية إلى tokens بحجم حرف أو بايت. ويقولها SLP3 بوضوح: الـ tokenizers متعددة اللغات تميل إلى استخدام معظم tokens الخاصة بها للإنجليزية، وتحصل اللغات الأخرى على تمثيلات أضعف. المفردات (vocabulary) المشتركة ميزانية، والميزانية المشتركة تحابي من كان الأكثر ترددًا وقت توزيعها. هذا هو البندان الأولان في الشريحة في جملة واحدة: المفردات المشتركة تحابي اللغات عالية الموارد، لذلك تُقطَّع اللغات قليلة الموارد تقطيعًا زائدًا (over-segmented) إلى تسلسلات أطول.

Petrov et al. (2023) turn this into a number. Take parallel sentences, tokenize both, and divide the length for language A by the length for language B. They call the ratio the tokenization premium of A relative to B.

يحوّل Petrov et al. (2023) هذا إلى رقم. خذ جملًا متوازية، وقطّع الاثنتين، واقسم الطول في اللغة A على الطول في اللغة B. يسمّون هذه النسبة tokenization premium للغة A نسبةً إلى B.

premiumA∣B=∣t(sA)∣∣t(sB)∣\text{premium}_{A \mid B} = \frac{|t(s_A)|}{|t(s_B)|}
Tokenization premium: tokens for a sentence in A over tokens for its translation in B (Petrov et al. 2023)علاوة التقسيم (tokenization premium): رموز جملة في اللغة A مقسومة على رموز ترجمتها في اللغة B (Petrov et al. 2023)
LanguageاللغةGPT-2 / RoBERTa tokenizertokenizer الخاص بـ GPT-2 / RoBERTaChatGPT / GPT-4 (cl100k)ChatGPT / GPT-4 (cl100k)ByT5 (bytes)ByT5 (بايتات)
Portugueseالبرتغالية1.941.941.481.48n/aغير متاح
Germanالألمانية2.142.141.581.58n/aغير متاح
Chinese (Simplified)الصينية (المبسطة)3.213.211.911.910.930.93
Standard Arabicالعربية الفصحى4.404.403.043.041.601.60
Burmeseالبورمية (Burmese)16.8916.8911.7011.703.513.51
Shanلغة الشان (Shan)18.7618.7615.0515.053.943.94
Premium relative to English on FLORES-200 parallel text (Petrov et al. 2023, Tables 1 and 5)الـ premium نسبةً إلى الإنجليزية على النص المتوازي FLORES-200 (Petrov et al. 2023، الجدولان 1 و5)

Arabic pays 4.40 times the English token count under the GPT-2 vocabulary and 3.04 under cl100k. Shan and Burmese pay over fifteen and eleven times. The ByT5 column shows that even byte and character models, which have no merges at all, are unequal because scripts differ in bytes per character: Petrov et al. report up to fifteen times difference for subword models and over four times between the byte encodings of Burmese or Tibetan and Chinese. Ahia et al. (2023) measured the same effect on ChatGPT across 22 languages, with some needing five times as many tokens as others, and name two causes: how much of the language was in the pretraining data, and inherent properties of the language and its script. In their analysis of the BLOOMZ tokenizer they flag Arabic as an outlier with more tokens than some other mid-resourced languages.

تدفع العربية 4.40 ضعف عدد tokens الإنجليزية تحت مفردات GPT-2، و 3.04 تحت cl100k. وتدفع لغتا Shan و Burmese أكثر من خمسة عشر وأحد عشر ضعفًا على الترتيب. ويبيّن عمود ByT5 أن حتى نماذج البايتات والحروف، التي لا تملك أي عمليات دمج (merges) على الإطلاق، غير متساوية لأن أنظمة الكتابة تختلف في عدد البايتات لكل حرف: يذكر Petrov et al. فرقًا يصل إلى خمسة عشر ضعفًا في نماذج الـ subword، وأكثر من أربعة أضعاف بين ترميزات البايت للغة Burmese أو Tibetan واللغة الصينية. وقاس Ahia et al. (2023) الأثر نفسه على ChatGPT عبر 22 لغة، فبعضها احتاج خمسة أضعاف الـ tokens التي احتاجتها لغات أخرى، وسمّوا سببين: كم كان حجم اللغة في بيانات التدريب المسبق، والخصائص الذاتية للغة ونظام كتابتها. وفي تحليلهم لـ tokenizer الخاص بـ BLOOMZ أشاروا إلى العربية كحالة شاذة تحتاج tokens أكثر من بعض اللغات الأخرى متوسطة الموارد.

Three bills for one over-segmented sentenceثلاث فواتير لجملة واحدة مقطّعة تقطيعًا زائدًا

  1. Compute. Every layer processes every token, so three times the tokens is at least three times the work, and self-attention, whose cost grows with the square of sequence length, makes long inputs worse still.الحساب. كل طبقة تعالج كل token، فثلاثة أضعاف الـ tokens تعني ثلاثة أضعاف العمل على الأقل، والـ self-attention، الذي تنمو تكلفته مع مربع طول التسلسل، يجعل المدخلات الطويلة أسوأ من ذلك.
  2. Context. A window of fixed token length holds a third as many Arabic words as English words. Ahia et al. note that fewer in-context examples fit, which directly lowers few-shot quality.السياق. النافذة ذات الطول الثابت بالـ tokens تتسع لثلث عدد الكلمات العربية مقارنة بالكلمات الإنجليزية. ويلاحظ Ahia et al. أن عدد الأمثلة التي تتسع لها النافذة (in-context examples) يقلّ، وهذا يخفض جودة الـ few-shot مباشرة.
  3. Money and quality. APIs bill per token, so Ahia et al. conclude that speakers of many languages are overcharged while obtaining poorer results, and SLP3 adds that fragmented tokens give poorer representations of meaning.المال والجودة. واجهات الـ API تحاسب لكل token، لذلك يخلص Ahia et al. إلى أن متحدثي لغات كثيرة يدفعون أكثر بينما يحصلون على نتائج أضعف، ويضيف SLP3 أن الـ tokens المفتتة تعطي تمثيلات أضعف للمعنى.

Premium under ArabicBERT, relative to Arabic (Petrov et al. 2023, Section 4.2)الـ premium تحت ArabicBERT نسبةً إلى العربية (Petrov et al. 2023، القسم 4.2)

Kanuri (Arabic script)الكانورية (بالحرف العربي)
1.271.27
Acehnese (Arabic script)الأتشيهية (بالحرف العربي)
1.731.73
Englishالإنجليزية
1.821.82

The slide's third bullet, that tokenizer design affects fairness and performance, is an engineering decision you can make differently.

البند الثالث في الشريحة، أن تصميم الـ tokenizer يؤثر في العدالة والأداء، هو قرار هندسي يمكنك أن تتخذه بشكل مختلف.

Quick checkاختبار سريع

Under an English-heavy vocabulary, why does Arabic text become more tokens than its English translation?تحت مفردات تهيمن عليها الإنجليزية، لماذا يصبح النص العربي tokens أكثر من ترجمته الإنجليزية؟

Recall

Why does a low-resource language get longer token sequences under a shared vocabulary, and what are two consequences?لماذا تحصل اللغة قليلة الموارد على تسلسلات tokens أطول تحت مفردات مشتركة، وما نتيجتان لذلك؟

The merge budget was spent on the frequent, high-resource language, so words of the low-resource language fragment into small pieces (Arabic premium 3.04 under cl100k). Consequences: more compute and less usable context per word, and higher API cost with lower quality.
ميزانية الدمج أُنفقت على اللغة الشائعة عالية الموارد، لذلك تتفتت كلمات اللغة قليلة الموارد إلى قطع صغيرة (الـ premium للعربية 3.04 تحت cl100k). النتائج: حساب أكثر وسياق قابل للاستخدام أقل لكل كلمة، وتكلفة API أعلى مع جودة أقل.

SuperBPE: letting merges cross the spaceSuperBPE: السماح لعمليات الدمج بعبور المسافة

If fences limit how much a vocabulary can compress, one way to compress more is to remove a fence late in training. Liu et al. (2025) call this SuperBPE: a pretokenization curriculum for BPE that first learns subwords under the ordinary whitespace fences, then continues with merges that are allowed to bridge whitespace and produce superwords. The figure on the slide is their example. Ordinary BPE gives By | the | way | , | I | am | a | fan | of | the | Milky | Way | ., thirteen tokens. SuperBPE gives By the way | , I am | a | fan | of the | Milky Way | ., seven.

إذا كانت الأسوار تحدّ من مقدار ما تستطيع المفردات ضغطه، فإحدى طرق الضغط أكثر هي إزالة سور في مرحلة متأخرة من التدريب. يسمّي Liu et al. (2025) هذا SuperBPE: منهج pretokenization متدرّج لـ BPE يتعلّم أولًا الـ subwords تحت أسوار المسافات المعتادة، ثم يواصل بعمليات دمج مسموح لها بعبور المسافات فتنتج كلمات فائقة (superwords). الشكل في الشريحة هو مثالهم. يعطي BPE العادي By | the | way | , | I | am | a | fan | of | the | Milky | Way | .، أي ثلاثة عشر token. ويعطي SuperBPE By the way | , I am | a | fan | of the | Milky Way | .، أي سبعة.

The upper row fuses into the lower one: frequent multiword units such as By the way and Milky Way become single tokens.الصف العلوي يندمج في الصف السفلي: الوحدات الشائعة المكوّنة من عدة كلمات مثل By the way و Milky Way تصبح tokens مفردة.

The gain is the slide's stated goal, efficiency. At a 200K vocabulary SuperBPE beats ordinary BPE on every measure the authors report, so the extra training stage pays for itself.

المكسب هو الهدف المعلن في الشريحة: الكفاءة. عند مفردات بحجم 200K يتفوق SuperBPE على BPE العادي في كل مقياس ذكره المؤلفون، فمرحلة التدريب الإضافية تدفع ثمنها بنفسها.

SuperBPE at 200K vs BPE (Liu et al. 2025)SuperBPE عند 200K مقارنة بـ BPE (Liu et al. 2025)

Token reduction vs BPEانخفاض عدد الـ tokens مقارنة بـ BPE
33%33%
Average over 30 downstream tasksالمتوسط على 30 مهمة لاحقة
+4.0 points+4.0 نقطة
MMLU
+8.2 points+8.2 نقطة
Inference computeحساب الاستدلال (inference)
-27%-27%

SLP3 mentions BoundlessBPE (Schmidt et al. 2025) as a sibling method with the same idea.

يذكر SLP3 طريقة BoundlessBPE (Schmidt et al. 2025) كطريقة شقيقة تقوم على الفكرة نفسها.

The tradeoff is the slide's third bullet, Compositionality. Under ordinary BPE, Way in Milky Way and way in By the way share visible structure with every other use of the word. Once Milky Way is one token, the model must learn its relation to way from scratch, and a rare phrase that almost matches a superword is split in an unfamiliar way. Fewer tokens means less shared structure across tokens.

المقايضة هي البند الثالث في الشريحة: التركيبية (compositionality). تحت BPE العادي، تشترك Way في Milky Way و way في By the way في بنية ظاهرة مع كل استخدام آخر للكلمة. وبمجرد أن تصبح Milky Way token واحدًا، يجب على النموذج أن يتعلّم علاقتها بـ way من الصفر، والعبارة النادرة التي تكاد تطابق كلمة فائقة تُقطَّع بطريقة غير مألوفة. tokens أقل تعني بنية مشتركة أقل بين الـ tokens.

Every surprise in this part came from a switch someone set before training: whether the space belongs to the next word, whether digits are capped, whether contractions split, how many merges to make, which languages got the budget. The slide collects those switches into a checklist. Treat each line as a question with a consequence, because that is how an exam or a design review will ask it.

كل مفاجأة في هذا الجزء جاءت من مفتاح ضبطه شخص ما قبل التدريب: هل المسافة تنتمي إلى الكلمة التالية، وهل الأرقام محدودة، وهل تُفصل الاختصارات، وكم عملية دمج تُجرى، وأي اللغات حصلت على الميزانية. تجمع الشريحة هذه المفاتيح في قائمة مراجعة. تعامل مع كل سطر كسؤال له نتيجة، لأن هذه هي الطريقة التي سيُطرح بها في الامتحان أو في مراجعة تصميم.

The tokenizer design checklist, each choice with its consequenceقائمة مراجعة تصميم الـ tokenizer، كل قرار مع نتيجته

Pre-tokenizationالـ pre-tokenization
Split on whitespace, and what happens to the space? Drop it (BERT), glue it onto the next piece (·world in GPT-2) or keep it as an explicit symbol (▁ in SentencePiece). The answer decides whether ·anyhow and anyhow can ever share a token, fixes the fences that merges cannot cross, and decides whether detokenization can restore the original text exactly. SuperBPE removes the fence in its second stage.هل نقسم على المسافات، وماذا يحدث للمسافة؟ نرميها (BERT)، أو نلصقها بالقطعة التالية (·world في GPT-2)، أو نحتفظ بها كرمز صريح (▁ في SentencePiece). الجواب يحدد هل يمكن أن تشترك ·anyhow و anyhow في token واحد أصلًا، ويثبّت الأسوار التي لا تستطيع عمليات الدمج عبورها، ويحدد هل يستطيع الـ detokenization استعادة النص الأصلي بدقة. ويزيل SuperBPE هذا السور في مرحلته الثانية.
Normalizationالتطبيع (normalization)
NFC or NFD, lowercasing, accent stripping? BERT uncased runs NFD, lowercase and strip-accents, so Héllò becomes hello and case and diacritics are gone for good. GPT-style tokenizers keep them, at the cost of separate entries for Apple and apple.NFC أم NFD، وهل نحوّل إلى أحرف صغيرة، وهل نزيل علامات التشكيل؟ نسخة BERT uncased تطبّق NFD ثم التحويل إلى أحرف صغيرة ثم إزالة علامات التشكيل، فتصبح Héllò هي hello وتضيع حالة الأحرف وعلامات التشكيل نهائيًا. أما الـ tokenizers على طريقة GPT فتحتفظ بها، مقابل مدخلين منفصلين لـ Apple و apple.
Special tokensالرموز الخاصة (special tokens)
<BOS>, <EOS>, <UNK>, <PAD>. A byte-level tokenizer never needs <UNK>. GPT-2 has one special token, <|endoftext|> at id 50256; BERT adds [CLS] and [SEP] in post-processing.<BOS>، <EOS>، <UNK>، <PAD>. الـ tokenizer الذي يعمل على مستوى البايت (byte-level) لا يحتاج أبدًا إلى <UNK>. لدى GPT-2 رمز خاص واحد، <|endoftext|> برقم 50256؛ ويضيف BERT الرمزين [CLS] و [SEP] في المعالجة اللاحقة.
Numbersالأرقام
Cap digit runs (\p{N}{1,3}) or split every digit? Decides whether 224123 is two tokens or six, and shapes how well the model does arithmetic on numbers it has never seen whole.هل نحدّ سلاسل الأرقام (\p{N}{1,3}) أم نفصل كل رقم على حدة؟ هذا يحدد هل يكون 224123 token اثنين أم ستة، ويؤثر في جودة الحساب الذي يجريه النموذج على أعداد لم يرها كاملة من قبل.
URLs and emojiالروابط والـ emoji
Byte fallback keeps them representable: gpt2 encodes 😀 (F0 9F 98 80) as F0 9F 98 plus 80. A URL becomes a long run of punctuation and word pieces unless a rule keeps it whole.الرجوع إلى البايتات (byte fallback) يُبقيها قابلة للتمثيل: يرمّز gpt2 الرمز 😀 (F0 9F 98 80) على شكل F0 9F 98 زائد 80. والرابط (URL) يصبح سلسلة طويلة من علامات الترقيم وأجزاء الكلمات ما لم توجد قاعدة تُبقيه كاملًا.
Multilingual coverage and biasالتغطية متعددة اللغات والانحياز
How much of the vocabulary budget each language gets. An English-heavy budget gives Arabic a premium of 3.04 under cl100k and Shan 15.05, which is the fairness cost of the previous concept.كم تحصل كل لغة من ميزانية المفردات. الميزانية التي تهيمن عليها الإنجليزية تعطي العربية premium قدره 3.04 تحت cl100k، ولغة Shan 15.05، وهذه هي تكلفة العدالة في المفهوم السابق.

Three answers to the spaces questionثلاث إجابات عن سؤال المسافات

The slide's pre-tokenization line asks one question with three real answers. Once the text is split on whitespace, what happens to the space? The choice fixes the Space marker (end-of-word marker) convention the model will live with, and it decides whether detokenization can recover the original string.

سطر الـ pre-tokenization في الشريحة يطرح سؤالًا واحدًا له ثلاث إجابات حقيقية. بعد تقسيم النص على المسافات، ماذا يحدث للمسافة؟ هذا القرار يثبّت اصطلاح علامة المسافة (space marker) الذي سيعيش معه النموذج، ويحدد ما إذا كان الـ detokenization قادرًا على استعادة السلسلة الأصلية.

Tokenizerالـ TokenizerPiecesالقطعWhere the space goesأين تذهب المسافةDetokenizationالـ Detokenization
BERTBERTHello | worldHello | worldSpace discardedتُرمى المسافةNot reversible: detokenization cannot say where the spaces wereغير قابل للعكس: لا يستطيع الـ detokenization أن يعرف أين كانت المسافات
GPT-2GPT-2Hello | ·worldHello | ·worldSpace glued onto the next pieceتلتصق المسافة بالقطعة التاليةReversible: every piece carries its leading spaceقابل للعكس: كل قطعة تحمل مسافتها السابقة
SentencePieceSentencePiece▁Hello | ▁world▁Hello | ▁worldSpace kept as an explicit ▁ symbolتُحفظ المسافة كرمز صريح ▁Losslessly reversible: replace ▁ by a space and the original text is restoredقابل للعكس دون أي فقد: استبدل ▁ بمسافة فيعود النص الأصلي كما كان
Three whitespace policies on Hello world (· is a glued space, ▁ is U+2581)ثلاث سياسات للمسافات على Hello world (الرمز · مسافة ملتصقة، و ▁ هو U+2581)

BERT throws the space away, which keeps the alphabet small but makes the token stream lossy: don't and the three tokens don, ', t cannot be told apart from a hyphenless spelling once the spaces are gone. GPT-2 glues the space onto the following piece, so ·world and world are different entries and the original spacing is recoverable. SentencePiece keeps the space as an explicit ▁ symbol, which is exactly reversible: swapping ▁ back to a space restores the input byte for byte. It also means the regex fences from the first concept apply to a text where spaces are ordinary symbols, so SentencePiece can in principle learn a merge that crosses what used to be a word boundary.

يرمي BERT المسافة، وهذا يبقي الأبجدية صغيرة لكنه يجعل تدفق الـ tokens فاقدًا للمعلومات: don't والـ tokens الثلاثة don و ' و t لا يمكن التمييز بينها وبين كتابة بلا شرطة بمجرد أن تختفي المسافات. أما GPT-2 فيلصق المسافة بالقطعة التالية، فتكون ·world و world مدخلين مختلفين ويمكن استعادة المسافات الأصلية. ويحتفظ SentencePiece بالمسافة كرمز صريح ▁، وهذا قابل للعكس تمامًا: استبدال ▁ بمسافة يعيد المدخل بايتًا ببايت. ويعني ذلك أيضًا أن أسوار الـ regex من المفهوم الأول تنطبق على نص تكون فيه المسافات رموزًا عادية، لذلك يمكن لـ SentencePiece من حيث المبدأ أن يتعلّم عملية دمج تعبر ما كان في السابق حدًا بين كلمتين.

Quick checkاختبار سريع

Which checklist choice makes Héllò and hello identical?أي قرار في القائمة يجعل Héllò و hello متطابقتين؟

The rule that ties the list together is that all of it is frozen with the model. The Unicode normalization form, the case folding decision, the Special tokens and the Pre-tokenizer regex are recorded in the tokenizer file, and Hugging Face's pipeline documentation warns that changing the normalizer or the pre-tokenizer requires retraining the tokenizer, which in turn means the embeddings of the language model no longer match. You cannot fix an English-centric tokenizer after the fact; you choose it before the first gradient step.

القاعدة التي تربط القائمة كلها هي أن كل ما فيها يُجمَّد مع النموذج. شكل التطبيع في Unicode (Unicode normalization)، وقرار توحيد حالة الأحرف (case folding)، و الرموز الخاصة (special tokens)، و regex الـ pre-tokenizer، كلها مسجلة في ملف الـ tokenizer، وتحذّر وثائق خط المعالجة في Hugging Face من أن تغيير الـ normalizer أو الـ pre-tokenizer يتطلب إعادة تدريب الـ tokenizer، وهذا بدوره يعني أن الـ embeddings في النموذج اللغوي لم تعد متطابقة معه. لا يمكنك إصلاح tokenizer متمحور حول الإنجليزية بعد الحدث؛ أنت تختاره قبل أول خطوة gradient.

Recall

Name four items of the tokenizer design checklist and one consequence for any one of them.سمِّ أربعة بنود من قائمة تصميم الـ tokenizer ونتيجة واحدة لأي واحد منها.

Pre-tokenization, normalization, special tokens, handling of numbers, URLs and emoji, and multilingual coverage. For example, normalization with lowercasing and accent stripping makes Héllò and hello identical, which shrinks the vocabulary but loses case and diacritics permanently.
الـ pre-tokenization، والتطبيع، والرموز الخاصة، ومعالجة الأرقام والروابط والـ emoji، والتغطية متعددة اللغات. مثال: التطبيع مع تحويل الأحرف إلى صغيرة وإزالة علامات التشكيل يجعل Héllò و hello متطابقتين، وهذا يقلّص المفردات لكنه يُضيع حالة الأحرف وعلامات التشكيل نهائيًا.

Here is a passage with six periods, one question mark and one exclamation mark: Dr. Ahmad arrived at 5 p.m. on Monday. He paid 3.50 riyals. Was it enough? Yes! It contains four sentences. A splitter that cuts at every period would produce seven fragments and would glue the last two sentences together. Decide for yourself which marks end a sentence before reading the rules.

إليك مقطعًا فيه ست نقاط، وعلامة استفهام واحدة، وعلامة تعجب واحدة: Dr. Ahmad arrived at 5 p.m. on Monday. He paid 3.50 riyals. Was it enough? Yes! يحتوي على أربع جمل. الأداة التي تقطع عند كل نقطة ستنتج سبع شظايا، وستلصق الجملتين الأخيرتين معًا. قرّر بنفسك أي العلامات تنهي جملة قبل أن تقرأ القواعد.

ExerciseتمرينSentence segmentation: decide what each mark doesتقسيم الجمل: حدد وظيفة كل علامة

Click a punctuation mark to cycle it through sentence boundary, not a boundary, and undecided. Then check. Each verdict names the rule and the feature that decided it.انقر على علامة ترقيم لتتنقل بين ثلاث حالات: حد جملة، وليست حدًا، ولم يُحسم. ثم تحقّق. يذكر كل حكم القاعدة والسمة التي حسمته.

Dr Ahmad arrived at 5 pm on Monday He paid 350 riyals Was it enough Yes

0 of 8 marks decidedحُسمت 0 من 8 علامات

A double bar marks a sentence boundary, a middle dot marks a period that belongs to its word or number. After checking, an accent ring means correct and a teal ring with strikethrough means wrong or undecided.يدل الشريط المزدوج على حد جملة، وتدل النقطة الوسطى على نقطة تنتمي إلى كلمتها أو عددها. بعد التحقق، تعني الحلقة باللون المميز إجابة صحيحة، وتعني الحلقة الخضراء المزرقة مع الشطب إجابة خاطئة أو غير محسومة.

Sentence segmentation is the task of finding sentence boundaries, and the slide names its usual customers: parsing and machine translation, both of which take one sentence at a time. SLP3 states the difficulty exactly. Question marks and exclamation points are relatively unambiguous markers of sentence boundaries. The period is ambiguous between a sentence boundary and an abbreviation marker such as Dr. or Inc., and it can even do both at once: when a sentence ends in Inc., one period marks the abbreviation and the boundary. Kiss and Strunk (2006) list the period's other jobs too: initials, ordinal numbers and ellipses, and in their corpora abbreviations account for up to 30% of the candidate boundaries.

تقسيم النص إلى جمل (sentence segmentation) هو مهمة إيجاد حدود الجمل، وتسمّي الشريحة المستفيدين المعتادين منه: التحليل النحوي (parsing) والترجمة الآلية، وكلاهما يأخذ جملة واحدة في كل مرة. ويحدد SLP3 الصعوبة بدقة. علامات الاستفهام والتعجب مؤشرات غير غامضة نسبيًا على حدود الجمل. أما النقطة فغامضة بين أن تكون حدًا لجملة وأن تكون علامة اختصار مثل Dr. أو Inc.، بل يمكنها أن تؤدي الدورين معًا: حين تنتهي جملة بـ Inc.، تعلّم نقطة واحدة الاختصار والحد معًا. ويسرد Kiss and Strunk (2006) وظائف النقطة الأخرى أيضًا: الأحرف الأولى من الأسماء، والأعداد الترتيبية، وعلامات الحذف (ellipses)، وفي مدوّناتهم تشكّل الاختصارات ما يصل إلى 30% من الحدود المرشحة.

One glyph, three readings. The boundary reading is the one a capitalized next token supports; the lexicon and the digit test claim the others.رمز واحد وثلاث قراءات. قراءة الحد هي التي يدعمها token تالٍ يبدأ بحرف كبير؛ أما القراءتان الأخريان فيطالب بهما قاموس الاختصارات واختبار الأرقام.

Rules, lexicons and the joint solutionالقواعد، وقواميس الاختصارات، والحل المشترك

The slide's rule-based heuristics combine punctuation with capitalization patterns, an abbreviation list and quote or bracket balancing. Each rule is a feature that votes on one candidate mark.

القواعد اليدوية في الشريحة تجمع علامات الترقيم مع أنماط الأحرف الكبيرة، وقائمة اختصارات، وموازنة علامات الاقتباس والأقواس. كل قاعدة سمة (feature) تصوّت على علامة مرشحة واحدة.

Rule-based features and what they decideسمات النهج القائم على القواعد وما تحسمه

Punctuation plus capitalizationعلامات الترقيم مع الأحرف الكبيرة
A period followed by whitespace and a capitalized token suggests a boundary; a lowercase next token argues against oneالنقطة التي تتبعها مسافة ثم token يبدأ بحرف كبير توحي بوجود حد؛ أما الـ token التالي الذي يبدأ بحرف صغير فيرجّح عدم وجوده
Abbreviation listقائمة الاختصارات
If the token before the period is Dr, Inc, p.m. or another listed abbreviation, the period belongs to the wordإذا كان الـ token قبل النقطة Dr أو Inc أو p.m. أو اختصارًا آخر في القائمة، فالنقطة تنتمي إلى الكلمة
Quote and bracket balancingموازنة علامات الاقتباس والأقواس
A closing quote or bracket after the mark still belongs to the sentence that is endingعلامة الاقتباس أو القوس الذي يغلق بعد العلامة ما زال ينتمي إلى الجملة التي تنتهي
Digit on both sidesأرقام على الجانبين
3.50 and 3.14 are numbers, so a word tokenizer keeps them whole and the segmenter never sees the period3.50 و 3.14 أعداد، لذلك يُبقيها tokenizer الكلمات كاملة ولا يرى مقسّم الجمل النقطة أبدًا

Notice the conflict in the exercise. After Dr. comes Ahmad, capitalized, so the capitalization rule votes for a boundary and only the abbreviation lexicon overrules it. After p.m. comes on, lowercase, so both rules agree. This is why the slide says segmentation is often done jointly with Tokenization and abbreviation lexicons: if the tokenizer has already kept Dr., p.m. and 3.50 as single tokens, the segmenter never has to look at those periods. Stanford CoreNLP states its rule in exactly that form: a sentence ends when a sentence-ending punctuation mark is not already grouped with other characters into a token, optionally followed by closing quotes or brackets. SLP3 notes the abbreviation dictionary can be hand-built or machine-learned.

لاحظ التعارض في التمرين. بعد Dr. تأتي Ahmad بحرف كبير، فتصوّت قاعدة الأحرف الكبيرة لصالح وجود حد، ولا يلغيها إلا قاموس الاختصارات. وبعد p.m. تأتي on بحرف صغير، فتتفق القاعدتان. لهذا تقول الشريحة إن التقسيم إلى جمل يُجرى غالبًا بالاشتراك مع tokenization وقواميس الاختصارات: إذا كان الـ tokenizer قد أبقى Dr. و p.m. و 3.50 كـ tokens مفردة، فلن يحتاج مقسّم الجمل إلى النظر في تلك النقاط أبدًا. ويصوغ Stanford CoreNLP قاعدته بهذا الشكل بالضبط: تنتهي الجملة حين تكون علامة إنهاء الجملة غير مجمّعة مسبقًا مع حروف أخرى في token واحد، وقد تتبعها اختياريًا علامات اقتباس أو أقواس إغلاق. ويلاحظ SLP3 أن قاموس الاختصارات يمكن بناؤه يدويًا أو تعلّمه آليًا.

Learning the boundaryتعلّم الحد

The statistical approach on the slide replaces hand-tuned votes with a classifier that learns the probability of a boundary from annotated data. Its features are the same evidence the rules used: the token before the mark, whether it is in an abbreviation list, its length, whether the next token is capitalized, whether the next token is a frequent sentence starter, and, in neural models, the surrounding context as a whole. Kiss and Strunk (2006) went one step further and removed the annotation. Their Punkt system, the default sentence tokenizer in NLTK, is unsupervised and language independent. It treats an abbreviation as a very tight collocation of a truncated word and a final period, which can be detected from raw text by asking whether the word occurs with a period far more often than chance, helped by the facts that abbreviations are usually short and sometimes contain internal periods. It also learns frequent sentence starters and collocations across a period. Its measured results on newspaper text are in the table.

النهج الإحصائي في الشريحة يستبدل الأصوات المضبوطة يدويًا بمصنِّف (classifier) يتعلّم احتمال وجود حد من بيانات موسومة. سماته هي الأدلة نفسها التي استخدمتها القواعد: الـ token الذي يسبق العلامة، وهل هو في قائمة الاختصارات، وطوله، وهل الـ token التالي يبدأ بحرف كبير، وهل هو من الكلمات الشائعة في بدايات الجمل، وفي النماذج العصبية السياق المحيط ككل. وذهب Kiss and Strunk (2006) خطوة أبعد فأزالا الحاجة إلى الوسم. نظامهما Punkt، وهو مقسّم الجمل الافتراضي في NLTK، غير موجَّه (unsupervised) ومستقل عن اللغة. يعامل الاختصار كتلازم (collocation) شديد الإحكام بين كلمة مقتطعة ونقطة نهائية، ويمكن اكتشافه من النص الخام بالسؤال عمّا إذا كانت الكلمة تظهر مع نقطة أكثر بكثير مما تتوقعه الصدفة، ويساعده في ذلك أن الاختصارات عادةً قصيرة وأحيانًا تحتوي على نقاط داخلية. ويتعلّم أيضًا الكلمات الشائعة في بدايات الجمل والتلازمات التي تعبر النقطة. ونتائجه المقيسة على نصوص الصحف موجودة في الجدول.

Punkt boundary detection results (Kiss and Strunk 2006)نتائج Punkt في اكتشاف حدود الجمل (Kiss and Strunk 2006)

Mean accuracy, 11 languagesمتوسط الدقة على 11 لغة
98.74%98.74%
Boundary error, Englishخطأ تحديد الحدود، الإنجليزية
1.65%1.65%
Boundary error, Germanخطأ تحديد الحدود، الألمانية
0.35%0.35%
ApproachالنهجHow it decidesكيف يقررNeedsما يحتاجهStrengths and limitsنقاط القوة والحدود
Rule-basedقائم على القواعدHand-written heuristics and a hand-built abbreviation listقواعد مكتوبة يدويًا وقائمة اختصارات مبنية يدويًاNoneلا شيءFast and transparent; brittle on new domains and languagesسريع وشفاف؛ هشّ أمام المجالات واللغات الجديدة
Statistical (supervised)إحصائي (موجَّه)P(boundary | context)P(boundary | context)Annotated sentence boundariesحدود جمل موسومة يدويًاAdapts to a domain; needs labeled data per languageيتكيّف مع المجال؛ يحتاج بيانات موسومة لكل لغة
Punkt (unsupervised)Punkt (غير موجَّه)Abbreviations as tight collocations of a truncated word and a period, plus sentence startersالاختصارات كتلازمات محكمة بين كلمة مقتطعة ونقطة، إضافة إلى الكلمات التي تبدأ بها الجملRaw text onlyنص خام فقطLanguage independent; 98.74% mean accuracy over eleven languagesمستقل عن اللغة؛ متوسط دقة 98.74% على إحدى عشرة لغة
Three ways to decide what a period doesثلاث طرق لتحديد وظيفة النقطة

Quick checkاختبار سريع

Which punctuation mark is hardest for sentence segmentation, and why?أي علامة ترقيم هي الأصعب في تقسيم النص إلى جمل، ولماذا؟

Recall

Why is the period harder than the question mark for sentence segmentation, and what are two features a classifier would use?لماذا تكون النقطة أصعب من علامة الاستفهام في تقسيم النص إلى جمل، وما سمتان يستخدمهما المصنِّف؟

The period also marks abbreviations, initials, ordinals and decimals, while a question mark almost always ends a sentence. Features: whether the token before the period is in an abbreviation list, and whether the next token is capitalized or a frequent sentence starter.
النقطة تعلّم أيضًا الاختصارات، والأحرف الأولى من الأسماء، والأعداد الترتيبية، والأعداد العشرية، بينما علامة الاستفهام تنهي الجملة في الغالب دائمًا. السمات: هل الـ token الذي يسبق النقطة موجود في قائمة الاختصارات، وهل الـ token التالي يبدأ بحرف كبير أو هو من الكلمات الشائعة في بدايات الجمل.

Recall

What does Punkt learn without any labeled data?ماذا يتعلّم Punkt دون أي بيانات موسومة؟

Which words are abbreviations, detected as tight collocations of a truncated word with a following period, plus collocations that span a period and words that frequently start sentences.
أي الكلمات اختصارات، ويكتشفها كتلازمات محكمة بين كلمة مقتطعة ونقطة تليها، إضافة إلى التلازمات التي تمتد عبر النقطة، والكلمات التي تبدأ بها الجمل كثيرًا.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • A production tokenizer is a pipeline: normalization, regex pre-tokenization, a BPE, unigram or WordPiece model, then special-token post-processing.الـ tokenizer المستخدم فعليًا خط معالجة (pipeline): تطبيع (normalization)، ثم pre-tokenization بتعبير نمطي (regex)، ثم نموذج BPE أو unigram أو WordPiece، ثم معالجة لاحقة تضيف الرموز الخاصة (special tokens).
  • Merges live inside pre-tokenizer pieces. The leading space belongs to the piece, so Any|how at the start and ·anyhow later are different byte strings.عمليات الدمج تحدث داخل قطع الـ pre-tokenizer فقط. والمسافة السابقة للكلمة جزء من القطعة، لذلك Any|how في بداية الجملة و ·anyhow لاحقًا سلسلتا بايتات مختلفتان.
  • cl100k and o200k cap digit runs at three per piece, and vocabulary size is a design parameter: 50,257 for GPT-2, 30,522 for BERT, 128K for Llama 3, 200K for GPT-4o.يحدّ cl100k و o200k سلاسل الأرقام بثلاثة أرقام في القطعة الواحدة، وحجم المفردات معامل تصميمي: 50,257 في GPT-2، و30,522 في BERT، و128K في Llama 3، و200K في GPT-4o.
  • Byte-level BPE starts from 256 symbols, so nothing is out of vocabulary, but a token may cut a multi-byte character: gpt2 splits kaf into D9 and 83.يبدأ byte-level BPE من 256 رمزًا، فلا يوجد شيء خارج المفردات، لكن الـ token قد يقطع حرفًا متعدد البايتات: gpt2 يقسم الكاف إلى D9 و 83.
  • Shared vocabularies favor English. Under cl100k Arabic pays a premium of 3.04 and Shan 15.05 (Petrov et al. 2023); longer sequences cost compute, context and money.المفردات المشتركة تحابي الإنجليزية. تحت cl100k تدفع العربية premium قدره 3.04 ولغة Shan قدره 15.05 (Petrov et al. 2023)؛ والتسلسلات الأطول تكلّف حسابًا وسياقًا ومالًا.
  • SuperBPE adds a second stage whose merges cross spaces: up to 33% fewer tokens at 200K, at the price of compositionality.يضيف SuperBPE مرحلة ثانية تعبر فيها عمليات الدمج المسافات: ما يصل إلى 33% tokens أقل عند حجم 200K، مقابل خسارة في التركيبية (compositionality).
  • Every checklist switch is frozen with the model: pre-tokenization, normalization, special tokens, numbers and URLs, multilingual coverage.كل مفتاح في قائمة التصميم يُجمَّد مع النموذج: الـ pre-tokenization، والتطبيع، والرموز الخاصة، والأرقام والروابط، والتغطية متعددة اللغات.
  • The period is ambiguous and the question mark and exclamation mark mostly are not. Abbreviation lexicons, capitalization, decimal checks or Punkt's collocation test decide.النقطة غامضة، أما علامة الاستفهام وعلامة التعجب فغالبًا ليستا كذلك. يحسم الأمر قاموس الاختصارات، أو حالة الأحرف الكبيرة، أو فحص الأعداد العشرية، أو اختبار التلازم (collocation) في Punkt.

Sourcesالمصادر