Skip to main content

An Arabic Preprocessing Toolkit for Pretraining of LLM

Project description

Tahdheeb (تهذیب)

Overview

Tahdheeb (تهذیب) is an Arabic Preprocessing Library developed at Qatar Computing Research. This library provides tools for preprocessing Arabic text, including normalization, diacritics handling, number normalization, punctuation correction, and more.

Develpers

  • Ehsaneddin Asgari
  • Yassine Elkheir

Features

  • Normalization: Standardizes Arabic text by normalizing character variations and numbers.
  • Diacritics Handling: Removes or processes Arabic diacritics.
  • Punctuation Correction: Corrects punctuation usage in Arabic text.
  • Determine Arabic Ratio: Output the arabic ration.

Installation

You can install Tahdheeb using pip:

pip install src

Example Usage

The following example demonstrates how to use the Tahdheeb library for preprocessing Arabic text, including fixing punctuation issues, improving text structure, and normalizing the text.

Code Example

from src.preprocessing.text_content import ArabicTextProcess
from src.preprocessing.text_verification import ArabicTextVerify
from src.preprocessing.text_structure import ArabicStructure

text = 'الـــــــــسلام    عليكم . كيـــف    حالك ؟ أتـــمنى أن تكون بــــــخير .أ نحنو  هنا للمســـــاعدة ...هل  تريــــد شيء مـــا؟  الرجـــــــاء    إبلاغن ا بــــأي وقــــت! في ٢دقيقه شكرا ، جزيــــلا Thanks!'

# Initialize the classes
ATB = ArabicStructure()
ASP = ArabicTextProcess()
ATP = ArabicTextVerify()

# Check the ratio of Arabic text
print('Ratio of Arabic text:', ATP.check_arabic_ratio(text))

print('\nText before structure improvement:\n', text, '\n-----------\n')

# Preprocess the text to fix structure and remove English text
text = ATB.preprocess(text, remove_english=True)
print('\nText after structure improvement:\n', text, '\n-----------\n')

# Normalize the text (basic normalization)
text = ASP.get_arabic_normal(text, extensive_normalization=False)
print('\nText after normalizing:\n', text, '\n-----------\n')

# Normalize the text (extensive normalization)
text = ASP.get_arabic_normal(text, extensive_normalization=True)
print('\nText after extensive normalizing:\n', text, '\n-----------\n')

text = 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ'

print('\ntext before diac removal:\n', text, '\n-----------\n')
text = ASP.get_arabic_normal(text,  extensive_normalization=False, diacritics_removal=True)
print('\ntext after diac removal:\n', text, '\n-----------\n')

Output

Ratio of Arabic text: 0.7107843137254902

text before struct improving:
 الـــــــــسلام    عليكم . كيـــف    حالك ؟ أتـــمنى أن تكون بــــــخير .أ نحنو  هنا للمســـــاعدة ...هل  تريــــد شيء مـــا؟  الرجـــــــاء    إبلاغن ا بــــأي وقــــت! في ٢دقيقه شكرا ، جزيــــلا Thanks! 
-----------


text after struct improving:
 الـــــــــسلام عليكم. كيـــف حالك؟ أتـــمنى أن تكون بــــــخير. أ نحنو هنا للمســـــاعدة... هل تريــــد شيء مـــا؟ الرجـــــــاء إبلاغن ا بــــأي وقــــت! في ٢ دقيقه شكرا، جزيــــلا! 
-----------


text after normalizing:
 السلام عليكم. كيف حالك؟ أتمنى أن تكون بخير. أ نحنو هنا للمساعدة... هل تريد شيء ما؟ الرجاء إبلاغن ا بأي وقت! في 2 دقيقه شكرا، جزيلا! 
-----------


text after extensive normalizing:
 السلام علىكم. كىف حالك؟ اتمنى ان تكون بخىر. ا نحنو هنا للمساعده... هل ترىد شىء ما؟ الرجاء ابلاغن ا باى وقت! فى 2 دقىقه شكرا، جزىلا! 
-----------

text before diac removal:
 بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ 
-----------


