File size: 2,366 Bytes
6cc8793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/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("[+] اكتمل تطبيع النصوص بنجاح.")