ar / README.md
omarkamali's picture
Upload all models and assets for ar (20251201)
02fa115 verified
---
language: ar
language_name: Arabic
language_family: arabic
tags:
- wikilangs
- nlp
- tokenizer
- embeddings
- n-gram
- markov
- wikipedia
- monolingual
- family-arabic
license: mit
library_name: wikilangs
pipeline_tag: feature-extraction
datasets:
- omarkamali/wikipedia-monthly
dataset_info:
name: wikipedia-monthly
description: Monthly snapshots of Wikipedia articles across 300+ languages
metrics:
- name: best_compression_ratio
type: compression
value: 4.103
- name: best_isotropy
type: isotropy
value: 0.7155
- name: vocabulary_size
type: vocab
value: 1000000
generated: 2025-12-27
---
# Arabic - Wikilangs Models
## Comprehensive Research Report & Full Ablation Study
This repository contains NLP models trained and evaluated by Wikilangs, specifically on **Arabic** Wikipedia data.
We analyze tokenizers, n-gram models, Markov chains, vocabulary statistics, and word embeddings.
## 📋 Repository Contents
### Models & Assets
- Tokenizers (8k, 16k, 32k, 64k)
- N-gram models (2, 3, 4-gram)
- Markov chains (context of 1, 2, 3 and 4)
- Subword N-gram and Markov chains
- Embeddings in various sizes and dimensions
- Language Vocabulary
- Language Statistics
![Performance Dashboard](visualizations/performance_dashboard.png)
### Analysis and Evaluation
- [1. Tokenizer Evaluation](#1-tokenizer-evaluation)
- [2. N-gram Model Evaluation](#2-n-gram-model-evaluation)
- [3. Markov Chain Evaluation](#3-markov-chain-evaluation)
- [4. Vocabulary Analysis](#4-vocabulary-analysis)
- [5. Word Embeddings Evaluation](#5-word-embeddings-evaluation)
- [6. Summary & Recommendations](#6-summary--recommendations)
- [Metrics Glossary](#appendix-metrics-glossary--interpretation-guide)
- [Visualizations Index](#visualizations-index)
---
## 1. Tokenizer Evaluation
![Tokenizer Compression](visualizations/tokenizer_compression.png)
### Results
| Vocab Size | Compression | Avg Token Len | UNK Rate | Total Tokens |
|------------|-------------|---------------|----------|--------------|
| **8k** | 3.156x | 3.13 | 0.0848% | 5,982,398 |
| **16k** | 3.513x | 3.49 | 0.0944% | 5,374,291 |
| **32k** | 3.837x | 3.81 | 0.1031% | 4,920,728 |
| **64k** | 4.103x 🏆 | 4.07 | 0.1103% | 4,602,368 |
### Tokenization Examples
Below are sample sentences tokenized with each vocabulary size:
**Sample 1:** `تحويل ميلفورد (كونيتيكت)`
| Vocab | Tokens | Count |
|-------|--------|-------|
| 8k | `▁تحويل ▁ميل فورد ▁( ك وني تي كت )` | 9 |
| 16k | `▁تحويل ▁ميل فورد ▁( ك وني تي كت )` | 9 |
| 32k | `▁تحويل ▁ميل فورد ▁( كوني تيكت )` | 7 |
| 64k | `▁تحويل ▁ميل فورد ▁( كونيتيكت )` | 6 |
**Sample 2:** `قد يقصد من «الفرفار» :
الفرفار (إدا وكماض) : دوار تابع لجماعة إدا وڭماض في إقل...`
| Vocab | Tokens | Count |
|-------|--------|-------|
| 8k | `▁قد ▁يق صد ▁من ▁« الف رف ار » ▁: ... (+43 more)` | 53 |
| 16k | `▁قد ▁يقصد ▁من ▁« الف رف ار » ▁: ▁الف ... (+37 more)` | 47 |
| 32k | `▁قد ▁يقصد ▁من ▁« الف رف ار » ▁: ▁الف ... (+36 more)` | 46 |
| 64k | `▁قد ▁يقصد ▁من ▁« الف رف ار » ▁: ▁الف ... (+34 more)` | 44 |
**Sample 3:** `المراجع
تصنيف:أنهار إفريقية دولية
تصنيف:أنهار بوروندي
تصنيف:أنهار تنزانيا
تصني...`
| Vocab | Tokens | Count |
|-------|--------|-------|
| 8k | `▁المراجع ▁تصنيف : أن هار ▁إ فريقية ▁دولية ▁تصنيف : ... (+16 more)` | 26 |
| 16k | `▁المراجع ▁تصنيف : أنهار ▁إفريقية ▁دولية ▁تصنيف : أنهار ▁بور ... (+12 more)` | 22 |
| 32k | `▁المراجع ▁تصنيف : أنهار ▁إفريقية ▁دولية ▁تصنيف : أنهار ▁بور ... (+9 more)` | 19 |
| 64k | `▁المراجع ▁تصنيف : أنهار ▁إفريقية ▁دولية ▁تصنيف : أنهار ▁بوروندي ... (+8 more)` | 18 |
### Key Findings
- **Best Compression:** 64k achieves 4.103x compression
- **Lowest UNK Rate:** 8k with 0.0848% unknown tokens
- **Trade-off:** Larger vocabularies improve compression but increase model size
- **Recommendation:** 32k vocabulary provides optimal balance for production use
---
## 2. N-gram Model Evaluation
![N-gram Perplexity](visualizations/ngram_perplexity.png)
![N-gram Coverage](visualizations/ngram_coverage.png)
### Results
| N-gram | Perplexity | Entropy | Unique N-grams | Top-100 Coverage | Top-1000 Coverage |
|--------|------------|---------|----------------|------------------|-------------------|
| **2-gram** | 224,018 🏆 | 17.77 | 6,245,473 | 10.5% | 22.3% |
| **2-gram** | 514 🏆 | 9.00 | 52,884 | 52.6% | 94.6% |
| **3-gram** | 831,530 | 19.67 | 14,344,223 | 6.6% | 16.3% |
| **3-gram** | 4,885 | 12.25 | 487,957 | 23.0% | 53.9% |
| **4-gram** | 1,784,666 | 20.77 | 25,822,600 | 4.6% | 13.6% |
| **4-gram** | 29,916 | 14.87 | 3,376,435 | 13.3% | 31.6% |
### Top 5 N-grams by Size
**2-grams:**
| Rank | N-gram | Count |
|------|--------|-------|
| 1 | `تصنيف :` | 9,397,729 |
| 2 | `ً ا` | 2,647,403 |
| 3 | `: لاعبو` | 1,539,560 |
| 4 | `| |` | 1,324,145 |
| 5 | `كرة قدم` | 758,315 |
**3-grams:**
| Rank | N-gram | Count |
|------|--------|-------|
| 1 | `تصنيف : لاعبو` | 1,539,552 |
| 2 | `تصنيف : مواليد` | 617,808 |
| 3 | `: لاعبو كرة` | 498,223 |
| 4 | `| | |` | 459,400 |
| 5 | `تصنيف : أشخاص` | 441,938 |
**4-grams:**
| Rank | N-gram | Count |
|------|--------|-------|
| 1 | `تصنيف : لاعبو كرة` | 498,220 |
| 2 | `: لاعبو كرة قدم` | 381,016 |
| 3 | `القرن 20 تصنيف :` | 278,900 |
| 4 | `في القرن 20 تصنيف` | 266,135 |
| 5 | `| | | |` | 255,908 |
### Key Findings
- **Best Perplexity:** 2-gram with 514
- **Entropy Trend:** Decreases with larger n-grams (more predictable)
- **Coverage:** Top-1000 patterns cover ~32% of corpus
- **Recommendation:** 4-gram or 5-gram for best predictive performance
---
## 3. Markov Chain Evaluation
![Markov Entropy](visualizations/markov_entropy.png)
![Markov Branching](visualizations/markov_branching.png)
### Results
| Context | Avg Entropy | Perplexity | Branching Factor | Unique Contexts | Predictability |
|---------|-------------|------------|------------------|-----------------|----------------|
| **1** | 0.7411 | 1.671 | 12.81 | 5,367,543 | 25.9% |
| **1** | 1.8418 | 3.585 | 17.44 | 13,038 | 0.0% |
| **2** | 0.4074 | 1.326 | 2.70 | 68,744,585 | 59.3% |
| **2** | 0.7015 | 1.626 | 5.08 | 227,339 | 29.9% |
| **3** | 0.1748 | 1.129 | 1.44 | 185,531,332 | 82.5% |
| **3** | 0.8426 | 1.793 | 5.22 | 1,153,787 | 15.7% |
| **4** | 0.0757 🏆 | 1.054 | 1.16 | 267,713,644 | 92.4% |
| **4** | 0.7645 🏆 | 1.699 | 3.88 | 6,025,353 | 23.6% |
### Generated Text Samples
Below are text samples generated from each Markov chain model:
**Context Size 1:**
1. `. اكت ُ ص م َ ّ ة . أخته ، وافتتاح مشروع مرصد أونديجوف |`
2. `في المدار في جمهورية ألمانيا تصنيف : خلافات في . أنظر : تشغيل الحواسيب . 1`
3. `، واستحوذت أيضا التحريفية للبلغاريين والأجانب أو تمليح اللحوم والأتواب والملابس والبطانيات الى القاه...`
**Context Size 2:**
1. `تصنيف : كتاب ومؤلفو قصص مصورة تصنيف : فائزون بميداليات برونزية في ألعاب الكومنولث في إنجلترا تصنيف`
2. `ً ا للاغتسال . وقال القرطبي في تفسيره على أنه آمن خلال الرضاعة الطبيعية يسبب زيادة الكوليسترول`
3. `: لاعبو كرة قدم صرب مغتربون في روسيا تصنيف : أفلام دراما باللغة الإنجليزية تصنيف : سائقو`
**Context Size 3:**
1. `تصنيف : لاعبو بوتكيت ريد سوكس تصنيف : مواليد 1955 تصنيف : مؤيدون لتنظيم ملكية الأسلحة تصنيف :`
2. `تصنيف : مواليد 1986 تصنيف : لاعبو وسط كرة قدم رجالية تصنيف : مواليد 1390 هـ تصنيف :`
3. `: لاعبو كرة قدم مغاربة تصنيف : عداؤو مسافات متوسطة نيوزيلنديون تصنيف : مواليد 1981 تصنيف : مواليد`
**Context Size 4:**
1. `تصنيف : لاعبو كرة قدم مغتربون في المجر تصنيف : لاعبو كرة اليد في الألعاب الأولمبية الصيفية 1956 تصني...`
2. `: لاعبو كرة قدم مغتربون في إنجلترا تصنيف : لاعبو كرة قدم مغتربون في إيطاليا تصنيف : أماكن مأهولة`
3. `القرن 20 تصنيف : كاتبات أمريكيات في القرن 20 تصنيف : شعراء بالعربية في القرن 21 تصنيف : لاعبو`
### Key Findings
- **Best Predictability:** Context-4 with 92.4% predictability
- **Branching Factor:** Decreases with context size (more deterministic)
- **Memory Trade-off:** Larger contexts require more storage (6,025,353 contexts)
- **Recommendation:** Context-3 or Context-4 for text generation
---
## 4. Vocabulary Analysis
![Zipf's Law](visualizations/zipf_law.png)
![Top Words](visualizations/top20_words.png)
![Coverage Curve](visualizations/vocab_coverage.png)
### Statistics
| Metric | Value |
|--------|-------|
| Vocabulary Size | 1,000,000 |
| Total Tokens | 366,842,150 |
| Mean Frequency | 366.84 |
| Median Frequency | 12 |
| Frequency Std Dev | 20900.79 |
### Most Common Words
| Rank | Word | Frequency |
|------|------|-----------|
| 1 | في | 14,346,570 |
| 2 | تصنيف | 9,437,038 |
| 3 | من | 8,350,052 |
| 4 | على | 3,295,037 |
| 5 | ا | 2,755,855 |
| 6 | إلى | 2,451,934 |
| 7 | عام | 1,684,151 |
| 8 | لاعبو | 1,540,822 |
| 9 | أن | 1,441,897 |
| 10 | مع | 1,171,753 |
### Least Common Words (from vocabulary)
| Rank | Word | Frequency |
|------|------|-----------|
| 1 | твоим | 4 |
| 2 | своему | 4 |
| 3 | вашей | 4 |
| 4 | нашу | 4 |
| 5 | кого | 4 |
| 6 | чьей | 4 |
| 7 | работать | 4 |
| 8 | говорит | 4 |
| 9 | говорят | 4 |
| 10 | идёт | 4 |
### Zipf's Law Analysis
| Metric | Value |
|--------|-------|
| Zipf Coefficient | 0.9655 |
| R² (Goodness of Fit) | 0.990109 |
| Adherence Quality | **excellent** |
### Coverage Analysis
| Top N Words | Coverage |
|-------------|----------|
| Top 100 | 24.9% |
| Top 1,000 | 48.1% |
| Top 5,000 | 68.6% |
| Top 10,000 | 76.6% |
### Key Findings
- **Zipf Compliance:** R²=0.9901 indicates excellent adherence to Zipf's law
- **High Frequency Dominance:** Top 100 words cover 24.9% of corpus
- **Long Tail:** 990,000 words needed for remaining 23.4% coverage
---
## 5. Word Embeddings Evaluation
![Embedding Isotropy](visualizations/embedding_isotropy.png)
![Similarity Matrix](visualizations/embedding_similarity.png)
![t-SNE Words](visualizations/tsne_words.png)
![t-SNE Sentences](visualizations/tsne_sentences.png)
### Model Comparison
| Model | Vocab Size | Dimension | Avg Norm | Std Norm | Isotropy |
|-------|------------|-----------|----------|----------|----------|
| **mono_32d** | 1,505,991 | 32 | 3.562 | 1.491 | 0.7155 🏆 |
| **mono_64d** | 1,505,991 | 64 | 3.899 | 1.405 | 0.7134 |
| **mono_128d** | 1,505,991 | 128 | 4.337 | 1.358 | 0.6849 |
| **embeddings_enhanced** | 0 | 0 | 0.000 | 0.000 | 0.0000 |
### Key Findings
- **Best Isotropy:** mono_32d with 0.7155 (more uniform distribution)
- **Dimension Trade-off:** Higher dimensions capture more semantics but reduce isotropy
- **Vocabulary Coverage:** All models cover 1,505,991 words
- **Recommendation:** 100d for balanced semantic capture and efficiency
---
## 6. Summary & Recommendations
![Performance Dashboard](visualizations/performance_dashboard.png)
### Production Recommendations
| Component | Recommended | Rationale |
|-----------|-------------|-----------|
| Tokenizer | **32k BPE** | Best compression (4.10x) with low UNK rate |
| N-gram | **5-gram** | Lowest perplexity (514) |
| Markov | **Context-4** | Highest predictability (92.4%) |
| Embeddings | **100d** | Balanced semantic capture and isotropy |
---
## Appendix: Metrics Glossary & Interpretation Guide
This section provides definitions, intuitions, and guidance for interpreting the metrics used throughout this report.
### Tokenizer Metrics
**Compression Ratio**
> *Definition:* The ratio of characters to tokens (chars/token). Measures how efficiently the tokenizer represents text.
>
> *Intuition:* Higher compression means fewer tokens needed to represent the same text, reducing sequence lengths for downstream models. A 3x compression means ~3 characters per token on average.
>
> *What to seek:* Higher is generally better for efficiency, but extremely high compression may indicate overly aggressive merging that loses morphological information.
**Average Token Length (Fertility)**
> *Definition:* Mean number of characters per token produced by the tokenizer.
>
> *Intuition:* Reflects the granularity of tokenization. Longer tokens capture more context but may struggle with rare words; shorter tokens are more flexible but increase sequence length.
>
> *What to seek:* Balance between 2-5 characters for most languages. Arabic/morphologically-rich languages may benefit from slightly longer tokens.
**Unknown Token Rate (OOV Rate)**
> *Definition:* Percentage of tokens that map to the unknown/UNK token, indicating words the tokenizer cannot represent.
>
> *Intuition:* Lower OOV means better vocabulary coverage. High OOV indicates the tokenizer encounters many unseen character sequences.
>
> *What to seek:* Below 1% is excellent; below 5% is acceptable. BPE tokenizers typically achieve very low OOV due to subword fallback.
### N-gram Model Metrics
**Perplexity**
> *Definition:* Measures how "surprised" the model is by test data. Mathematically: 2^(cross-entropy). Lower values indicate better prediction.
>
> *Intuition:* If perplexity is 100, the model is as uncertain as if choosing uniformly among 100 options at each step. A perplexity of 10 means effectively choosing among 10 equally likely options.
>
> *What to seek:* Lower is better. Perplexity decreases with larger n-grams (more context). Values vary widely by language and corpus size.
**Entropy**
> *Definition:* Average information content (in bits) needed to encode the next token given the context. Related to perplexity: perplexity = 2^entropy.
>
> *Intuition:* High entropy means high uncertainty/randomness; low entropy means predictable patterns. Natural language typically has entropy between 1-4 bits per character.
>
> *What to seek:* Lower entropy indicates more predictable text patterns. Entropy should decrease as n-gram size increases.
**Coverage (Top-K)**
> *Definition:* Percentage of corpus occurrences explained by the top K most frequent n-grams.
>
> *Intuition:* High coverage with few patterns indicates repetitive/formulaic text; low coverage suggests diverse vocabulary usage.
>
> *What to seek:* Depends on use case. For language modeling, moderate coverage (40-60% with top-1000) is typical for natural text.
### Markov Chain Metrics
**Average Entropy**
> *Definition:* Mean entropy across all contexts, measuring average uncertainty in next-word prediction.
>
> *Intuition:* Lower entropy means the model is more confident about what comes next. Context-1 has high entropy (many possible next words); Context-4 has low entropy (few likely continuations).
>
> *What to seek:* Decreasing entropy with larger context sizes. Very low entropy (<0.1) indicates highly deterministic transitions.
**Branching Factor**
> *Definition:* Average number of unique next tokens observed for each context.
>
> *Intuition:* High branching = many possible continuations (flexible but uncertain); low branching = few options (predictable but potentially repetitive).
>
> *What to seek:* Branching factor should decrease with context size. Values near 1.0 indicate nearly deterministic chains.
**Predictability**
> *Definition:* Derived metric: (1 - normalized_entropy) × 100%. Indicates how deterministic the model's predictions are.
>
> *Intuition:* 100% predictability means the next word is always certain; 0% means completely random. Real text falls between these extremes.
>
> *What to seek:* Higher predictability for text generation quality, but too high (>98%) may produce repetitive output.
### Vocabulary & Zipf's Law Metrics
**Zipf's Coefficient**
> *Definition:* The slope of the log-log plot of word frequency vs. rank. Zipf's law predicts this should be approximately -1.
>
> *Intuition:* A coefficient near -1 indicates the corpus follows natural language patterns where a few words are very common and most words are rare.
>
> *What to seek:* Values between -0.8 and -1.2 indicate healthy natural language distribution. Deviations may suggest domain-specific or artificial text.
**R² (Coefficient of Determination)**
> *Definition:* Measures how well the linear fit explains the frequency-rank relationship. Ranges from 0 to 1.
>
> *Intuition:* R² near 1.0 means the data closely follows Zipf's law; lower values indicate deviation from expected word frequency patterns.
>
> *What to seek:* R² > 0.95 is excellent; > 0.99 indicates near-perfect Zipf adherence typical of large natural corpora.
**Vocabulary Coverage**
> *Definition:* Cumulative percentage of corpus tokens accounted for by the top N words.
>
> *Intuition:* Shows how concentrated word usage is. If top-100 words cover 50% of text, the corpus relies heavily on common words.
>
> *What to seek:* Top-100 covering 30-50% is typical. Higher coverage indicates more repetitive text; lower suggests richer vocabulary.
### Word Embedding Metrics
**Isotropy**
> *Definition:* Measures how uniformly distributed vectors are in the embedding space. Computed as the ratio of minimum to maximum singular values.
>
> *Intuition:* High isotropy (near 1.0) means vectors spread evenly in all directions; low isotropy means vectors cluster in certain directions, reducing expressiveness.
>
> *What to seek:* Higher isotropy generally indicates better-quality embeddings. Values > 0.1 are reasonable; > 0.3 is good. Lower-dimensional embeddings tend to have higher isotropy.
**Average Norm**
> *Definition:* Mean magnitude (L2 norm) of word vectors in the embedding space.
>
> *Intuition:* Indicates the typical "length" of vectors. Consistent norms suggest stable training; high variance may indicate some words are undertrained.
>
> *What to seek:* Relatively consistent norms across models. The absolute value matters less than consistency (low std deviation).
**Cosine Similarity**
> *Definition:* Measures angular similarity between vectors, ranging from -1 (opposite) to 1 (identical direction).
>
> *Intuition:* Words with similar meanings should have high cosine similarity. This is the standard metric for semantic relatedness in embeddings.
>
> *What to seek:* Semantically related words should score > 0.5; unrelated words should be near 0. Synonyms often score > 0.7.
**t-SNE Visualization**
> *Definition:* t-Distributed Stochastic Neighbor Embedding - a dimensionality reduction technique that preserves local structure for visualization.
>
> *Intuition:* Clusters in t-SNE plots indicate groups of semantically related words. Spread indicates vocabulary diversity; tight clusters suggest semantic coherence.
>
> *What to seek:* Meaningful clusters (e.g., numbers together, verbs together). Avoid over-interpreting distances - t-SNE preserves local, not global, structure.
### General Interpretation Guidelines
1. **Compare within model families:** Metrics are most meaningful when comparing models of the same type (e.g., 8k vs 64k tokenizer).
2. **Consider trade-offs:** Better performance on one metric often comes at the cost of another (e.g., compression vs. OOV rate).
3. **Context matters:** Optimal values depend on downstream tasks. Text generation may prioritize different metrics than classification.
4. **Corpus influence:** All metrics are influenced by corpus characteristics. Wikipedia text differs from social media or literature.
5. **Language-specific patterns:** Morphologically rich languages (like Arabic) may show different optimal ranges than analytic languages.
### Visualizations Index
| Visualization | Description |
|---------------|-------------|
| Tokenizer Compression | Compression ratios by vocabulary size |
| Tokenizer Fertility | Average token length by vocabulary |
| Tokenizer OOV | Unknown token rates |
| Tokenizer Total Tokens | Total tokens by vocabulary |
| N-gram Perplexity | Perplexity by n-gram size |
| N-gram Entropy | Entropy by n-gram size |
| N-gram Coverage | Top pattern coverage |
| N-gram Unique | Unique n-gram counts |
| Markov Entropy | Entropy by context size |
| Markov Branching | Branching factor by context |
| Markov Contexts | Unique context counts |
| Zipf's Law | Frequency-rank distribution with fit |
| Vocab Frequency | Word frequency distribution |
| Top 20 Words | Most frequent words |
| Vocab Coverage | Cumulative coverage curve |
| Embedding Isotropy | Vector space uniformity |
| Embedding Norms | Vector magnitude distribution |
| Embedding Similarity | Word similarity heatmap |
| Nearest Neighbors | Similar words for key terms |
| t-SNE Words | 2D word embedding visualization |
| t-SNE Sentences | 2D sentence embedding visualization |
| Position Encoding | Encoding method comparison |
| Model Sizes | Storage requirements |
| Performance Dashboard | Comprehensive performance overview |
---
## About This Project
### Data Source
Models trained on [wikipedia-monthly](https://huggingface.co/datasets/omarkamali/wikipedia-monthly) - a monthly snapshot of Wikipedia articles across 300+ languages.
### Project
A project by **[Wikilangs](https://wikilangs.org)** - Open-source NLP models for every Wikipedia language.
### Maintainer
[Omar Kamali](https://omarkamali.com) - [Omneity Labs](https://omneitylabs.com)
### Citation
If you use these models in your research, please cite:
```bibtex
@misc{wikilangs2025,
author = {Kamali, Omar},
title = {Wikilangs: Open NLP Models for Wikipedia Languages},
year = {2025},
publisher = {HuggingFace},
url = {https://huggingface.co/wikilangs}
institution = {Omneity Labs}
}
```
### License
MIT License - Free for academic and commercial use.
### Links
- 🌐 Website: [wikilangs.org](https://wikilangs.org)
- 🤗 Models: [huggingface.co/wikilangs](https://huggingface.co/wikilangs)
- 📊 Data: [wikipedia-monthly](https://huggingface.co/datasets/omarkamali/wikipedia-monthly)
- 👤 Author: [Omar Kamali](https://huggingface.co/omarkamali)
---
*Generated by Wikilangs Models Pipeline*
*Report Date: 2025-12-27 16:32:09*