Spaces:
Sleeping
Sleeping
Update helpers/utils.py
Browse files- helpers/utils.py +36 -32
helpers/utils.py
CHANGED
|
@@ -1,38 +1,28 @@
|
|
| 1 |
import re
|
| 2 |
from helpers.cleaner import clean_text
|
| 3 |
|
| 4 |
-
# ----------- الأنماط -----------
|
|
|
|
|
|
|
| 5 |
ARTICLE_PATTERN = re.compile(
|
| 6 |
r"^\s*ماد[ةه]\s*[\(\s]*([0-9]+)[\)\s]*",
|
| 7 |
-
re.IGNORECASE
|
| 8 |
)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
r'^\s*(الباب|الفصل|القسم)\s+([^\n]+)',
|
| 13 |
re.IGNORECASE | re.UNICODE
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
LAW_ISSUE_PATTERN = re.compile(
|
| 18 |
-
r'أصدرنا القانون\s+(.*?)(?=\s{2,}|$)',
|
| 19 |
re.IGNORECASE | re.UNICODE
|
| 20 |
)
|
| 21 |
-
def is_article(text: str) -> bool:
|
| 22 |
-
"""هل السطر عبارة عن مادة قانونية؟"""
|
| 23 |
-
if not isinstance(text, str):
|
| 24 |
-
return False
|
| 25 |
-
text_norm = normalize_digits(text)
|
| 26 |
-
return bool(ARTICLE_PATTERN.match(text_norm))
|
| 27 |
-
|
| 28 |
-
def is_section(text: str) -> bool:
|
| 29 |
-
"""هل السطر عنوان قسم (باب / فصل / قسم)؟"""
|
| 30 |
-
if not isinstance(text, str):
|
| 31 |
-
return False
|
| 32 |
-
return bool(SECTION_PATTERN.match(text))
|
| 33 |
-
|
| 34 |
|
| 35 |
# ----------- تحويل الأرقام الهندية إلى عربية -----------
|
|
|
|
| 36 |
ARABIC_INDIC_DIGITS = str.maketrans("٠١٢٣٤٥٦٧٨٩", "0123456789")
|
| 37 |
|
| 38 |
def normalize_digits(s: str) -> str:
|
|
@@ -40,48 +30,62 @@ def normalize_digits(s: str) -> str:
|
|
| 40 |
return ""
|
| 41 |
return s.translate(ARABIC_INDIC_DIGITS)
|
| 42 |
|
| 43 |
-
# ----------- تنظيف
|
|
|
|
| 44 |
def clean_text_block(text: str) -> str:
|
| 45 |
if not isinstance(text, str):
|
| 46 |
return ""
|
| 47 |
return clean_text(normalize_digits(text.strip()))
|
| 48 |
|
|
|
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# ----------- تحديد نوع السطر -----------
|
| 53 |
def detect_line_type(text: str) -> str:
|
| 54 |
"""
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
"""
|
| 59 |
if not isinstance(text, str) or not text.strip():
|
| 60 |
return "text"
|
| 61 |
|
| 62 |
text_norm = normalize_digits(text)
|
| 63 |
|
| 64 |
-
if
|
| 65 |
return "section"
|
| 66 |
if ARTICLE_PATTERN.match(text_norm):
|
| 67 |
return "article"
|
| 68 |
-
if LAW_ISSUE_PATTERN.search(text_norm):
|
| 69 |
return "section"
|
| 70 |
return "text"
|
| 71 |
|
| 72 |
-
# ----------- استخراج تفاصيل القانون
|
|
|
|
| 73 |
def extract_law_detail(text: str) -> str | None:
|
| 74 |
-
"""
|
| 75 |
-
إرجاع التفاصيل التي تلي «أصدرنا القانون» مباشرة حتى نهاية السطر.
|
| 76 |
-
ترجع None إذا لم توجد العبارة.
|
| 77 |
-
"""
|
| 78 |
if not isinstance(text, str):
|
| 79 |
return None
|
| 80 |
m = LAW_ISSUE_PATTERN.search(text)
|
| 81 |
return m.group(1).strip() if m else None
|
| 82 |
|
| 83 |
# ----------- استخراج رقم المادة -----------
|
|
|
|
| 84 |
def extract_article_number(text: str) -> int | None:
|
|
|
|
| 85 |
if not isinstance(text, str):
|
| 86 |
return None
|
| 87 |
text_norm = normalize_digits(text)
|
|
@@ -90,4 +94,4 @@ def extract_article_number(text: str) -> int | None:
|
|
| 90 |
digits = re.match(r'(\d+)', m.group(1))
|
| 91 |
if digits:
|
| 92 |
return int(digits.group(1))
|
| 93 |
-
return None
|
|
|
|
| 1 |
import re
|
| 2 |
from helpers.cleaner import clean_text
|
| 3 |
|
| 4 |
+
# ----------- الأنماط (Regex Patterns) -----------
|
| 5 |
+
|
| 6 |
+
# المواد القانونية (مادة رقم ...)
|
| 7 |
ARTICLE_PATTERN = re.compile(
|
| 8 |
r"^\s*ماد[ةه]\s*[\(\s]*([0-9]+)[\)\s]*",
|
| 9 |
+
re.IGNORECASE | re.UNICODE
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# الأقسام التقليدية (الباب / الفصل / القسم)
|
| 13 |
+
SECTION_PATTERN = re.compile(
|
| 14 |
r'^\s*(الباب|الفصل|القسم)\s+([^\n]+)',
|
| 15 |
re.IGNORECASE | re.UNICODE
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# عبارة "أصدرنا القانون ..." داخل النص
|
| 19 |
LAW_ISSUE_PATTERN = re.compile(
|
| 20 |
+
r'أصدرنا القانون\s+(.*?)(?=\s{2,}|$)',
|
| 21 |
re.IGNORECASE | re.UNICODE
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# ----------- تحويل الأرقام الهندية إلى عربية -----------
|
| 25 |
+
|
| 26 |
ARABIC_INDIC_DIGITS = str.maketrans("٠١٢٣٤٥٦٧٨٩", "0123456789")
|
| 27 |
|
| 28 |
def normalize_digits(s: str) -> str:
|
|
|
|
| 30 |
return ""
|
| 31 |
return s.translate(ARABIC_INDIC_DIGITS)
|
| 32 |
|
| 33 |
+
# ----------- تنظيف النصوص -----------
|
| 34 |
+
|
| 35 |
def clean_text_block(text: str) -> str:
|
| 36 |
if not isinstance(text, str):
|
| 37 |
return ""
|
| 38 |
return clean_text(normalize_digits(text.strip()))
|
| 39 |
|
| 40 |
+
# ----------- دوال التعرف على نوع النص -----------
|
| 41 |
|
| 42 |
+
def is_article(text: str) -> bool:
|
| 43 |
+
"""هل السطر عبارة عن مادة قانونية؟"""
|
| 44 |
+
if not isinstance(text, str):
|
| 45 |
+
return False
|
| 46 |
+
text_norm = normalize_digits(text)
|
| 47 |
+
return bool(ARTICLE_PATTERN.match(text_norm))
|
| 48 |
|
| 49 |
+
def is_section(text: str) -> bool:
|
| 50 |
+
"""هل السطر عنوان قسم (باب / فصل / قسم)؟"""
|
| 51 |
+
if not isinstance(text, str):
|
| 52 |
+
return False
|
| 53 |
+
text_norm = normalize_digits(text)
|
| 54 |
+
return bool(SECTION_PATTERN.match(text_norm))
|
| 55 |
|
|
|
|
| 56 |
def detect_line_type(text: str) -> str:
|
| 57 |
"""
|
| 58 |
+
تحديد نوع السطر:
|
| 59 |
+
- section → بداية قسم (باب/فصل/قسم) أو يحتوي «أصدرنا القانون»
|
| 60 |
+
- article → بداية مادة
|
| 61 |
+
- text → نص عادي
|
| 62 |
"""
|
| 63 |
if not isinstance(text, str) or not text.strip():
|
| 64 |
return "text"
|
| 65 |
|
| 66 |
text_norm = normalize_digits(text)
|
| 67 |
|
| 68 |
+
if SECTION_PATTERN.match(text_norm):
|
| 69 |
return "section"
|
| 70 |
if ARTICLE_PATTERN.match(text_norm):
|
| 71 |
return "article"
|
| 72 |
+
if LAW_ISSUE_PATTERN.search(text_norm):
|
| 73 |
return "section"
|
| 74 |
return "text"
|
| 75 |
|
| 76 |
+
# ----------- استخراج تفاصيل القانون -----------
|
| 77 |
+
|
| 78 |
def extract_law_detail(text: str) -> str | None:
|
| 79 |
+
"""إرجاع التفاصيل التي تلي «أصدرنا القانون» مباشرة حتى نهاية السطر."""
|
|
|
|
|
|
|
|
|
|
| 80 |
if not isinstance(text, str):
|
| 81 |
return None
|
| 82 |
m = LAW_ISSUE_PATTERN.search(text)
|
| 83 |
return m.group(1).strip() if m else None
|
| 84 |
|
| 85 |
# ----------- استخراج رقم المادة -----------
|
| 86 |
+
|
| 87 |
def extract_article_number(text: str) -> int | None:
|
| 88 |
+
"""استخراج رقم المادة من النص إذا وجد."""
|
| 89 |
if not isinstance(text, str):
|
| 90 |
return None
|
| 91 |
text_norm = normalize_digits(text)
|
|
|
|
| 94 |
digits = re.match(r'(\d+)', m.group(1))
|
| 95 |
if digits:
|
| 96 |
return int(digits.group(1))
|
| 97 |
+
return None
|