Spaces:
Runtime error

Komail262 commited on
Commit
4d65678
·
1 Parent(s): 36f2eae

chore: add raw text verification scripts

Browse files
src/arabic_ordinals.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ ONES = {
4
+ "الأولى": 1, "الثانية": 2, "الثالثة": 3, "الرابعة": 4, "الخامسة": 5,
5
+ "السادسة": 6, "السابعة": 7, "الثامنة": 8, "التاسعة": 9, "العاشرة": 10,
6
+ }
7
+
8
+ TEENS = {
9
+ "الحادية عشرة": 11, "الثانية عشرة": 12, "الثالثة عشرة": 13,
10
+ "الرابعة عشرة": 14, "الخامسة عشرة": 15, "السادسة عشرة": 16,
11
+ "السابعة عشرة": 17, "الثامنة عشرة": 18, "التاسعة عشرة": 19,
12
+ }
13
+
14
+ TENS = {
15
+ "العشرون": 20, "العشرين": 20, "الثلاثون": 30, "الثلاثين": 30,
16
+ "الأربعون": 40, "الأربعين": 40, "الخمسون": 50, "الخمسين": 50,
17
+ "الستون": 60, "الستين": 60, "السبعون": 70, "السبعين": 70,
18
+ "الثمانون": 80, "الثمانين": 80, "التسعون": 90, "التسعين": 90,
19
+ }
20
+
21
+ # Units as used in compounds: "الحادية والعشرون"
22
+ COMPOUND_ONES = {
23
+ "الحادية": 1, "الثانية": 2, "الثالثة": 3, "الرابعة": 4, "الخامسة": 5,
24
+ "السادسة": 6, "السابعة": 7, "الثامنة": 8, "التاسعة": 9,
25
+ }
26
+
27
+
28
+ def normalize(text: str) -> str:
29
+ """Remove diacritics-free noise: alef variants, colons, extra spaces."""
30
+ text = text.replace("ـ", "")
31
+ text = re.sub(r"[::]", " ", text)
32
+ text = re.sub(r"\s+", " ", text)
33
+ return text.strip()
34
+
35
+
36
+ def ordinal_to_int(heading: str):
37
+ """Convert an Arabic ordinal article heading to an integer. None if unknown."""
38
+ text = normalize(heading)
39
+ text = re.sub(r"^المادة\s*", "", text)
40
+
41
+ hundreds = 0
42
+ if re.search(r"بعد\s+الما?ئتين", text):
43
+ hundreds = 200
44
+ text = re.sub(r"بعد\s+الما?ئتين", "", text)
45
+ elif re.search(r"بعد\s+الما?ئة", text):
46
+ hundreds = 100
47
+ text = re.sub(r"بعد\s+الما?ئة", "", text)
48
+
49
+ text = normalize(text)
50
+
51
+ if not text: # "المادة المائة" style handled below
52
+ return hundreds or None
53
+ if text in ("المائة", "المئة"):
54
+ return 100
55
+ if text in ("المائتان", "المائتين", "المئتان", "المئتين"):
56
+ return 200
57
+ if text in TEENS:
58
+ return hundreds + TEENS[text]
59
+ if text in ONES:
60
+ return hundreds + ONES[text]
61
+ if text in TENS:
62
+ return hundreds + TENS[text]
63
+
64
+ # Compound: "الحادية والعشرون"
65
+ parts = text.split()
66
+ if len(parts) == 2 and parts[1].startswith("و"):
67
+ unit = COMPOUND_ONES.get(parts[0])
68
+ ten = TENS.get(parts[1][1:])
69
+ if unit is not None and ten is not None:
70
+ return hundreds + unit + ten
71
+
72
+ return None
73
+ # Renumbered heading: "(المادة X حالياً) (المادة Y سابقاً)"
74
+ RENUMBERED_RE = re.compile(r"^\(\s*المادة\s+(?P<current>.+?)\s+حالي\S*\s*\)")
75
+ FORMER_RE = re.compile(r"\(\s*المادة\s+(?P<former>.+?)\s+سابق\S*\s*\)")
76
+
77
+
78
+ def is_heading_candidate(line: str) -> bool:
79
+ return bool(re.match(r"^\(?\s*المادة", line.strip()))
80
+
81
+
82
+ def parse_heading(line: str):
83
+ """Return {'number', 'former_number', 'raw'} or None if not a heading."""
84
+ line = line.strip()
85
+
86
+ m = RENUMBERED_RE.match(line)
87
+ if m:
88
+ rest = line[m.end():]
89
+ former_m = FORMER_RE.search(rest)
90
+ return {
91
+ "number": ordinal_to_int(m.group("current")),
92
+ "former_number": ordinal_to_int(former_m.group("former")) if former_m else None,
93
+ "raw": line,
94
+ }
95
+
96
+ if line.startswith("المادة"):
97
+ return {"number": ordinal_to_int(line), "former_number": None, "raw": line}
98
+
99
+ return None
src/inspect_duplicates.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from collections import defaultdict
3
+ from arabic_ordinals import is_heading_candidate, parse_heading
4
+
5
+ lines = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8").splitlines()
6
+
7
+ occurrences = defaultdict(list)
8
+ renumbered = []
9
+
10
+ for i, line in enumerate(lines):
11
+ if not is_heading_candidate(line):
12
+ continue
13
+ h = parse_heading(line)
14
+ if h and h["number"] is not None:
15
+ occurrences[h["number"]].append(i)
16
+ if h["former_number"] is not None:
17
+ renumbered.append((i, h["number"], h["former_number"]))
18
+
19
+ print(f"Renumbered articles found: {len(renumbered)}")
20
+ for i, num, former in renumbered:
21
+ print(f" [{i:4}] {num} (was {former})")
22
+ print("=" * 70)
23
+
24
+ dupes = {n: idxs for n, idxs in occurrences.items() if len(idxs) > 1}
25
+ print(f"Duplicate numbers: {sorted(dupes)}")
26
+ print("=" * 70)
27
+
28
+ for num in sorted(dupes):
29
+ print(f"### ARTICLE {num} appears {len(dupes[num])} times")
30
+ for idx in dupes[num]:
31
+ print(f" --- line {idx} ---")
32
+ for j in range(idx, min(idx + 4, len(lines))):
33
+ content = lines[j].strip()
34
+ if content:
35
+ print(f" {content[:110]}")
36
+ print("=" * 70)
src/inspect_numbers.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from arabic_ordinals import is_heading_candidate, parse_heading
3
+
4
+ lines = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8").splitlines()
5
+
6
+ parsed, failed = [], []
7
+ for i, line in enumerate(lines):
8
+ if not is_heading_candidate(line):
9
+ continue
10
+ h = parse_heading(line)
11
+ if h is None or h["number"] is None:
12
+ failed.append((i, line.strip()))
13
+ else:
14
+ parsed.append((i, h))
15
+
16
+ print(f"Candidates: {len(parsed) + len(failed)} | Parsed: {len(parsed)} | Failed: {len(failed)}")
17
+ print("=" * 60)
18
+
19
+ if failed:
20
+ print("FAILED:")
21
+ for i, line in failed:
22
+ print(f"[{i:4}] {line[:90]}")
23
+ print("=" * 60)
24
+
25
+ renumbered = [(i, h) for i, h in parsed if h["former_number"] is not None]
26
+ print(f"Renumbered articles: {len(renumbered)}")
27
+ for i, h in renumbered:
28
+ print(f"[{i:4}] {h['number']} (was {h['former_number']})")
29
+ print("=" * 60)
30
+
31
+ nums = [h["number"] for _, h in parsed]
32
+ seen, dupes = set(), []
33
+ for n in nums:
34
+ if n in seen:
35
+ dupes.append(n)
36
+ seen.add(n)
37
+
38
+ print(f"Total unique : {len(seen)}")
39
+ print(f"Duplicates : {sorted(set(dupes))}")
40
+ print(f"Missing 1-245: {sorted(set(range(1, 246)) - seen)}")
41
+ print(f"Out of range : {sorted(n for n in seen if n > 245)}")
src/inspect_patterns.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ RAW_PATH = Path("data/raw/labor_law_raw.txt")
4
+ lines = RAW_PATH.read_text(encoding="utf-8").splitlines()
5
+
6
+ # Candidate headings: lines that START with "المادة"
7
+ candidates = [
8
+ (i, line.strip())
9
+ for i, line in enumerate(lines)
10
+ if line.strip().startswith("المادة")
11
+ ]
12
+
13
+ print(f"Total lines : {len(lines)}")
14
+ print(f"Lines STARTING with 'المادة' : {len(candidates)}")
15
+ print("=" * 60)
16
+
17
+ print("FIRST 20 CANDIDATES:")
18
+ for i, line in candidates[:20]:
19
+ print(f"[{i:4}] {line}")
20
+
21
+ print("=" * 60)
22
+ print("LAST 8 CANDIDATES:")
23
+ for i, line in candidates[-8:]:
24
+ print(f"[{i:4}] {line}")
25
+
26
+ print("=" * 60)
27
+ for marker in ("الباب", "الفصل"):
28
+ hits = [l.strip() for l in lines if l.strip().startswith(marker)]
29
+ print(f"Lines STARTING with '{marker}' : {len(hits)}")
30
+ for h in hits[:6]:
31
+ print(f" {h}")
32
+ print("-" * 40)
src/inspect_region.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ lines = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8").splitlines()
4
+
5
+ START, END = 870, 945
6
+
7
+ for i in range(START, min(END, len(lines))):
8
+ content = lines[i].strip()
9
+ if content:
10
+ print(f"[{i:4}] {content[:130]}")
src/verify_content.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import hashlib
3
+
4
+ raw = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8")
5
+
6
+ # Normalize line endings, then hash — this ignores CRLF vs LF differences
7
+ normalized = raw.replace("\r\n", "\n").replace("\r", "\n")
8
+
9
+ h = hashlib.sha256(normalized.encode("utf-8")).hexdigest()
10
+
11
+ print(f"Chars (raw) : {len(raw)}")
12
+ print(f"Chars (normalized) : {len(normalized)}")
13
+ print(f"CRLF pairs : {raw.count(chr(13) + chr(10))}")
14
+ print(f"Lone LF : {normalized.count(chr(10))}")
15
+ print(f"Normalized SHA256 : {h}")