Mazenbs commited on
Commit
cdb4964
·
verified ·
1 Parent(s): 7f167ce

Update parser/assembler.py

Browse files
Files changed (1) hide show
  1. parser/assembler.py +25 -52
parser/assembler.py CHANGED
@@ -1,65 +1,34 @@
1
  # parser/assembler.py
2
-
3
- from typing import List, Dict, Optional, Tuple
4
  from helpers.utils import normalize_digits, extract_article_number, is_article, is_section
5
 
 
6
  def extract_title_and_preamble(
7
  blocks: List[Dict[str, str]],
8
- *,
9
- default_title: Optional[str] = None,
10
- default_preamble: Optional[str] = None
11
  ) -> Tuple[str, str, List[Dict[str, str]]]:
12
  """
13
- تعمل على قائمة من الكتل ويمكن أن تحتوي كل كتلة على مفتاح type مسبقاً:
14
- type = "title" | "preamble" | "body"
15
- إذا وُجد type يُستخدم مباشرة، وإلا تُستخدم الطريقة التقليدية.
16
- يمكن أيضاً تمرير عنوان أو مقدمة افتراضية لتُستخدم بدلاً من الاستخراج التلقائي.
17
  """
18
- # إذا أُرسلت قيم افتراضية نستخدمها فوراً
19
- if default_title is not None and default_preamble is not None:
20
- # نفصل كتل body فقط (أي شيء لا يُعتبر title أو preamble)
21
- body_blocks = [b for b in blocks if b.get("type") != "title" and b.get("type") != "preamble"]
22
- return default_title, default_preamble, body_blocks
23
-
24
- # هل القائمة تحتوي على حقل type مُحدد مسبقاً؟
25
- if any(b.get("type") in {"title", "preamble", "body"} for b in blocks):
26
- title_blocks = [b for b in blocks if b.get("type") == "title"]
27
- preamble_blocks = [b for b in blocks if b.get("type") == "preamble"]
28
- body_blocks = [b for b in blocks if b.get("type") == "body"]
29
-
30
- title = "\n".join([b["text"].strip() for b in title_blocks]).strip()
31
- preamble = "\n".join([b["text"].strip() for b in preamble_blocks]).strip()
32
- return title, preamble, body_blocks
33
-
34
- # الطريقة التقليدية (المنطق القديم دون تغيير)
35
- title = ""
36
- preamble_blocks = []
37
-
38
- # استخراج العنوان
39
- while blocks:
40
- block = blocks.pop(0)
41
- t = block.get("text", "").strip()
42
- if t.lower() != "html" and t:
43
- title = t
44
- break
45
-
46
- # استخراج المقدمة حتى أول قسم أو مادة
47
- while blocks:
48
- t = blocks[0].get("text", "").strip()
49
- if is_section(t) or is_article(t):
50
- break
51
- preamble_blocks.append(blocks.pop(0))
52
 
 
53
  preamble = "\n".join([b["text"] for b in preamble_blocks]).strip()
54
- return title, preamble, blocks
 
55
 
56
 
57
  def extract_articles_from_blocks(blocks: List[Dict[str, str]]) -> List[Dict]:
 
 
 
58
  articles = []
59
  current = None
60
 
61
  for block in blocks:
62
- t = block.get("text", "").strip()
63
  if is_article(t):
64
  if current:
65
  current["text"] = current["text"].strip()
@@ -79,11 +48,14 @@ def extract_articles_from_blocks(blocks: List[Dict[str, str]]) -> List[Dict]:
79
 
80
 
81
  def extract_sections(blocks: List[Dict[str, str]]) -> List[Dict]:
 
 
 
82
  sections = []
83
  current = {"name": "", "texts": []}
84
 
85
  for block in blocks:
86
- t = normalize_digits(block.get("text", "").strip())
87
 
88
  if is_section(t):
89
  if current["texts"] or current["name"]:
