Mazenbs commited on
Commit
87cfaef
·
verified ·
1 Parent(s): 560009a

Update parser/assembler.py

Browse files
Files changed (1) hide show
  1. parser/assembler.py +39 -36
parser/assembler.py CHANGED
@@ -2,49 +2,56 @@ from typing import List, Dict
2
  from helpers.utils import normalize_digits, extract_article_number, is_article, is_section
3
 
4
 
5
- def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
6
  """
7
  استخراج عنوان القانون والمقدمة، مع الاحتفاظ بالنصوص غير المصنفة.
8
  """
9
  title = ""
10
- while texts:
11
- t = texts.pop(0).strip()
 
 
 
 
 
12
  if t.lower() != "html" and t != "":
13
  title = t
14
  break
15
 
16
- preamble_lines = []
17
- remaining_texts = []
18
-
19
- for t in texts:
20
  if is_section(t) or is_article(t):
21
- remaining_texts.append(t)
22
  else:
23
- preamble_lines.append(t)
24
 
25
- preamble = "\n".join(preamble_lines).strip()
26
- return title, preamble, remaining_texts
 
27
 
28
 
29
- def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
30
  """
31
  استخراج المواد مع الاحتفاظ بالنصوص التابعة لها.
32
  """
33
  articles = []
34
  current = None
35
 
36
- for t in texts:
37
- t = t.strip()
38
  if is_article(t):
39
  if current:
40
  current["text"] = current["text"].strip()
41
  articles.append(current)
42
- current = {"number": extract_article_number(t), "text": t}
43
  else:
44
  if current:
45
  current["text"] += "\n" + t
 
46
  else:
47
- current = {"number": None, "text": t}
 
48
 
49
  if current:
50
  current["text"] = current["text"].strip()
@@ -53,30 +60,25 @@ def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
53
  return articles
54
 
55
 
56
- def extract_sections(texts: List[str]) -> List[Dict]:
57
  """
58
  تقسيم النصوص إلى أقسام وفصول باستخدام الأنماط الذكية.
59
  """
60
  sections = []
61
  current = {"name": "", "texts": []}
62
 
63
- for t in texts:
64
- if not isinstance(t, str):
65
- continue
66
-
67
- t_norm = normalize_digits(t.strip())
68
 
69
- # هل السطر عنوان قسم أو فصل؟
70
- if is_section(t_norm):
71
  # لو يوجد قسم سابق → نحفظه
72
  if current["texts"] or current["name"]:
73
  sections.append(current)
74
 
75
  # نبدأ قسم/فصل جديد
76
- current = {"name": t_norm, "texts": []}
77
  else:
78
- # السطور التابعة للقسم الحالي
79
- current["texts"].append(t_norm)
80
 
81
  # إضافة آخر قسم
82
  if current["texts"] or current["name"]:
@@ -90,29 +92,29 @@ def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
90
  تحويل النصوص إلى هيكل قانوني منظم مع ضمان عدم فقد أي نص.
91
  """
92
  # النصوص الخام (منظفة مسبقًا في text_blocks.py)
93
- raw_texts = [b.get("text", "") for b in text_blocks if b.get("text")]
94
 
95
  # استخراج العنوان والمقدمة
96
- title, preamble, remaining_texts = extract_title_and_preamble(raw_texts)
97
 
98
  # استخراج الأقسام
99
- sections_raw = extract_sections(remaining_texts)
100
 
101
  sections = []
102
  for sec in sections_raw:
103
- raw_texts = sec["texts"]
104
 
105
- # المحتوى غير المواد
106
- content = "\n".join(t for t in raw_texts if not is_article(t)).strip()
107
 
108
  # استخراج المواد
109
- articles = extract_articles_from_texts(raw_texts)
110
 
111
  sections.append({
112
  "title": sec["name"],
113
  "content": content,
114
  "articles": [
115
- {"number": a["number"], "text": a["text"]}
116
  for a in articles
117
  ]
118
  })
@@ -123,6 +125,7 @@ def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
123
  "law": {
124
  "title": title,
125
  "preamble": preamble,
126
- "sections": sections
 
127
  }
128
  }
 
2
  from helpers.utils import normalize_digits, extract_article_number, is_article, is_section
3
 
4
 
5
+ def extract_title_and_preamble(text_blocks: List[Dict[str, str]]) -> (str, str, List[Dict[str, str]]):
6
  """
