Mazenbs commited on
Commit
50eda68
·
verified ·
1 Parent(s): e1d1219

Update parser/assembler.py

Browse files
Files changed (1) hide show
  1. parser/assembler.py +71 -89
parser/assembler.py CHANGED
@@ -1,116 +1,98 @@
1
  from typing import List, Dict
2
  from .section_extractor import extract_sections
3
  from helpers.cleaner import clean_text
4
- from helpers.utils import normalize_digits, is_article, extract_article_number
5
 
6
 
7
- def parse_law_from_texts(text_blocks: List[Dict[str, str]]):
 
 
 
 
 
8
  """
9
- تحويل نصوص القانون المستخرجة إلى هيكل JSON متكامل مع تنظيف النصوص
10
- واستخراج المواد بشكل صحيح باستخدام الأنماط الحديثة من utils.
11
  """
12
-
13
- # -----------------------------------
14
- # 1) تنظيف النصوص الخام + تحويل الأرقام
15
- # -----------------------------------
16
- pure_texts = [
17
- clean_text(normalize_digits(block.get("text", "").strip()))
18
- for block in text_blocks
19
- if block.get("text")
20
- ]
21
-
22
- # -----------------------------------
23
- # 2) استخراج العنوان
24
- # -----------------------------------
25
  title = ""
26
- preamble_lines = []
27
-
28
- while pure_texts:
29
- candidate = pure_texts.pop(0).strip()
30
- if candidate.lower() != "html":
31
- title = candidate
32
  break
33
 
34
- # -----------------------------------
35
- # 3) تجميع المقدمة قبل أقسام (باب / فصل / قسم)
36
- # -----------------------------------
37
- while pure_texts and not any(
38
- keyword in pure_texts[0]
39
- for keyword in ["الباب", "الفصل", "القسم"]
40
- ):
41
- preamble_lines.append(pure_texts.pop(0))
42
 
43
  preamble = "\n".join(preamble_lines)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- # -----------------------------------
46
- # 4) استخراج الأقسام الخام
47
- # -----------------------------------
48
- sections_raw = extract_sections(pure_texts)
49
 
50
- # -----------------------------------
51
- # 5) إعادة صياغة الأقسام + استخراج المواد الحديثة
52
- # -----------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  sections = []
 
 
 
 
 
 
 
 
54
 
55
- for s in sections_raw:
56
- raw_texts = s["texts"]
57
-
58
- # -------- المحتوى غير المادة --------
59
- content_lines = [t for t in raw_texts if not is_article(t)]
60
- content = "\n".join(content_lines).strip()
61
-
62
- # -------- استخراج المواد --------
63
- articles = []
64
- current_article = None
65
-
66
- for t in raw_texts:
67
- t = t.strip()
68
-
69
- if is_article(t):
70
- # إضافة المادة السابقة إن وجدت
71
- if current_article:
72
- current_article["text"] = current_article["text"].strip()
73
- articles.append(current_article)
74
-
75
- # بدء مادة جديدة
76
- current_article = {
77
- "number": extract_article_number(t),
78
- "text": t,
79
- }
80
- else:
81
- # إلحاق النص بالمادة الحالية
82
- if current_article:
83
- current_article["text"] += "\n" + t
84
-
85
- # إضافة آخر مادة
86
- if current_article:
87
- current_article["text"] = current_article["text"].strip()
88
- articles.append(current_article)
89
-
90
- # -------- تنظيف بيانات القسم --------
91
- clean_section = {
92
- "title": clean_text(normalize_digits(s["name"])),
93
- "content": clean_text(normalize_digits(content)),
94
  "articles": [
95
- {
96
- "number": a["number"],
97
- "text": clean_text(normalize_digits(a["text"]))
98
- }
99
  for a in articles
100
  ]
101
- }
102
-
103
- sections.append(clean_section)
104
 
105
- # -----------------------------------
106
- # 6) إرجاع المستند القانوني الكامل
107
- # -----------------------------------
108
  return {
109
  "message": "تم التحليل بنجاح",
110
  "saved_to_db": False,
111
  "law": {
112
- "title": clean_text(normalize_digits(title)),
113
- "preamble": clean_text(normalize_digits(preamble)),
114
  "sections": sections
115
  }
116
  }
 
1
  from typing import List, Dict
2
  from .section_extractor import extract_sections
3
  from helpers.cleaner import clean_text
4
+ from helpers.utils import normalize_digits, is_article, extract_article_number, is_section
5
 
6
 
7
+ def clean_text_block(text: str) -> str:
8
+ """تنظيف النص وتحويل الأرقام الهندية إلى عربية."""
9
+ return clean_text(normalize_digits(text.strip()))
10
+
11
+
12
+ def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
13
  """
14
+ استخراج عنوان القانون والمقدمة، وإرجاع بقية النصوص بعد المقدمة.
 
15
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  title = ""
17
+ while texts:
18
+ t = texts.pop(0)
19
+ if t.lower() != "html":
20
+ title = t
 
 
21
  break
22
 
23
+ preamble_lines = []
24
+ while texts and not any(is_section(k) for k in [texts[0]]):
25
+ preamble_lines.append(texts.pop(0))
 
 
 
 
 
26
 
27
  preamble = "\n".join(preamble_lines)
28
+ return title, preamble, texts
29
+
30
+
31
+ def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
32
+ """استخراج المواد من قائمة نصوص القسم باستخدام is_article و extract_article_number."""
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
+
47
+ if current:
48
+ current["text"] = current["text"].strip()
49
+ articles.append(current)
50
+
51
+ return articles
52
 
 
 
 
 
53
 
54
+ def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
55
+ """
56
+ تحويل نصوص القانون إلى هيكل JSON مع تنظيف واستخراج مواد وأقسام
57
+ بالاعتماد على دوال utils.py.
58
+ """
59
+
60
+ # 1) تنظيف النصوص الخام
61
+ pure_texts = [clean_text_block(b.get("text", "")) for b in text_blocks if b.get("text")]
62
+
63
+ # 2) استخراج العنوان والمقدمة
64
+ title, preamble, remaining_texts = extract_title_and_preamble(pure_texts)
65
+
66
+ # 3) استخراج الأقسام الخام
67
+ sections_raw = extract_sections(remaining_texts)
68
+
69
+ # 4) بناء الأقسام مع المواد
70
  sections = []
71
+ for sec in sections_raw:
72
+ raw_texts = sec["texts"]
73
+
74
+ # المحتوى غير المواد
75
+ content = "\n".join(t for t in raw_texts if not is_article(t)).strip()
76
+
77
+ # استخراج المواد
78
+ articles = extract_articles_from_texts(raw_texts)
79
 
80
+ sections.append({
81
+ "title": clean_text_block(sec["name"]),
82
+ "content": clean_text_block(content),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  "articles": [
84
+ {"number": a["number"], "text": clean_text_block(a["text"])}
 
 
 
85
  for a in articles
86
  ]
87
+ })
 
 
88
 
89
+ # 5) إرجاع المستند القانوني الكامل
 
 
90
  return {
91
  "message": "تم التحليل بنجاح",
92
  "saved_to_db": False,
93
  "law": {
94
+ "title": clean_text_block(title),
95
+ "preamble": clean_text_block(preamble),
96
  "sections": sections
97
  }
98
  }