@@ -99,16 +71,17 @@ def extract_sections(blocks: List[Dict[str, str]]) -> List[Dict]:
99
 
100
 
101
  def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
102
- raw_blocks = [b for b in text_blocks if b.get("text")]
103
-
104
- title, preamble, remaining_blocks = extract_title_and_preamble(raw_blocks)
 
105
  sections_raw = extract_sections(remaining_blocks)
106
 
107
  sections = []
108
  for sec in sections_raw:
109
  raw_blocks = sec["texts"]
110
 
111
- content = "\n".join([b["text"] for b in raw_blocks if not is_article(b.get("text", ""))]).strip()
112
  articles = extract_articles_from_blocks(raw_blocks)
113
 
114
  sections.append({
@@ -121,10 +94,10 @@ def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
121
  })
122
 
123
  return {
124
- "message": "success", # كلمة إنجليزية بدلًا من تم التحليل بنجاح
125
  "all_blocks": {
126
  "count": len(text_blocks),
127
- #"items": text_blocks
128
  },
129
  "law": {
130
  "title": title,
 
1
  # parser/assembler.py
2
+ from typing import List, Dict, Tuple
 
3
  from helpers.utils import normalize_digits, extract_article_number, is_article, is_section
4
 
5
+
6
  def extract_title_and_preamble(
7
  blocks: List[Dict[str, str]],
 
 
 
8
  ) -> Tuple[str, str, List[Dict[str, str]]]:
9
  """
10
+ استخراج العنوان والمقدمة وبقية النصوص (body) من الكتل.
11
+ كل كتلة تحتوي على {"text": ..., "type": ...}، جميع العناصر مستخدمة.
 
 
12
  """
13
+ title_blocks = [b for b in blocks if b["type"] == "title"]
14
+ preamble_blocks = [b for b in blocks if b["type"] == "preamble"]
15
+ body_blocks = [b for b in blocks if b["type"] == "body"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ title = "\n".join([b["text"] for b in title_blocks]).strip()
18
  preamble = "\n".join([b["text"] for b in preamble_blocks]).strip()
19
+
20
+ return title, preamble, body_blocks
21
 
22
 
23
  def extract_articles_from_blocks(blocks: List[Dict[str, str]]) -> List[Dict]:
24
+ """
25
+ تقسيم النصوص إلى مقالات حسب is_article.
26
+ """
27
  articles = []
28
  current = None
29
 
30
  for block in blocks:
31
+ t = block["text"].strip()
32
  if is_article(t):
33
  if current:
34
  current["text"] = current["text"].strip()
 
48
 
49
 
50
  def extract_sections(blocks: List[Dict[str, str]]) -> List[Dict]:
51
+ """
52
+ تقسيم النصوص إلى أقسام حسب is_section.
53
+ """
54
  sections = []
55
  current = {"name": "", "texts": []}
56
 
57
  for block in blocks:
58
+ t = normalize_digits(block["text"].strip())
59
 
60
  if is_section(t):
61
  if current["texts"] or current["name"]:
 
71
 
72
 
73
  def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
74
+ """
75
+ تحويل قائمة النصوص المفهرسة إلى هيكل القانون النهائي مع أقسامه ومقالاته.
76
+ """
77
+ title, preamble, remaining_blocks = extract_title_and_preamble(text_blocks)
78
  sections_raw = extract_sections(remaining_blocks)
79
 
80
  sections = []
81
  for sec in sections_raw:
82
  raw_blocks = sec["texts"]
83
 
84
+ content = "\n".join([b["text"] for b in raw_blocks if not is_article(b["text"])]).strip()
85
  articles = extract_articles_from_blocks(raw_blocks)
86
 
87
  sections.append({
 
94
  })
95
 
96
  return {
97
+ "message": "success",
98
  "all_blocks": {
99
  "count": len(text_blocks),
100
+ #"items": text_blocks # يمكن إلغاء التعليق إذا أردت كل العناصر
101
  },
102
  "law": {
103
  "title": title,