Ouaill commited on
Commit
fe0c8aa
Β·
verified Β·
1 Parent(s): e2bd1ff

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +192 -0
README.md ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Darija Subword Tokenizer Benchmark
3
+ license: mit
4
+ tags:
5
+ - tokenizer
6
+ - moroccan-darija
7
+ - arabic
8
+ - bpe
9
+ - unigram
10
+ - wordpiece
11
+ - bbpe
12
+ - benchmark
13
+ language:
14
+ - ar
15
+ - lat
16
+ size_categories:
17
+ - n>100K
18
+ ---
19
+
20
+ # Darija Subword Tokenizer Benchmark (`OiQ/daa-tokenizers`)
21
+
22
+ Comprehensive benchmark of 24 subword tokenizers for **Moroccan Darija** (Arabic and Arabizi/Latin scripts), trained on the [`OiQ/daa-pairs`](https://huggingface.co/datasets/OiQ/daa-pairs) dataset (112,814 sentence pairs).
23
+
24
+ ## Tokenizers
25
+
26
+ | Type | Algorithms | Vocab Sizes | Count |
27
+ |------|-----------|-------------|-------|
28
+ | **Shared** (single vocab for Arabic + Arabizi) | BPE, Unigram, WordPiece, BBPE | 8K, 16K, 32K | 12 |
29
+ | **Concatenated** (separate Arabic/Arabizi vocabs, shifted IDs) | BPE, Unigram, WordPiece, BBPE | 8K, 16K, 32K | 12 |
30
+
31
+ **Total: 24 tokenizers** (plus 28 exported in `transformers`-compatible format β€” 4 concat 32K tokenizers were not exported due to missing raw tokenizer files from an OOM-killed training run).
32
+
33
+ ## Quick Start
34
+
35
+ ```python
36
+ from transformers import AutoTokenizer
37
+
38
+ # Load a shared tokenizer
39
+ tok = AutoTokenizer.from_pretrained("OiQ/daa-tokenizers", subfolder="transformers_tokenizers/shared_bpe_16000")
40
+
41
+ # Load a concatenated tokenizer (Arabic)
42
+ tok_ar = AutoTokenizer.from_pretrained("OiQ/daa-tokenizers", subfolder="transformers_tokenizers/concat_bpe_16000_tokenizer_ar")
43
+
44
+ # Load a concatenated tokenizer (Arabizi/Latin)
45
+ tok_az = AutoTokenizer.from_pretrained("OiQ/daa-tokenizers", subfolder="transformers_tokenizers/concat_bpe_16000_tokenizer_az")
46
+
47
+ # Tokenize
48
+ text_ar = "Ω…Ψ§Ψ¨Ω‚Ψ§Ψ΄ ΩƒΩŠΨΉΨ±Ω Ψ΄Ω†Ωˆ يدير، Ψ¨ΩŠΩ† Ψ§Ω„Ω‚Ψ§Ω†ΩˆΩ† ΩˆΨ¨ΩŠΩ† ΩˆΩ„ΩŠΨ―Ψ§Ψͺو."
49
+ text_az = "wash kayn shi jdid?"
50
+ print(tok.encode(text_ar))
51
+ print(tok_az.encode(text_az))
52
+ ```
53
+
54
+ ## Evaluation Metrics
55
+
56
+ ### Surface Metrics
57
+ - **Fertility**: Average tokens per word (lower = more efficient)
58
+ - **CPT**: Characters per token β€” grapheme-aware (higher = more efficient)
59
+ - **OOV Rate**: Out-of-vocabulary rate (0.0 for all tokenizers)
60
+ - **Cross-script Disparity**: |fertility_ar - fertility_az| / max(fertility_ar, fertility_az)
61
+ - **Exact Match Rate**: Fraction of texts that round-trip perfectly
62
+ - **Vocab Gini**: Vocabulary usage inequality (0 = uniform, 1 = concentrated)
63
+ - **Shannon Entropy**: Token distribution entropy
64
+
65
+ ### Morphological Metrics (Arabic-script only)
66
+ - **ΞΌe (Morphological Edit Distance)**: DP alignment between tokenizer token boundaries and Farasa morpheme boundaries. Lower = better morphological alignment.
67
+ - **ΞΌc (Morphological Consistency F1)**: Whether shared morphemes (via KMeans clustering) are assigned shared tokens. Precision/Recall/F1.
68
+
69
+ ### Statistics
70
+ All fertility and CPT metrics include **bootstrap 95% confidence intervals** (n=500 resamples).
71
+
72
+ ## Results Summary
73
+
74
+ ### Best Tokenizers by Vocabulary Size
75
+
76
+ | Vocab Size | Name | Type | Algorithm | Fertility | Disparity | ΞΌe | ΞΌc F1 | Exact Match |
77
+ |------------|------|------|-----------|-----------|-----------|----|----|-------------|
78
+ | 8000 | concat_bpe_8000 | concatenated | BPE | 1.6890 | 0.1649 | 4.2158 | 0.1680 | 99.00% |
79
+ | 16000 | concat_wordpiece_16000 | concatenated | WordPiece | 1.5162 | 0.1361 | 4.1506 | 0.1533 | 99.00% |
80
+ | 32000 | concat_wordpiece_32000 | concatenated | WordPiece | 1.3885 | 0.1309 | 4.0975 | 0.1329 | 99.00% |
81
+
82
+ ### Full Results Table
83
+
84
+ | Name | Type | Algorithm | Vocab | Fertility | CPT | Disparity | Exact Match | Gini | ΞΌe | ΞΌc F1 |
85
+ |------|------|-----------|-------|-----------|-----|-----------|-------------|------|----|----|
86
+ | shared_bpe_8000 | shared | BPE | 8000 | 1.7787 | 3.00 | 0.2199 | 99.00% | 0.7709 | 4.1755 | 0.1617 |
87
+ | concat_bpe_8000 | concatenated | BPE | 8000 | 1.6890 | 3.16 | 0.1649 | 99.00% | 0.6850 | 4.2158 | 0.1680 |
88
+ | shared_unigram_8000 | shared | Unigram | 8000 | 1.7790 | 3.00 | 0.1475 | 99.00% | 0.8189 | 4.1823 | 0.1770 |
89
+ | concat_unigram_8000 | concatenated | Unigram | 8000 | 1.6948 | 3.15 | 0.1669 | 99.00% | 0.7623 | 4.2111 | 0.1801 |
90
+ | shared_wordpiece_8000 | shared | WordPiece | 8000 | 1.7762 | 3.01 | 0.2268 | 99.00% | 0.7565 | 4.1766 | 0.1552 |
91
+ | concat_wordpiece_8000 | concatenated | WordPiece | 8000 | 1.6962 | 3.15 | 0.1560 | 99.00% | 0.6600 | 4.2191 | 0.1619 |
92
+ | shared_bbpe_8000 | shared | BBPE | 8000 | 1.9554 | 2.73 | 0.4718 | 99.00% | 0.8122 | 4.1921 | 0.1995 |
93
+ | concat_bbpe_8000 | concatenated | BBPE | 8000 | 1.8828 | 2.84 | 0.1168 | 99.00% | 0.7491 | 4.2317 | 0.2136 |
94
+ | shared_bpe_16000 | shared | BPE | 16000 | 1.5972 | 3.35 | 0.1511 | 99.00% | 0.7920 | 4.1165 | 0.1324 |
95
+ | concat_bpe_16000 | concatenated | BPE | 16000 | 1.5166 | 3.52 | 0.1427 | 99.00% | 0.7225 | 4.1531 | 0.1376 |
96
+ | shared_unigram_16000 | shared | Unigram | 16000 | 1.6213 | 3.30 | 0.0885 | 99.00% | 0.8335 | 4.1315 | 0.1623 |
97
+ | concat_unigram_16000 | concatenated | Unigram | 16000 | 1.5417 | 3.47 | 0.1464 | 99.00% | 0.7918 | 4.1582 | 0.1575 |
98
+ | shared_wordpiece_16000 | shared | WordPiece | 16000 | 1.5914 | 3.36 | 0.1512 | 99.00% | 0.7797 | 4.1158 | 0.1385 |
99
+ | concat_wordpiece_16000 | concatenated | WordPiece | 16000 | 1.5162 | 3.53 | 0.1361 | 99.00% | 0.7024 | 4.1506 | 0.1533 |
100
+ | shared_bbpe_16000 | shared | BBPE | 16000 | 1.7937 | 2.98 | 0.4265 | 99.00% | 0.8353 | 4.1365 | 0.1824 |
101
+ | concat_bbpe_16000 | concatenated | BBPE | 16000 | 1.7340 | 3.08 | 0.1722 | 99.00% | 0.7908 | 4.1720 | 0.1986 |
102
+ | shared_bpe_32000 | shared | BPE | 32000 | 1.4562 | 3.67 | 0.0727 | 99.00% | 0.7969 | 4.0706 | 0.1149 |
103
+ | concat_bpe_32000 | concatenated | BPE | 32000 | 1.3912 | 3.84 | 0.1340 | 99.00% | 0.7568 | 4.0970 | 0.1146 |
104
+ | shared_unigram_32000 | shared | Unigram | 32000 | 1.5034 | 3.56 | 0.0193 | 99.00% | 0.8325 | 4.0979 | 0.1482 |
105
+ | concat_unigram_32000 | concatenated | Unigram | 32000 | 1.4304 | 3.74 | 0.1449 | 99.00% | 0.8069 | 4.1112 | 0.1420 |
106
+ | shared_wordpiece_32000 | shared | WordPiece | 32000 | 1.4501 | 3.69 | 0.0724 | 99.00% | 0.7886 | 4.0696 | 0.1273 |
107
+ | concat_wordpiece_32000 | concatenated | WordPiece | 32000 | 1.3885 | 3.85 | 0.1309 | 99.00% | 0.7419 | 4.0975 | 0.1329 |
108
+ | shared_bbpe_32000 | shared | BBPE | 32000 | 1.6737 | 3.19 | 0.3737 | 99.00% | 0.8431 | 4.0974 | 0.1737 |
109
+ | concat_bbpe_32000 | concatenated | BBPE | 32000 | 1.6299 | 3.28 | 0.2067 | 99.00% | 0.8217 | 4.1216 | 0.1798 |
110
+
111
+ ## Visualizations
112
+
113
+ ### Fertility Comparison
114
+ ![Fertility Comparison](plots/fertility_overall_comparison_v2.png)
115
+
116
+ ### Cross-Script Disparity
117
+ ![Disparity Comparison](plots/fertility_disparity_comparison_v2.png)
118
+
119
+ ### Script-Level Comparison
120
+ ![Script Comparison](plots/script_comparison_v2.png)
121
+
122
+ ### Bootstrap Confidence Intervals
123
+ ![Bootstrap CI Forest Plot](plots/bootstrap_ci_forest.png)
124
+
125
+ ### Morphological Edit Distance (ΞΌe)
126
+ ![Morph Edit Distance](plots/morph_edit_distance_ar_comparison_v2.png)
127
+
128
+ ### Morphological Consistency (ΞΌc F1)
129
+ ![Morph Consistency F1](plots/morph_consistency_f1_comparison_v2.png)
130
+
131
+ ### CPT Trends
132
+ ![CPT Trends](plots/cpt_overall_trends.png)
133
+
134
+ ### Fertility Heatmap
135
+ ![Fertility Heatmap](plots/fertility_overall_heatmap_v2.png)
136
+
137
+ ### Morph Edit Distance Heatmap
138
+ ![Morph Edit Distance Heatmap](plots/morph_edit_distance_ar_heatmap_v2.png)
139
+
140
+ ## Repository Structure
141
+
142
+ ```
143
+ daa-tokenizers/
144
+ β”œβ”€β”€ README.md # This file
145
+ β”œβ”€β”€ script.py # Benchmark script (reproducible)
146
+ β”œβ”€β”€ tokenizer_results.csv # Full metrics table
147
+ β”œβ”€β”€ tokenizer_results.json # Full metrics (JSON)
148
+ β”œβ”€β”€ bootstrap_ci.csv # Bootstrap 95% CIs
149
+ β”œβ”€β”€ benchmark_report.md # Detailed report
150
+ β”œβ”€β”€ morphology/
151
+ β”‚ └── farasa_segmentations.json # Cached Farasa morph segmentations (~100MB)
152
+ β”œβ”€β”€ tokenizers/ # Raw tokenizer JSON files (36 files)
153
+ β”œβ”€β”€ transformers_tokenizers/ # 28 dirs for transformers library
154
+ β”‚ β”œβ”€β”€ shared_bpe_8000/
155
+ β”‚ β”œβ”€β”€ shared_bpe_16000/
156
+ β”‚ β”œβ”€β”€ ...
157
+ β”‚ β”œβ”€β”€ concat_bpe_8000_tokenizer_ar/
158
+ β”‚ β”œβ”€β”€ concat_bpe_8000_tokenizer_az/
159
+ β”‚ └── ...
160
+ └── plots/ # 24 visualization PNGs
161
+ β”œβ”€β”€ fertility_overall_comparison_v2.png
162
+ β”œβ”€β”€ bootstrap_ci_forest.png
163
+ β”œβ”€β”€ morph_edit_distance_ar_heatmap_v2.png
164
+ └── ...
165
+ ```
166
+
167
+ ## Methodology
168
+
169
+ 1. **Dataset**: `OiQ/daa-pairs` β€” 112,814 Moroccan Darija sentence pairs in Arabic script and Arabizi
170
+ 2. **Pre-tokenization**: Metaspace (BPE/Unigram/WordPiece) or ByteLevel (BBPE)
171
+ 3. **Training**: HuggingFace `tokenizers` library with standard trainer hyperparameters
172
+ 4. **Evaluation**: 14 metrics including fertility, CPT, disparity, exact match, morphological alignment (Farasa), and bootstrap CIs
173
+ 5. **Export**: `transformers`-compatible format via `PreTrainedTokenizerFast`
174
+
175
+ ## Key Findings
176
+
177
+ - **Concatenated tokenizers** reduce cross-script fertility disparity compared to shared-vocabulary tokenizers
178
+ - **WordPiece** at 32K achieves the lowest overall fertility (1.39) among concatenated tokenizers
179
+ - **BBPE** guarantees 100% exact reconstruction but has higher fertility due to byte-level granularity
180
+ - **Unigram** models produce the lowest cross-script disparity among shared tokenizers
181
+ - Morphological metrics (ΞΌe, ΞΌc) reveal that no single algorithm dominates morphological alignment β€” BBPE achieves highest consistency F1 while BPE achieves lowest edit distance
182
+
183
+ ## Citation
184
+
185
+ ```bibtex
186
+ @misc{oiq2025daa-tokenizers,
187
+ title={Darija Subword Tokenizer Benchmark},
188
+ author={OiQ},
189
+ year={2025},
190
+ url={https://huggingface.co/OiQ/daa-tokenizers}
191
+ }
192
+ ```