Skip to content
项目

关于

Arabic normalization, Arabizi to Arabic conversion and a dialect classifier baseline in pure Python. No dependencies. MIT.

人工智能与机器学习

这些数字如何统计

6 星标29 复刻 +1 24小时

实测星标增长

24小时
+1
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

shamy-arabic-nlp

PyPI version Python versions License: MIT tests

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.

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

>>> 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, word-final -a as ta marbuta (madrasa -> مدرسة), and a lexicon of common Egyptian, Levantine and Gulf words (3ayez -> عايز, a2ol -> اقول, 7aga -> حاجة). 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

pip install shamy-arabic-nlp

Python 3.8 or newer. No dependencies.

Quick start

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:

from shamy_arabic import DialectClassifier

clf = DialectClassifier().fit({
    "EGY": ["ازيك يا باشا عامل ايه", "انا رايح الشغل دلوقتي"],
    "GLF": ["شلونك يا طويل العمر", "وين رايح الحين"],
})
clf.predict("شلونك عساك بخير")     # 'GLF'
clf.predict_proba("ازيك")          # {'EGY': 1.0, 'GLF': 0.0}  (sharp on a tiny training set)
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 digit 2 is both hamza and qaf: the letter rule maps it to ء, and the lexicon covers the common qaf words (a2ol -> اقول, 2alb -> قلب, 2ahwa -> قهوة). Word-final -a is treated as ta marbuta, which is right for most nouns (madrasa -> مدرسة) and wrong for a few particles. 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

git clone https://github.com/omarelshamy25x/shamy-arabic-nlp.git
cd shamy-arabic-nlp
pip install -e .
pip install pytest
pytest -q

Contributing

Contributions are welcome, including small ones. You do not need to ask first.

  1. Pick an issue labelled good first issue and comment that you are taking it.
  2. Fork the repo and install it with pip install -e ., then run python -m pytest.
  3. Make one focused change with a test, and open a pull request that says what it changes and why.

Details are in CONTRIBUTING.md. Arabic and English are both fine in issues and pull requests.

License

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

探索相关项目

加载中…