Spaces:
Running
Running
Update parser/assembler.py
Browse files- parser/assembler.py +25 -8
parser/assembler.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 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,
|
|
|
|
| 5 |
|
| 6 |
def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
| 7 |
"""
|
|
@@ -18,7 +19,8 @@ def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
|
| 18 |
remaining_texts = []
|
| 19 |
|
| 20 |
for t in texts:
|
| 21 |
-
|
|
|
|
| 22 |
remaining_texts.append(t)
|
| 23 |
else:
|
| 24 |
preamble_lines.append(t)
|
|
@@ -28,20 +30,34 @@ def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
|
| 28 |
|
| 29 |
|
| 30 |
def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
|
| 31 |
-
"""
|
|
|
|
|
|
|
| 32 |
articles = []
|
| 33 |
current = None
|
| 34 |
|
| 35 |
for t in texts:
|
| 36 |
t = t.strip()
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
if current:
|
| 39 |
current["text"] = current["text"].strip()
|
| 40 |
articles.append(current)
|
|
|
|
| 41 |
current = {"number": extract_article_number(t), "text": t}
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
if current:
|
| 44 |
current["text"] += "\n" + t
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if current:
|
| 47 |
current["text"] = current["text"].strip()
|
|
@@ -53,7 +69,7 @@ def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
|
|
| 53 |
def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
|
| 54 |
"""
|
| 55 |
تحويل نصوص القانون إلى هيكل JSON مع تنظيف واستخراج مواد وأقسام
|
| 56 |
-
|
| 57 |
"""
|
| 58 |
|
| 59 |
# 1) تنظيف النصوص الخام
|
|
@@ -70,8 +86,9 @@ def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
|
|
| 70 |
for sec in sections_raw:
|
| 71 |
raw_texts = sec["texts"]
|
| 72 |
|
| 73 |
-
# المحتوى غير المواد
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
# استخراج المواد
|
| 77 |
articles = extract_articles_from_texts(raw_texts)
|
|
|
|
| 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, extract_article_number, clean_text_block, detect_line_type
|
| 5 |
+
|
| 6 |
|
| 7 |
def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
| 8 |
"""
|
|
|
|
| 19 |
remaining_texts = []
|
| 20 |
|
| 21 |
for t in texts:
|
| 22 |
+
line_type = detect_line_type(t)
|
| 23 |
+
if line_type in ("section", "article"):
|
| 24 |
remaining_texts.append(t)
|
| 25 |
else:
|
| 26 |
preamble_lines.append(t)
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
|
| 33 |
+
"""
|
| 34 |
+
استخراج المواد مع ضمان عدم فقد أي نص باستخدام detect_line_type.
|
| 35 |
+
"""
|
| 36 |
articles = []
|
| 37 |
current = None
|
| 38 |
|
| 39 |
for t in texts:
|
| 40 |
t = t.strip()
|
| 41 |
+
line_type = detect_line_type(t)
|
| 42 |
+
|
| 43 |
+
if line_type == "article":
|
| 44 |
+
# حفظ المادة السابقة قبل الانتقال للجديدة
|
| 45 |
if current:
|
| 46 |
current["text"] = current["text"].strip()
|
| 47 |
articles.append(current)
|
| 48 |
+
# إنشاء مادة جديدة
|
| 49 |
current = {"number": extract_article_number(t), "text": t}
|
| 50 |
+
|
| 51 |
+
elif line_type == "text":
|
| 52 |
+
# إضافة نص عادي للمادة الحالية أو إنشاء مادة بدون رقم
|
| 53 |
if current:
|
| 54 |
current["text"] += "\n" + t
|
| 55 |
+
else:
|
| 56 |
+
current = {"number": None, "text": t}
|
| 57 |
+
|
| 58 |
+
elif line_type == "section":
|
| 59 |
+
# تجاهل بداية قسم هنا (سيتم التعامل معها في parse_law_from_texts)
|
| 60 |
+
continue
|
| 61 |
|
| 62 |
if current:
|
| 63 |
current["text"] = current["text"].strip()
|
|
|
|
| 69 |
def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
|
| 70 |
"""
|
| 71 |
تحويل نصوص القانون إلى هيكل JSON مع تنظيف واستخراج مواد وأقسام
|
| 72 |
+
باستخدام detect_line_type.
|
| 73 |
"""
|
| 74 |
|
| 75 |
# 1) تنظيف النصوص الخام
|
|
|
|
| 86 |
for sec in sections_raw:
|
| 87 |
raw_texts = sec["texts"]
|
| 88 |
|
| 89 |
+
# المحتوى غير المواد (نصوص عادية داخل القسم)
|
| 90 |
+
content_lines = [t for t in raw_texts if detect_line_type(t) == "text"]
|
| 91 |
+
content = "\n".join(content_lines).strip()
|
| 92 |
|
| 93 |
# استخراج المواد
|
| 94 |
articles = extract_articles_from_texts(raw_texts)
|