Mazenbs commited on
Commit
6b2daa6
·
verified ·
1 Parent(s): 3a15806

Create utils.py

Browse files
Files changed (1) hide show
  1. helpers/utils.py +51 -0
helpers/utils.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from helpers.cleaner import clean_text
3
+
4
+ ARABIC_INDIC_DIGITS = str.maketrans("٠١٢٣٤٥٦٧٨٩", "0123456789")
5
+
6
+ def normalize_digits(s: str) -> str:
7
+ return s.translate(ARABIC_INDIC_DIGITS)
8
+
9
+ # ---------------------------------------
10
+ # تنظيف النصوص + تحويل الأرقام
11
+ # ---------------------------------------
12
+ def clean_text_block(text: str) -> str:
13
+ if not isinstance(text, str):
14
+ return ""
15
+ return clean_text(normalize_digits(text.strip()))
16
+
17
+ # ---------------------------------------
18
+ # التعرف على المواد القانونية (يشمل "مكرر" وحروف)
19
+ # ---------------------------------------
20
+ ARTICLE_PATTERN = re.compile(
21
+ r"^\s*(?:المادة|مادة)\s*[\(\s]*([0-9٠-٩]+(?:\s*مكرر)?)\s*[\)\s]*([أ-ي\-]*)",
22
+ re.IGNORECASE
23
+ )
24
+
25
+ def is_article(text: str) -> bool:
26
+ if not isinstance(text, str):
27
+ return False
28
+ text_norm = normalize_digits(text)
29
+ return bool(ARTICLE_PATTERN.match(text_norm))
30
+
31
+ def extract_article_number(text: str):
32
+ if not isinstance(text, str):
33
+ return None
34
+ text_norm = normalize_digits(text)
35
+ m = ARTICLE_PATTERN.match(text_norm)
36
+ if m:
37
+ return m.group(1).replace(" ", "") # إزالة المسافات في "مكرر"
38
+ return None
39
+
40
+ # ---------------------------------------
41
+ # التعرف على الأقسام (الباب / الفصل / القسم)
42
+ # ---------------------------------------
43
+ SECTION_PATTERN = re.compile(
44
+ r"^\s*(الباب|الفصل|القسم)\s+([^\n]+)",
45
+ re.IGNORECASE
46
+ )
47
+
48
+ def is_section(text: str) -> bool:
49
+ if not isinstance(text, str):
50
+ return False
51
+ return bool(SECTION_PATTERN.match(text))