|
| 1 | +"""Label the Arabic corpus with morphological POS tags (qalsadi, CPU). |
| 2 | +
|
| 3 | +The aux-task lever: ByT5 has never seen explicit morphological |
| 4 | +supervision (POS/case). Qalsadi analyzes diacritized words offline — |
| 5 | +our gold corpus is fully diacritized, so its analyses act as |
| 6 | +near-ground-truth morphological labels for an r6 multi-task target. |
| 7 | +
|
| 8 | +Phase 1 (small limit) prints a sample analysis for API verification; |
| 9 | +phase 2 (full) writes /datasets/arabic-morph/train.jsonl. |
| 10 | +
|
| 11 | +Usage: |
| 12 | + modal run scripts/label_morph.py::probe # verify qalsadi API |
| 13 | + modal run scripts/label_morph.py # label 300k lines |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import json |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import modal |
| 22 | + |
| 23 | +datasets_volume = modal.Volume.from_name("rababa-datasets", create_if_missing=True) |
| 24 | +CORPUS = "/datasets/sadeed-decontam/train.txt" |
| 25 | +OUT = Path("/datasets/arabic-morph/train.jsonl") |
| 26 | +N_LINES = 300_000 |
| 27 | +N_WORKERS = 8 |
| 28 | + |
| 29 | +image = modal.Image.debian_slim(python_version="3.11").pip_install("qalsadi") |
| 30 | +app = modal.App("rababa-label-morph", image=image) |
| 31 | + |
| 32 | +_analyzer = None |
| 33 | + |
| 34 | + |
| 35 | +def _get_analyzer(): |
| 36 | + global _analyzer |
| 37 | + if _analyzer is None: |
| 38 | + from qalsadi.analex import Analex |
| 39 | + _analyzer = Analex() |
| 40 | + return _analyzer |
| 41 | + |
| 42 | + |
| 43 | +def _pos_of(word_result) -> str: |
| 44 | + for attr in ("pos", "tags", "tag"): |
| 45 | + v = getattr(word_result, attr, None) |
| 46 | + if v: |
| 47 | + return str(v)[:40] |
| 48 | + for meth in ("get_pos", "get_tags"): |
| 49 | + m = getattr(word_result, meth, None) |
| 50 | + if callable(m): |
| 51 | + try: |
| 52 | + return str(m())[:40] |
| 53 | + except Exception: |
| 54 | + pass |
| 55 | + return "X" |
| 56 | + |
| 57 | + |
| 58 | +def _label_line(line: str) -> tuple[str, list[str]]: |
| 59 | + an = _get_analyzer() |
| 60 | + words = line.split() |
| 61 | + tags: list[str] = [] |
| 62 | + for w in words: |
| 63 | + try: |
| 64 | + res = an.check_word(w) or [] |
| 65 | + except Exception: |
| 66 | + res = [] |
| 67 | + tags.append(_pos_of(res[0]) if res else "X") |
| 68 | + return line, tags |
| 69 | + |
| 70 | + |
| 71 | +@app.function(volumes={"/datasets": datasets_volume}, cpu=N_WORKERS, timeout=2 * 60 * 60) |
| 72 | +def probe() -> dict: |
| 73 | + from qalsadi.analex import Analex |
| 74 | + an = Analex() |
| 75 | + sample = ["قَالَ", "الْكِتَابُ", "يَكْتُبُونَ", "بِسْمِ"] |
| 76 | + for w in sample: |
| 77 | + res = an.check_word(w) or [] |
| 78 | + if res: |
| 79 | + print(f"{w}: attrs={sorted(a for a in dir(res[0]) if not a.startswith('_'))[:25]}", flush=True) |
| 80 | + print(f" pos={_pos_of(res[0])!r} stem={getattr(res[0], 'stem', '?')}", flush=True) |
| 81 | + else: |
| 82 | + print(f"{w}: no analysis", flush=True) |
| 83 | + return {"ok": True} |
| 84 | + |
| 85 | + |
| 86 | +@app.function(volumes={"/datasets": datasets_volume}, cpu=N_WORKERS, timeout=11 * 60 * 60) |
| 87 | +def label() -> dict: |
| 88 | + import multiprocessing as mp |
| 89 | + from collections import Counter |
| 90 | + |
| 91 | + lines = [ |
| 92 | + l.strip() |
| 93 | + for l in Path(CORPUS).read_text(encoding="utf-8").splitlines() |
| 94 | + if l.strip() |
| 95 | + ][:N_LINES] |
| 96 | + print(f"[data] {len(lines)} lines", flush=True) |
| 97 | + |
| 98 | + with mp.Pool(N_WORKERS) as pool: |
| 99 | + results = pool.map(_label_line, lines, chunksize=200) |
| 100 | + |
| 101 | + from datetime import datetime, timezone |
| 102 | + |
| 103 | + tag_counts = Counter(t for _, tags in results for t in tags) |
| 104 | + OUT.parent.mkdir(parents=True, exist_ok=True) |
| 105 | + with OUT.open("w", encoding="utf-8") as f: |
| 106 | + import re |
| 107 | + diac = re.compile("[ؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۨ-ۭ]") |
| 108 | + for line, tags in results: |
| 109 | + src = diac.sub("", line) |
| 110 | + f.write(json.dumps({"src": src, "gold": line, "tags": tags}, ensure_ascii=False) + "\n") |
| 111 | + manifest = OUT.parent / "MANIFEST.txt" |
| 112 | + manifest.write_text( |
| 113 | + f"lines: {len(results)}\ntop_tags: {tag_counts.most_common(20)}\n" |
| 114 | + f"labeled: {datetime.now(timezone.utc).isoformat()}\n", |
| 115 | + encoding="utf-8", |
| 116 | + ) |
| 117 | + datasets_volume.commit() |
| 118 | + return {"lines": len(results), "top_tags": tag_counts.most_common(10)} |
0 commit comments