chore: add raw text verification scripts
Browse filesFiles changed (6)
- src/arabic_ordinals.pyAdded+99
- src/inspect_duplicates.pyAdded+36
- src/inspect_numbers.pyAdded+41
- src/inspect_patterns.pyAdded+32
- src/inspect_region.pyAdded+10
- src/verify_content.pyAdded+15
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import re
ONES = { "الأولى": 1, "الثانية": 2, "الثالثة": 3, "الرابعة": 4, "الخامسة": 5, "السادسة": 6, "السابعة": 7, "الثامنة": 8, "التاسعة": 9, "العاشرة": 10,}
TEENS = { "الحادية عشرة": 11, "الثانية عشرة": 12, "الثالثة عشرة": 13, "الرابعة عشرة": 14, "الخامسة عشرة": 15, "السادسة عشرة": 16, "السابعة عشرة": 17, "الثامنة عشرة": 18, "التاسعة عشرة": 19,}
TENS = { "العشرون": 20, "العشرين": 20, "الثلاثون": 30, "الثلاثين": 30, "الأربعون": 40, "الأربعين": 40, "الخمسون": 50, "الخمسين": 50, "الستون": 60, "الستين": 60, "السبعون": 70, "السبعين": 70, "الثمانون": 80, "الثمانين": 80, "التسعون": 90, "التسعين": 90,}
# Units as used in compounds: "الحادية والعشرون"COMPOUND_ONES = { "الحادية": 1, "الثانية": 2, "الثالثة": 3, "الرابعة": 4, "الخامسة": 5, "السادسة": 6, "السابعة": 7, "الثامنة": 8, "التاسعة": 9,}
def normalize(text: str) -> str: """Remove diacritics-free noise: alef variants, colons, extra spaces.""" text = text.replace("ـ", "") text = re.sub(r"[::]", " ", text) text = re.sub(r"\s+", " ", text) return text.strip()
def ordinal_to_int(heading: str): """Convert an Arabic ordinal article heading to an integer. None if unknown.""" text = normalize(heading) text = re.sub(r"^المادة\s*", "", text)
hundreds = 0 if re.search(r"بعد\s+الما?ئتين", text): hundreds = 200 text = re.sub(r"بعد\s+الما?ئتين", "", text) elif re.search(r"بعد\s+الما?ئة", text): hundreds = 100 text = re.sub(r"بعد\s+الما?ئة", "", text)
text = normalize(text)
if not text: # "المادة المائة" style handled below return hundreds or None if text in ("المائة", "المئة"): return 100 if text in ("المائتان", "المائتين", "المئتان", "المئتين"): return 200 if text in TEENS: return hundreds + TEENS[text] if text in ONES: return hundreds + ONES[text] if text in TENS: return hundreds + TENS[text]
# Compound: "الحادية والعشرون" parts = text.split() if len(parts) == 2 and parts[1].startswith("و"): unit = COMPOUND_ONES.get(parts[0]) ten = TENS.get(parts[1][1:]) if unit is not None and ten is not None: return hundreds + unit + ten
return None# Renumbered heading: "(المادة X حالياً) (المادة Y سابقاً)"RENUMBERED_RE = re.compile(r"^\(\s*المادة\s+(?P<current>.+?)\s+حالي\S*\s*\)")FORMER_RE = re.compile(r"\(\s*المادة\s+(?P<former>.+?)\s+سابق\S*\s*\)")
def is_heading_candidate(line: str) -> bool: return bool(re.match(r"^\(?\s*المادة", line.strip()))
def parse_heading(line: str): """Return {'number', 'former_number', 'raw'} or None if not a heading.""" line = line.strip()
m = RENUMBERED_RE.match(line) if m: rest = line[m.end():] former_m = FORMER_RE.search(rest) return { "number": ordinal_to_int(m.group("current")), "former_number": ordinal_to_int(former_m.group("former")) if former_m else None, "raw": line, }
if line.startswith("المادة"): return {"number": ordinal_to_int(line), "former_number": None, "raw": line}
return None123456789101112131415161718192021222324252627282930313233343536from pathlib import Pathfrom collections import defaultdictfrom arabic_ordinals import is_heading_candidate, parse_heading
lines = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8").splitlines()
occurrences = defaultdict(list)renumbered = []
for i, line in enumerate(lines): if not is_heading_candidate(line): continue h = parse_heading(line) if h and h["number"] is not None: occurrences[h["number"]].append(i) if h["former_number"] is not None: renumbered.append((i, h["number"], h["former_number"]))
print(f"Renumbered articles found: {len(renumbered)}")for i, num, former in renumbered: print(f" [{i:4}] {num} (was {former})")print("=" * 70)
dupes = {n: idxs for n, idxs in occurrences.items() if len(idxs) > 1}print(f"Duplicate numbers: {sorted(dupes)}")print("=" * 70)
for num in sorted(dupes): print(f"### ARTICLE {num} appears {len(dupes[num])} times") for idx in dupes[num]: print(f" --- line {idx} ---") for j in range(idx, min(idx + 4, len(lines))): content = lines[j].strip() if content: print(f" {content[:110]}") print("=" * 70)1234567891011121314151617181920212223242526272829303132333435363738394041from pathlib import Pathfrom arabic_ordinals import is_heading_candidate, parse_heading
lines = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8").splitlines()
parsed, failed = [], []for i, line in enumerate(lines): if not is_heading_candidate(line): continue h = parse_heading(line) if h is None or h["number"] is None: failed.append((i, line.strip())) else: parsed.append((i, h))
print(f"Candidates: {len(parsed) + len(failed)} | Parsed: {len(parsed)} | Failed: {len(failed)}")print("=" * 60)
if failed: print("FAILED:") for i, line in failed: print(f"[{i:4}] {line[:90]}") print("=" * 60)
renumbered = [(i, h) for i, h in parsed if h["former_number"] is not None]print(f"Renumbered articles: {len(renumbered)}")for i, h in renumbered: print(f"[{i:4}] {h['number']} (was {h['former_number']})")print("=" * 60)
nums = [h["number"] for _, h in parsed]seen, dupes = set(), []for n in nums: if n in seen: dupes.append(n) seen.add(n)
print(f"Total unique : {len(seen)}")print(f"Duplicates : {sorted(set(dupes))}")print(f"Missing 1-245: {sorted(set(range(1, 246)) - seen)}")print(f"Out of range : {sorted(n for n in seen if n > 245)}")1234567891011121314151617181920212223242526272829303132from pathlib import Path
RAW_PATH = Path("data/raw/labor_law_raw.txt")lines = RAW_PATH.read_text(encoding="utf-8").splitlines()
# Candidate headings: lines that START with "المادة"candidates = [ (i, line.strip()) for i, line in enumerate(lines) if line.strip().startswith("المادة")]
print(f"Total lines : {len(lines)}")print(f"Lines STARTING with 'المادة' : {len(candidates)}")print("=" * 60)
print("FIRST 20 CANDIDATES:")for i, line in candidates[:20]: print(f"[{i:4}] {line}")
print("=" * 60)print("LAST 8 CANDIDATES:")for i, line in candidates[-8:]: print(f"[{i:4}] {line}")
print("=" * 60)for marker in ("الباب", "الفصل"): hits = [l.strip() for l in lines if l.strip().startswith(marker)] print(f"Lines STARTING with '{marker}' : {len(hits)}") for h in hits[:6]: print(f" {h}") print("-" * 40)12345678910from pathlib import Path
lines = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8").splitlines()
START, END = 870, 945
for i in range(START, min(END, len(lines))): content = lines[i].strip() if content: print(f"[{i:4}] {content[:130]}")123456789101112131415from pathlib import Pathimport hashlib
raw = Path("data/raw/labor_law_raw.txt").read_text(encoding="utf-8")
# Normalize line endings, then hash — this ignores CRLF vs LF differencesnormalized = raw.replace("\r\n", "\n").replace("\r", "\n")
h = hashlib.sha256(normalized.encode("utf-8")).hexdigest()
print(f"Chars (raw) : {len(raw)}")print(f"Chars (normalized) : {len(normalized)}")print(f"CRLF pairs : {raw.count(chr(13) + chr(10))}")print(f"Lone LF : {normalized.count(chr(10))}")print(f"Normalized SHA256 : {h}")src/arabic_ordinals.pyAdded+99
src/inspect_duplicates.pyAdded+36
src/inspect_numbers.pyAdded+41
src/inspect_patterns.pyAdded+32
src/inspect_region.pyAdded+10
src/verify_content.pyAdded+15