Mazenbs commited on
Commit
ef66367
·
verified ·
1 Parent(s): 3f58b85

Update parser/section_extractor.py

Browse files
Files changed (1) hide show
  1. parser/section_extractor.py +53 -20
parser/section_extractor.py CHANGED
@@ -1,38 +1,71 @@
1
  # parser/section_extractor.py
2
  import re
3
  from typing import List, Dict, Any, Tuple
 
4
 
5
  SECTION_KEYWORDS = ["الكتاب", "الباب", "الفصل"]
6
 
 
 
 
7
  def is_section_line(line: str) -> bool:
8
- return bool(re.match(rf"^(?:{'|'.join(SECTION_KEYWORDS)})\b", line))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def extract_sections_from_text_blocks(text_blocks: List[str]) -> Tuple[List[Dict[str, Any]], List[str]]:
11
- sections = []
12
- preamble = []
13
- current = None
14
- found = False
15
 
16
  for block in text_blocks:
17
- parts = re.split(r"(?<=\.)|(?=\b(?:الكتاب|الباب|الفصل)\b)", block)
18
-
19
- for part in parts:
20
- part = part.strip()
21
- if not part:
22
  continue
23
 
24
- if is_section_line(part):
25
- found = True
26
- current = {"title": part, "lines": []}
 
 
 
 
 
 
27
  sections.append(current)
 
 
 
 
 
 
 
 
 
28
  continue
29
 
30
- if not found:
31
- preamble.append(part)
32
- else:
33
- if current is None:
34
- current = {"title": "", "lines": []}
35
- sections.append(current)
36
- current["lines"].append(part)
37
 
38
  return sections, preamble
 
1
  # parser/section_extractor.py
2
  import re
3
  from typing import List, Dict, Any, Tuple
4
+ from parser.article_extractor import ARTICLE_KEYWORD
5
 
6
  SECTION_KEYWORDS = ["الكتاب", "الباب", "الفصل"]
7
 
8
+ # يتأكد أن السطر يبدأ بكلمة قسم (الكتاب|الباب|الفصل) وليس مجرد وجودها داخل السطر
9
+ SECTION_RE = re.compile(rf"^\s*(?:{'|'.join(SECTION_KEYWORDS)})\b", re.IGNORECASE)
10
+
11
  def is_section_line(line: str) -> bool:
12
+ return bool(SECTION_RE.match(line))
13
+
14
+ def split_title_and_following(line: str) -> (str, str):
15
+ """
16
+ إذا كان سطر العنوان يحتوي على بداية مادة (مثلاً: 'الفصل الاول ... مادة(1) ...')
17
+ فإننا نُعيد (عنوان مقطّع, الباقي الذي يبدأ بكلمة 'مادة' أو نص لاحق)
18
+ وإلا نعيد (line, "")
19
+ """
20
+ # نبحث عن كلمة "مادة" كحد فارز (نأخذ أول ظهور)
21
+ idx = re.search(rf"\b{ARTICLE_KEYWORD}\b", line)
22
+ if idx:
23
+ i = idx.start()
24
+ title_part = line[:i].strip()
25
+ follow = line[i:].strip() # يبدأ بكلمة 'مادة'
26
+ return title_part, follow
27
+ return line.strip(), ""
28
 
29
  def extract_sections_from_text_blocks(text_blocks: List[str]) -> Tuple[List[Dict[str, Any]], List[str]]:
30
+ sections: List[Dict[str, Any]] = []
31
+ preamble: List[str] = []
32
+ current: Dict[str, Any] = None
33
+ found_any_section = False
34
 
35
  for block in text_blocks:
36
+ # نقسم البلوك إلى أسطر حقيقية للحفاظ على الترتيب
37
+ for raw_line in block.splitlines():
38
+ line = raw_line.strip()
39
+ if not line:
 
40
  continue
41
 
42
+ # إذا كان هذا سطر قسم
43
+ if is_section_line(line):
44
+ found_any_section = True
45
+ title, follow = split_title_and_following(line)
46
+
47
+ # لو العنوان فارغ بعد القص، نعطيه السطر الأصلي المقصّر
48
+ title = title or line
49
+
50
+ current = {"title": title, "lines": []}
51
  sections.append(current)
52
+
53
+ # إن وُجد نص يبدأ بـ "مادة" داخل نفس السطر، ضعه كسطر تابع ليعالج لاحقًا
54
+ if follow:
55
+ current["lines"].append(follow)
56
+ continue
57
+
58
+ # لو لم نعثر على أي قسم بعدُ → جزء من الـ preamble
59
+ if not found_any_section:
60
+ preamble.append(line)
61
  continue
62
 
63
+ # لو لا يوجد current (نادر) نفتح قسم افتراضي
64
+ if current is None:
65
+ current = {"title": "", "lines": []}
66
+ sections.append(current)
67
+
68
+ # إضافة السطر إلى القسم الحالي
69
+ current["lines"].append(line)
70
 
71
  return sections, preamble