Metadata-Version: 2.4
Name: shamy-arabic-nlp
Version: 0.1.1
Summary: Arabic normalizer, Arabizi to Arabic converter and dialect classifier baseline. Pure Python. By Omar Elshamy.
Author: Omar Elshamy
License: MIT
Project-URL: Homepage, https://github.com/omarelshamyx-dotcom/shamy-arabic-nlp
Project-URL: Source, https://github.com/omarelshamyx-dotcom/shamy-arabic-nlp
Project-URL: PyPI, https://pypi.org/project/shamy-arabic-nlp/
Keywords: arabic,nlp,arabizi,dialect,egyptian,normalization
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Natural Language :: Arabic
Classifier: Topic :: Text Processing :: Linguistic
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# shamy-arabic-nlp

[![PyPI version](https://img.shields.io/pypi/v/shamy-arabic-nlp.svg)](https://pypi.org/project/shamy-arabic-nlp/)
[![Python versions](https://img.shields.io/pypi/pyversions/shamy-arabic-nlp.svg)](https://pypi.org/project/shamy-arabic-nlp/)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![tests](https://github.com/omarelshamyx-dotcom/shamy-arabic-nlp/actions/workflows/tests.yml/badge.svg)](https://github.com/omarelshamyx-dotcom/shamy-arabic-nlp/actions/workflows/tests.yml)

Arabic text tools in pure Python: normalization, Arabizi (Latin chat Arabic) to Arabic script, and a trainable dialect classifier baseline. No dependencies, no models to download.

مكتبة بايثون للغة العربية بدون اعتماديات: توحيد النص، تحويل الفرانكو/العربيزي إلى حروف عربية، ونموذج أساسي لتصنيف اللهجة.

```python
>>> from shamy_arabic import normalize, arabizi_to_arabic, predict_dialect
>>> normalize("أَحْمَد جداااا ٣")
'احمد جدا 3'
>>> arabizi_to_arabic("ezayak ya 7abibi")
'ازيك يا حبيبي'
>>> predict_dialect("ازيك يا باشا عامل ايه")
'EGY'
```

## What is in here

1. **Normalizer** (`shamy_arabic.normalize`): strips tashkeel and tatweel, unifies alef forms, converts Arabic-Indic and Persian digits to 0-9, maps Arabic punctuation, collapses stretched letters and extra whitespace. Every rule is a keyword argument you can turn off.
2. **Arabizi to Arabic** (`shamy_arabic.arabizi_to_arabic`): rule based conversion tuned for Egyptian, Levantine and Gulf chat spelling. Handles the digit letters (2 3 5 6 7 8 9), digraphs (sh kh gh th dh ch), the `el`/`al` definite article, and a lexicon of very common words. `is_arabizi()` tells you whether a string looks like Arabizi at all.
3. **Dialect classifier** (`shamy_arabic.DialectClassifier`): multinomial Naive Bayes over word and character n-grams for MSA, Egyptian (EGY), Gulf (GLF), Levantine (LEV) and Maghrebi (MAG). Ships with a small seed corpus, trains on your own data in a few lines, and serializes to JSON.

## Install

```bash
pip install shamy-arabic-nlp
```

Python 3.8 or newer. No dependencies.

## Quick start

```python
from shamy_arabic import (
    normalize, strip_diacritics, to_ascii_digits, squeeze_repeats,
    arabizi_to_arabic, is_arabizi,
    DialectClassifier, default_classifier, predict_dialect, DIALECTS,
)

normalize("أَحْمَد  إبراهيم آمن؟")        # 'احمد ابراهيم امن?'
normalize("مصرررر ٣")                     # 'مصر 3'
normalize("على", ya=True)                 # 'علي'
normalize("مدرسة", ta_marbuta=True)       # 'مدرسه'
normalize("مدرسة")                        # 'مدرسة'  (left alone by default)

strip_diacritics("مُحَمَّدٌ")              # 'محمد'
to_ascii_digits("٢٠٢٤ و۱۲")                # '2024 و12'
squeeze_repeats("جداااا", 1)               # 'جدا'

arabizi_to_arabic("ana 3ayez a2ol 7aga")  # 'انا عايز اقول حاجة'
arabizi_to_arabic("ahlan, ezayak?")       # 'اهلا، ازيك؟'
is_arabizi("ana 3ayez a2ol 7aga")         # True

predict_dialect("ازيك يا باشا عامل ايه")   # 'EGY'
```

## API

| Name | Signature | What it does |
| --- | --- | --- |
| `normalize` | `(text, *, alef=True, ya=False, ta_marbuta=False, digits=True, punct=True, repeats=1, diacritics=True)` | Full pipeline. `repeats=0` disables collapsing; `ya` and `ta_marbuta` are off because they change meaning in dialects. |
| `strip_diacritics` | `(text)` | Removes harakat, shadda, sukun, dagger alef and tatweel. |
| `to_ascii_digits` | `(text)` | Arabic-Indic and Persian digits to `0-9`. |
| `squeeze_repeats` | `(text, keep=2)` | Collapses runs of 3+ identical characters. |
| `arabizi_to_arabic` | `(text)` | Converts Arabizi words, leaves Arabic text and numbers alone. |
| `is_arabizi` | `(text)` | Heuristic: Latin letters plus Arabizi digits or known words. |
| `DialectClassifier` | `(alpha=0.3)` | `.fit(data)`, `.predict(text)`, `.predict_proba(text)`, `.accuracy(data)`, `.save(path)`, `DialectClassifier.load(path)`. |
| `default_classifier` | `()` | Lazily trained classifier over the built-in corpus. |
| `predict_dialect` | `(text)` | One of `DIALECTS = ("MSA", "EGY", "GLF", "LEV", "MAG")`. |

### Training on your own data

`fit()` accepts either a dict of `label -> [sentences]` or an iterable of `(text, label)` pairs:

```python
from shamy_arabic import DialectClassifier

clf = DialectClassifier().fit({
    "EGY": ["ازيك يا باشا عامل ايه", "انا رايح الشغل دلوقتي"],
    "GLF": ["شلونك يا طويل العمر", "وين رايح الحين"],
})
clf.predict("شلونك عساك بخير")     # 'GLF'
clf.predict_proba("ازيك")          # {'EGY': 0.93, 'GLF': 0.07}
clf.save("dialect.model.json")
clf2 = DialectClassifier.load("dialect.model.json")
```

For real work, retrain on a proper dataset. MADAR and NADI are the usual starting points; check their licences first.

## Honest limits

- **Arabizi is ambiguous.** Short vowels are not written, so `kitab` could be several words. The converter leans on a lexicon for common words and on letter rules otherwise: good on common chat words, rough elsewhere. Extend `LEXICON` in `shamy_arabic/arabizi.py` for your own domain.
- **The dialect model is a baseline, not a product.** It is trained on about 100 short sentences written for this repository (`shamy_arabic/_corpus.py`) and tested on 15 held-out ones. High accuracy there does not mean it works on real social media text.
- **Normalization is deliberately conservative.** `ya` and `ta_marbuta` are opt-in because Egyptian spelling distinguishes ى/ي and ة/ه in ways that matter for matching.

## Development

```bash
git clone https://github.com/omarelshamyx-dotcom/shamy-arabic-nlp.git
cd shamy-arabic-nlp
pip install -e .
pip install pytest
pytest -q
```

## License

MIT. See [LICENSE](LICENSE). Copyright (c) 2026 Omar Elshamy.
