Mazenbs commited on
Commit
c2e3785
·
verified ·
1 Parent(s): 049a831

Update helpers/utils.py

Browse files
Files changed (1) hide show
  1. helpers/utils.py +21 -3
helpers/utils.py CHANGED
@@ -7,12 +7,16 @@ from helpers.cleaner import clean_text
7
  ARABIC_INDIC_DIGITS = str.maketrans("٠١٢٣٤٥٦٧٨٩", "0123456789")
8
 
9
  def normalize_digits(s: str) -> str:
 
 
 
10
  return s.translate(ARABIC_INDIC_DIGITS)
11
 
12
  # ---------------------------------------
13
  # دالة تنظيف النص + تحويل الأرقام
14
  # ---------------------------------------
15
  def clean_text_block(text: str) -> str:
 
16
  if not isinstance(text, str):
17
  return ""
18
  return clean_text(normalize_digits(text.strip()))
@@ -21,20 +25,28 @@ def clean_text_block(text: str) -> str:
21
  # أنماط المواد والأقسام
22
  # ---------------------------------------
23
 
 
24
  ARTICLE_PATTERN = re.compile(
25
  r"^\s*(?:ال?ماد[ةه])\s*[\(]?\s*([0-9٠-٩]+(?:\s*مكرر)?)\s*[\)]?\s*(.*)$",
26
  re.IGNORECASE
27
  )
28
 
 
29
  SECTION_PATTERN = re.compile(
30
  r"^\s*(الباب|الفصل|القسم)\s+([^\n]+)",
31
  re.IGNORECASE
32
  )
33
 
34
  # ---------------------------------------
35
- # دالة واحدة لتحديد نوع السطر
36
  # ---------------------------------------
37
  def detect_line_type(text: str) -> str:
 
 
 
 
 
 
38
  if not isinstance(text, str) or not text.strip():
39
  return "text"
40
 
@@ -47,9 +59,15 @@ def detect_line_type(text: str) -> str:
47
  else:
48
  return "text"
49
 
 
 
 
50
  def extract_article_number(text: str) -> int | None:
51
  """
52
- استخراج رقم المادة كـ int، حتى لو كانت مكتوبة بصيغة "مكرر".
 
 
 
53
  """
54
  if not isinstance(text, str):
55
  return None
@@ -58,7 +76,7 @@ def extract_article_number(text: str) -> int | None:
58
  m = ARTICLE_PATTERN.match(text_norm)
59
  if m:
60
  number_str = m.group(1)
61
- # إزالة أي كلمة "مكرر" و المسافات
62
  number_only = re.match(r'(\d+)', number_str)
63
  if number_only:
64
  return int(number_only.group(1))
 
7
  ARABIC_INDIC_DIGITS = str.maketrans("٠١٢٣٤٥٦٧٨٩", "0123456789")
8
 
9
  def normalize_digits(s: str) -> str:
10
+ """تحويل الأرقام الهندية إلى عربية"""
11
+ if not isinstance(s, str):
12
+ return ""
13
  return s.translate(ARABIC_INDIC_DIGITS)
14
 
15
  # ---------------------------------------
16
  # دالة تنظيف النص + تحويل الأرقام
17
  # ---------------------------------------
18
  def clean_text_block(text: str) -> str:
19
+ """تنظيف النص وتحويل الأرقام للهندية → عربية"""
20
  if not isinstance(text, str):
21
  return ""
22
  return clean_text(normalize_digits(text.strip()))
 
25
  # أنماط المواد والأقسام
26
  # ---------------------------------------
27
 
28
+ # نمط المادة: يدعم "مادة" و"ماده" و"المادة" و"المادّة" مع رقم وربما "مكرر"
29
  ARTICLE_PATTERN = re.compile(
30
  r"^\s*(?:ال?ماد[ةه])\s*[\(]?\s*([0-9٠-٩]+(?:\s*مكرر)?)\s*[\)]?\s*(.*)$",
31
  re.IGNORECASE
32
  )
33
 
34
+ # نمط القسم: الباب أو الفصل أو القسم
35
  SECTION_PATTERN = re.compile(
36
  r"^\s*(الباب|الفصل|القسم)\s+([^\n]+)",
37
  re.IGNORECASE
38
  )
39
 
40
  # ---------------------------------------
41
+ # دالة تحديد نوع السطر
42
  # ---------------------------------------
43
  def detect_line_type(text: str) -> str:
44
+ """
45
+ تحديد نوع السطر:
46
+ - 'section' → بداية قسم (باب/فصل/قسم)
47
+ - 'article' → بداية مادة
48
+ - 'text' → نص عادي
49
+ """
50
  if not isinstance(text, str) or not text.strip():
51
  return "text"
52
 
 
59
  else:
60
  return "text"
61
 
62
+ # ---------------------------------------
63
+ # استخراج رقم المادة كـ int
64
+ # ---------------------------------------
65
  def extract_article_number(text: str) -> int | None:
66
  """
67
+ استخراج رقم المادة من النص:
68
+ - يحول الرقم للهندية → عربية
69
+ - يتجاهل كلمة "مكرر"
70
+ - يرجع int أو None
71
  """
72
  if not isinstance(text, str):
73
  return None
 
76
  m = ARTICLE_PATTERN.match(text_norm)
77
  if m:
78
  number_str = m.group(1)
79
+ # إزالة أي كلمة "مكرر" والمسافات
80
  number_only = re.match(r'(\d+)', number_str)
81
  if number_only:
82
  return int(number_only.group(1))