Spaces:
Running
Running
Update parser/assembler.py
Browse files- parser/assembler.py +20 -40
parser/assembler.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
from typing import List, Dict
|
| 2 |
from helpers.utils import normalize_digits, extract_article_number, detect_line_type
|
| 3 |
|
| 4 |
-
|
| 5 |
def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
| 6 |
"""
|
| 7 |
استخراج عنوان القانون والمقدمة، وإرجاع بقية النصوص بعد المقدمة.
|
|
@@ -29,8 +28,7 @@ def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
|
| 29 |
|
| 30 |
def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
|
| 31 |
"""
|
| 32 |
-
استخراج المواد
|
| 33 |
-
أي نص عادي يتم إضافته للمادة السابقة أو كمادة بدون رقم.
|
| 34 |
"""
|
| 35 |
articles = []
|
| 36 |
current = None
|
|
@@ -40,34 +38,29 @@ def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
|
|
| 40 |
line_type = detect_line_type(t)
|
| 41 |
|
| 42 |
if line_type == "article":
|
| 43 |
-
# حفظ المادة السابقة قبل الانتقال للجديدة
|
| 44 |
if current:
|
| 45 |
current["text"] = current["text"].strip()
|
| 46 |
articles.append(current)
|
| 47 |
-
# بدء مادة جديدة
|
| 48 |
current = {"number": extract_article_number(t), "text": t}
|
| 49 |
-
|
| 50 |
elif line_type == "text":
|
| 51 |
-
# إضافة النص للمادة الحالية، أو إنشاء مادة بدون رقم إذا لم توجد
|
| 52 |
if current:
|
| 53 |
current["text"] += "\n" + t
|
| 54 |
else:
|
| 55 |
current = {"number": None, "text": t}
|
| 56 |
-
|
| 57 |
elif line_type == "section":
|
| 58 |
-
#
|
| 59 |
continue
|
| 60 |
|
| 61 |
-
# إضافة آخر مادة
|
| 62 |
if current:
|
| 63 |
current["text"] = current["text"].strip()
|
| 64 |
articles.append(current)
|
| 65 |
|
| 66 |
return articles
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
"""
|
| 69 |
-
تقسيم النصوص إلى أقسام
|
| 70 |
-
يعتمد على detect_line_type لتحديد نوع السطر.
|
| 71 |
"""
|
| 72 |
sections = []
|
| 73 |
current = {"name": "", "texts": []}
|
|
@@ -79,20 +72,13 @@ def extract_sections(texts: list):
|
|
| 79 |
t_norm = normalize_digits(t.strip())
|
| 80 |
line_type = detect_line_type(t_norm)
|
| 81 |
|
| 82 |
-
# إذا كان السطر بداية قسم/باب/فصل
|
| 83 |
if line_type == "section":
|
| 84 |
-
# حفظ القسم السابق إذا وجد
|
| 85 |
if current["texts"] or current["name"]:
|
| 86 |
sections.append(current)
|
| 87 |
-
|
| 88 |
-
# بدء قسم جديد
|
| 89 |
current = {"name": t_norm, "texts": []}
|
| 90 |
-
|
| 91 |
else:
|
| 92 |
-
# إضافة السطر إلى القسم الحالي (سواء كان مادة أو نص عادي)
|
| 93 |
current["texts"].append(t_norm)
|
| 94 |
|
| 95 |
-
# إضافة آخر قسم
|
| 96 |
if current["texts"] or current["name"]:
|
| 97 |
sections.append(current)
|
| 98 |
|
|
@@ -101,40 +87,34 @@ def extract_sections(texts: list):
|
|
| 101 |
|
| 102 |
def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
|
| 103 |
"""
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
"""
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
title, preamble, remaining_texts = extract_title_and_preamble(
|
| 112 |
|
| 113 |
-
#
|
| 114 |
sections_raw = extract_sections(remaining_texts)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
sections = []
|
| 118 |
for sec in sections_raw:
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
content_lines = [t for t in raw_texts if detect_line_type(t) == "text"]
|
| 123 |
content = "\n".join(content_lines).strip()
|
| 124 |
|
| 125 |
-
# استخراج المواد مع ضم النصوص العادية التابعة لكل مادة
|
| 126 |
-
articles = extract_articles_from_texts(raw_texts)
|
| 127 |
-
|
| 128 |
sections.append({
|
| 129 |
"title": sec["name"],
|
| 130 |
"content": content,
|
| 131 |
-
"articles": [
|
| 132 |
-
{"number": a["number"], "text": a["text"]}
|
| 133 |
-
for a in articles
|
| 134 |
-
]
|
| 135 |
})
|
| 136 |
|
| 137 |
-
# 5) إرجاع المستند القانوني الكامل
|
| 138 |
return {
|
| 139 |
"message": "تم التحليل بنجاح",
|
| 140 |
"saved_to_db": False,
|
|
|
|
| 1 |
from typing import List, Dict
|
| 2 |
from helpers.utils import normalize_digits, extract_article_number, detect_line_type
|
| 3 |
|
|
|
|
| 4 |
def extract_title_and_preamble(texts: List[str]) -> (str, str, List[str]):
|
| 5 |
"""
|
| 6 |
استخراج عنوان القانون والمقدمة، وإرجاع بقية النصوص بعد المقدمة.
|
|
|
|
| 28 |
|
| 29 |
def extract_articles_from_texts(texts: List[str]) -> List[Dict]:
|
| 30 |
"""
|
| 31 |
+
استخراج المواد من قائمة نصوص معينة، مع ضم أي نصوص تتبع المادة.
|
|
|
|
| 32 |
"""
|
| 33 |
articles = []
|
| 34 |
current = None
|
|
|
|
| 38 |
line_type = detect_line_type(t)
|
| 39 |
|
| 40 |
if line_type == "article":
|
|
|
|
| 41 |
if current:
|
| 42 |
current["text"] = current["text"].strip()
|
| 43 |
articles.append(current)
|
|
|
|
| 44 |
current = {"number": extract_article_number(t), "text": t}
|
|
|
|
| 45 |
elif line_type == "text":
|
|
|
|
| 46 |
if current:
|
| 47 |
current["text"] += "\n" + t
|
| 48 |
else:
|
| 49 |
current = {"number": None, "text": t}
|
|
|
|
| 50 |
elif line_type == "section":
|
| 51 |
+
# لا نفعل شيئًا هنا
|
| 52 |
continue
|
| 53 |
|
|
|
|
| 54 |
if current:
|
| 55 |
current["text"] = current["text"].strip()
|
| 56 |
articles.append(current)
|
| 57 |
|
| 58 |
return articles
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def extract_sections(texts: List[str]) -> List[Dict]:
|
| 62 |
"""
|
| 63 |
+
تقسيم النصوص إلى أقسام وفصول.
|
|
|
|
| 64 |
"""
|
| 65 |
sections = []
|
| 66 |
current = {"name": "", "texts": []}
|
|
|
|
| 72 |
t_norm = normalize_digits(t.strip())
|
| 73 |
line_type = detect_line_type(t_norm)
|
| 74 |
|
|
|
|
| 75 |
if line_type == "section":
|
|
|
|
| 76 |
if current["texts"] or current["name"]:
|
| 77 |
sections.append(current)
|
|
|
|
|
|
|
| 78 |
current = {"name": t_norm, "texts": []}
|
|
|
|
| 79 |
else:
|
|
|
|
| 80 |
current["texts"].append(t_norm)
|
| 81 |
|
|
|
|
| 82 |
if current["texts"] or current["name"]:
|
| 83 |
sections.append(current)
|
| 84 |
|
|
|
|
| 87 |
|
| 88 |
def parse_law_from_texts(text_blocks: List[Dict[str, str]]) -> Dict:
|
| 89 |
"""
|
| 90 |
+
استخراج القانون بشكل منظم:
|
| 91 |
+
1) العنوان والمقدمة
|
| 92 |
+
2) الأقسام
|
| 93 |
+
3) المواد داخل كل قسم
|
| 94 |
"""
|
| 95 |
+
# النصوص الخام
|
| 96 |
+
raw_texts = [b.get("text", "") for b in text_blocks if b.get("text")]
|
| 97 |
|
| 98 |
+
# 1) استخراج العنوان والمقدمة
|
| 99 |
+
title, preamble, remaining_texts = extract_title_and_preamble(raw_texts)
|
| 100 |
|
| 101 |
+
# 2) استخراج الأقسام
|
| 102 |
sections_raw = extract_sections(remaining_texts)
|
| 103 |
|
| 104 |
+
# 3) لكل قسم، استخراج المواد
|
| 105 |
sections = []
|
| 106 |
for sec in sections_raw:
|
| 107 |
+
articles = extract_articles_from_texts(sec["texts"])
|
| 108 |
+
# المحتوى النصي العادي داخل القسم
|
| 109 |
+
content_lines = [t for t in sec["texts"] if detect_line_type(t) == "text"]
|
|
|
|
| 110 |
content = "\n".join(content_lines).strip()
|
| 111 |
|
|
|
|
|
|
|
|
|
|
| 112 |
sections.append({
|
| 113 |
"title": sec["name"],
|
| 114 |
"content": content,
|
| 115 |
+
"articles": [{"number": a["number"], "text": a["text"]} for a in articles]
|
|
|
|
|
|
|
|
|
|
| 116 |
})
|
| 117 |
|
|
|
|
| 118 |
return {
|
| 119 |
"message": "تم التحليل بنجاح",
|
| 120 |
"saved_to_db": False,
|