manga01 / modules /utils.py
YOUKKASS's picture
Create modules/utils.py
64648bb verified
import re
def clean_arabic_translation(text: str) -> str:
"""
تنظيف وتحسين الترجمة العربية
"""
if not text or not isinstance(text, str):
return text
# إزالة الرموز غير المرغوب فيها
text = re.sub(r'[^\w\s\u0600-\u06FF،؛؟.!]', '', text)
# تصحيح الأخطاء الشائعة
correction_rules = {
" هو ": " هي ",
" قال ": " قالت ",
" هذا ": " هذه ",
" لكن ": " لكنّ ",
"إلي": "إلى",
"اللّه": "الله",
"ان ": "أن ",
"هي ": "هي ",
"ة": "ه", # تحويل التاء المربوطة إلى هاء في بعض السياقات
}
for wrong, right in correction_rules.items():
text = text.replace(wrong, right)
# تحويل الترقيم إلى العربية
punctuation_map = {
"?": "؟",
",": "،",
";": "؛",
":": ":",
}
for eng_punct, ar_punct in punctuation_map.items():
text = text.replace(eng_punct, ar_punct)
# إزالة المسافات الزائدة وتحسين النص
text = re.sub(r'\s+', ' ', text).strip()
# ضمان أن النص يبدأ بحرف مناسب وينتهي بترقيم مناسب
if text and text[-1] not in [".", "؟", "!", "،"]:
text += "."
return text