7
  استخراج عنوان القانون والمقدمة، مع الاحتفاظ بالنصوص غير المصنفة.
8
  """
9
  title = ""
10
+ preamble_blocks = []
11
+ remaining_blocks = []
12
+
13
+ # استخراج العنوان
14
+ while text_blocks:
15
+ block = text_blocks.pop(0)
16
+ t = block.get("text", "").strip()
17
  if t.lower() != "html" and t != "":
18
  title = t
19
  break
20
 
21
+ # استخراج المقدمة وبقية النصوص
22
+ for block in text_blocks:
23
+ t = block.get("text", "").strip()
 
24
  if is_section(t) or is_article(t):
25
+ remaining_blocks.append(block)
26
  else:
27
+ preamble_blocks.append(block)
28
 
29
+ # المقدمة تبقى كـ نصوص منفصلة لكن نعرضها مجمعة عند الإخراج
30
+ preamble = "\n".join([b["text"] for b in preamble_blocks]).strip()
31
+ return title, preamble, remaining_blocks
32
 
33
 
34
+ def extract_articles_from_blocks(blocks: List[Dict[str, str]]) -> List[Dict]:
35
  """
36
  استخراج المواد مع الاحتفاظ بالنصوص التابعة لها.
37
  """
38
  articles = []
39
  current = None
40
 
41
+ for block in blocks:
42
+ t = block.get("text", "").strip()
43
  if is_article(t):
44
  if current:
45
  current["text"] = current["text"].strip()
46
  articles.append(current)
47
+ current = {"number": extract_article_number(t), "text": t, "blocks": [block]}
48
  else:
49
  if current:
50
  current["text"] += "\n" + t
51
+ current["blocks"].append(block)
52
  else:
53
+ # نص غير مصنف نحفظه كمادة بدون رقم
54
+ current = {"number": None, "text": t, "blocks": [block]}
55
 
56
  if current:
57
  current["text"] = current["text"].strip()
 
60
  return articles
61
 
62
 
63
+ def extract_sections(blocks: List[Dict[str, str]]) -> List[Dict]:
64
  """
65
  تقسيم النصوص إلى أقسام وفصول باستخدام الأنماط الذكية.
66
  """
67
  sections = []
68
  current = {"name": "", "texts": []}
69
 
70
+ for block in blocks:
71
+ t = normalize_digits(block.get("text", "").strip())
 
 
 
72
 
73
+ if is_section(t):
 
74
  # لو يوجد قسم سابق → نحفظه
75
  if current["texts"] or current["name"]:
76
  sections.append(current)
77
 
78
  # نبدأ قسم/فصل جديد
79
+ current = {"name": t, "texts": []}
80
  else:
81
+ current["texts"].append(block)
 
82
 
83
  # إضافة آخر قسم
84
  if current["texts"] or current["name"]:
 
92
  تحويل النصوص إلى هيكل قانوني منظم مع ضمان عدم فقد أي نص.
93
  """
94
  # النصوص الخام (منظفة مسبقًا في text_blocks.py)
95
+ raw_blocks = [b for b in text_blocks if b.get("text")]
96
 
97
  # استخراج العنوان والمقدمة
98
+ title, preamble, remaining_blocks = extract_title_and_preamble(raw_blocks)
99
 
100
  # استخراج الأقسام
101
+ sections_raw = extract_sections(remaining_blocks)
102
 
103
  sections = []
104
  for sec in sections_raw:
105
+ raw_blocks = sec["texts"]
106
 
107
+ # المحتوى غير المواد (��بقى كـ نصوص منفصلة لكن نعرضه مجمّعًا)
108
+ content = "\n".join([b["text"] for b in raw_blocks if not is_article(b.get("text", ""))]).strip()
109
 
110
  # استخراج المواد
111
+ articles = extract_articles_from_blocks(raw_blocks)
112
 
113
  sections.append({
114
  "title": sec["name"],
115
  "content": content,
116
  "articles": [
117
+ {"number": a["number"], "text": a["text"], "blocks": a["blocks"]}
118
  for a in articles
119
  ]
120
  })
 
125
  "law": {
126
  "title": title,
127
  "preamble": preamble,
128
+ "sections": sections,
129
+ "all_blocks": text_blocks # ضمان أن كل النصوص محفوظة
130
  }
131
  }