text after diac removal:
 بسم الله الرحمن الرحيم 
-----------

Explanation

Import Libraries

from src.preprocessing.text_content import ArabicTextProcess
from src.preprocessing.text_verification import ArabicTextVerify
from src.preprocessing.text_structure import ArabicStructure

Original Text

The sample text contains various errors such as character repetition, incorrect punctuation, and mixed language content.

text = 'الـــــــــسلام    عليكم . كيـــف    حالك ؟ أتـــمنى أن تكون بــــــخير .أ نحنو  هنا للمســـــاعدة ...هل  تريــــد شيء مـــا؟  الرجـــــــاء    إبلاغن ا بــــأي وقــــت! في ٢دقيقه شكرا ، جزيــــلا Thanks!'

Initialize the Classes

Initialize the classes for structure improvement, text processing, and text verification.

ATB = ArabicStructure()
ASP = ArabicTextProcess()
ATP = ArabicTextVerify()

Check the Ratio of Arabic Text

Verify how much of the text is in Arabic.

print('Ratio of Arabic text:', ATP.check_arabic_ratio(text))

Before Structure Improvement

Print the original text before any processing.

print('\nText before structure improvement:\n', text, '\n-----------\n')

Structure Improvement

Preprocess the text to fix punctuation issues and remove English text.

text = ATB.preprocess(text, remove_english=True)
print('\nText after structure improvement:\n', text, '\n-----------\n')

Basic Normalization

Normalize the text with basic normalization.

text = ASP.get_arabic_normal(text, extensive_normalization=False)
print('\nText after normalizing:\n', text, '\n-----------\n')

Extensive Normalization

Normalize the text with extensive normalization.

text = ASP.get_arabic_normal(text, extensive_normalization=True)
print('\nText after extensive normalizing:\n', text, '\n-----------\n')

Output

This example will output the following information at each step:

  1. The ratio of Arabic text in the original input.
  2. The original text before any processing.
  3. The text after fixing structural issues and removing English text.
  4. The text after basic normalization.
  5. The text after extensive normalization.

This step-by-step process ensures that the Arabic text is cleaned, normalized, and ready for further processing or analysis.

Notes

Diacretics

Unicode Diacritic Name (English) Name (Arabic) Description
\u064b ً Tanwin Fathah تَنوين الفتح Indicates a nun sound after the vowel (an).
\u064c ٌ Tanwin Dammah تَنوين الضم Indicates a nun sound after the vowel (un).
\u064d ٍ Tanwin Kasrah تَنوين الكسر Indicates a nun sound after the vowel (in).
\u064e َ Fathah فتحة Represents a short "a" sound.
\u064f ُ Dammah ضمة Represents a short "u" sound.
\u0650 ِ Kasrah كسرة Represents a short "i" sound.
\u0651 ّ Shaddah شدة Indicates gemination or doubling of a consonant.
\u0652 ْ Sukun سكون Indicates the absence of a vowel.
\u0653 ٓ Maddah مدّة Indicates a long vowel sound.
\u0670 ٰ Superscript Alif ألف خنجرية Indicates a short "a" sound, mainly in Quranic text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Tahdheeb-0.1.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Tahdheeb-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file Tahdheeb-0.1.1.tar.gz.

File metadata

  • Download URL: Tahdheeb-0.1.1.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.5

File hashes

Hashes for Tahdheeb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e373cec0282d4971ca26fa2e491e14e3d7993f6954b4a63c88171f22e87f984b
MD5 e5549422e971bd92315e8018ff178aa8
BLAKE2b-256 dbf47fe3e705e72310dd6833642a760248d67413707075b8eeef60b8b30da8cd

See more details on using hashes here.

File details

Details for the file Tahdheeb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: Tahdheeb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.5

File hashes

Hashes for Tahdheeb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5253cd8278a0aa56358410200e5f59fbc62f4b3de03ad1747f6d1bb93f441cd3
MD5 ea268e620b6c3b705dacd2cc4368a4ae
BLAKE2b-256 9fdce8d52373b627f8c03401918de905ac458ac7671fe5b3409582ec498d3790

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page