dialects-ai-foundry / text_normalizer.py
Termuxphone's picture
Update v2 dataset: 319 train samples (26m) + ecommerce_dz batch + tuned LoRA config
6cc8793 verified
Raw History Blame Contribute Delete
2.37 kB
#!/usr/bin/env python3
import re
import json
import glob
import os
def normalize_darja(text: str) -> str:
if not text:
return ""
# 1. إزالة الحركات والتشكيل والتطويل (التكشيدة)
text = re.sub(r'[\u064B-\u065F\u0670\u0640]', '', text)
# 2. توحيد أشكال الألف
text = re.sub(r'[إأآٱ]', 'ا', text)
# 3. توحيد الألف المقصورة إلى ياء
text = re.sub(r'ى', 'ي', text)
# 4. توحيد التاء المربوطة
text = re.sub(r'ة', 'ه', text)
# 5. اختزال تكرار الحروف المتتالية لأكثر من مرتين (مثل: شبااااب -> شباب)
text = re.sub(r'(.)\1{2,}', r'\1', text)
# 6. إزالة أي محارف غير عربية والمسافات الزائدة (مع الحفاظ على الفراغات الأساسية)
text = re.sub(r'[^\u0600-\u06FF\s]', ' ', text)
text = re.sub(r'\s+', ' ', text).strip()
return text
def process_file(file_path: str, text_key: str):
if not os.path.exists(file_path):
return
updated_records = []
changes = 0
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
if not line.strip():
continue
row = json.loads(line)
orig_text = row.get(text_key, "")
norm_text = normalize_darja(orig_text)
if orig_text != norm_text:
changes += 1
row[text_key] = norm_text
updated_records.append(row)
with open(file_path, 'w', encoding='utf-8') as f:
for row in updated_records:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
print(f"[+] {file_path}: تم تحديث {changes} سطر من أصل {len(updated_records)}")
if __name__ == "__main__":
print("[*] بدء عملية تطبيع وتوحيد النصوص الإملائية...")
# تطبيع ملفات التقسيم splits/
for split in ["train", "validation", "test"]:
process_file(f"splits/{split}.jsonl", text_key="sentence")
# تطبيع ملف سكيكدة
process_file("data/skikda/manifest.jsonl", text_key="transcript")
print("[+] اكتمل تطبيع النصوص بنجاح.")