Skip to main content

devsamhan-arabic-text (Python)

مكتبة Python لتطبيع النص العربي — تشكيل، ألفات، همزات، بحث، فرز.

بدون تبعيات خارجية. متوافقة مع مواصفات devsamhan-arabic v1.0.1.

التثبيت

pip install devsamhan-arabic-text

أو محلياً:

pip install -e .

الاستخدام السريع

from devsamhan_arabic_text import (
    to_search_key, to_loose_search_key, to_display_key,
    to_slug, normalize_name, sort_arabic, compare_arabic,
    remove_tashkeel, normalize_alef, normalize_digits,
    is_arabic, arabic_ratio,
)

# مفتاح بحث للتخزين في قاعدة البيانات
to_search_key('مُحَمَّدٌ')       # → 'محمد'
to_search_key('فاطمة العلي')   # → 'فاطمة العلي'

# مفتاح متساهل: طبّقه على المفتاح المخزن المتساهل والاستعلام معاً
to_loose_search_key('فاطمة')   # → 'فاطمه'
to_loose_search_key('فاطمه')   # → 'فاطمه'
# ⚠️ لا تقارن هذا المفتاح بعمود to_search_key الصارم؛ استخدم عموداً متساهلاً.

# slug لـ URL
to_slug('مدينة الرياض')        # → 'مدينه-الرياض'

# ترتيب قائمة
sort_arabic(['يوسف', 'أحمد', 'محمد'])  # → ['أحمد', 'محمد', 'يوسف']

# تحويل الأرقام
normalize_digits('١٢٣', 'western')  # → '123'
normalize_digits('123', 'eastern')  # → '١٢٣'
normalize_digits('۱۲۳', 'eastern')  # → '١٢٣' (Persian is a source)

نمط قاعدة البيانات

خزّن النص الأصلي ومفتاحاً صارماً، وأضف عموداً متساهلاً اختيارياً. لا تغيّر search_key القائم.

ALTER TABLE students ADD COLUMN IF NOT EXISTS search_key_loose text;
UPDATE students SET search_key_loose = replace(search_key, 'ة', 'ه')
WHERE search_key IS NOT NULL AND search_key_loose IS NULL;
# حفظ: النص الأصلي + المفتاحان
cur.execute(
    "INSERT INTO students (name, search_key, search_key_loose) VALUES (%s, %s, %s)",
    (name, to_search_key(name), to_loose_search_key(name)),
)


def escape_like(value: str) -> str:
    return (
        value.replace("\\", "\\\\")
        .replace("%", "\\%")
        .replace("_", "\\_")
    )


# بحث متساهل حرفي: متساهل/متساهل — لا تستخدم search_key هنا
key = to_loose_search_key(query.strip())
if not key:
    rows = []
else:
    pattern = f"%{escape_like(key)}%"
    cur.execute(
        "SELECT id, name FROM students "
        "WHERE search_key_loose LIKE %s ESCAPE '\\'",
        (pattern,),
    )
    rows = cur.fetchall()
-- PostgreSQL: $1 هو النمط الكامل بعد escape_like، وليس المفتاح الخام
SELECT id, name FROM students
WHERE search_key_loose LIKE $1 ESCAPE '\';

text_pattern_ops يسرّع البادئات فقط. للاحتواء %query% استخدم pg_trgm. لا تدمج مدخل المستخدم في SQL نصياً. إذا كان الاستعلام فارغاً، لا ترسل %%؛ أعد نتيجة فارغة أو تجاوز الفلتر.

PostgREST/Supabase: اختبر R1 محلياً HTTP مباشر إلى PostgREST 14.10 + PostgreSQL 16، ووجد أن .like() يحترم فرار \ لـ % و_ و\. لم تُختبر استدعاءات Supabase SDK أو الخدمة المستضافة. المثال أعلاه يستخدم psycopg مباشرة؛ أعد اختبار إصدارك عند استخدام SDK.

الـ API

الدالة الوصف
to_search_key(text) مفتاح بحث للتخزين. يحافظ على ة.
to_loose_search_key(text) مثل to_search_key + ة→ه. للمفتاح المتساهل المخزن والاستعلام معاً.
to_display_key(text) يُزيل التطويل فقط. يحافظ على التشكيل.
to_slug(text) معرّف URL عربي آمن.
normalize_name(text) تطبيع الأسماء. يحافظ على ى و ة.
to_sort_key(text) مفتاح فرز.
sort_arabic(list) ترتيب مستقر بـ to_sort_key.
compare_arabic(a, b) مقارنة — يُعيد ‎-1 أو 0 أو 1.
remove_tashkeel(text) حذف جميع علامات الشكل.
remove_tatweel(text) حذف التطويل U+0640.
normalize_alef(text) أ إ آ ٱ → ا
normalize_hamza(text) ؤ ئ → ء
normalize_ya(text) ى ی → ي
normalize_ta_marbouta(text) ة → ه. يجب استدعاؤها صراحةً.
normalize_presentation_forms(text) أشكال العرض FB50–FEFF → يونيكود قياسي.
normalize_digits(text, to) تحويل الأرقام: 'western' أو 'eastern'.
is_arabic(text) True إذا كان النص يحتوي على أي حرف عربي.
arabic_ratio(text) نسبة الأحرف العربية (0.0–1.0).
SPEC_VERSION '1.0.1'

الترخيص

MIT — Devsamhan

Metadata

Release files for devsamhan-arabic-text 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for devsamhan-arabic-text 1.1.0
File Size Uploaded Provenance Actions
devsamhan_arabic_text-1.1.0.tar.gz 10.8 kB Without provenance Details for devsamhan_arabic_text-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for devsamhan-arabic-text 1.1.0
File Interpreter ABI Platform Actions
devsamhan_arabic_text-1.1.0-py3-none-any.whl Python 3 none any Details for devsamhan_arabic_text-1.1.0-py3-none-any.whl

Total release size: 19.7 kB

Release files / devsamhan_arabic_text-1.1.0.tar.gz

Download URL devsamhan_arabic_text-1.1.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
22f60fbaaceee8e649bc73e0b638abf48a54f9224d93572e890bc995df251d32
BLAKE2b-256 checksum
How to use checksums
8f6e59f328e0c5198664d7676f8bf5bfd54f45234a919aef6dec6943a230540a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / devsamhan_arabic_text-1.1.0-py3-none-any.whl

Download URL devsamhan_arabic_text-1.1.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39a97903d084e7ec3fa963e2eb426389e6dd19adc53cfbc6b891ab92086891bd
BLAKE2b-256 checksum
How to use checksums
1f42d88d4636a7aac3ca4aae8b91390f1415885c18dbf099f6b262d6d53451c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for devsamhan-arabic-text 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page